ISC 2020 Computer Science Theory Mix Two Words Program

A class Mix has been defined to mix two words, character by character, in the following manner:

The first character of the first word is followed by the first character of the second word and so on. If the words are of different length, the remaining characters of the longer word are put at the end.

Example: If the first word is “JUMP” and the second word is “STROLL”, then the required word will be “JSUTMRPOLL”.

Some of the members of the class are given below:

Class name: Mix
Data members/instance variables:
wrd: to store a word.
len: to store the length of the word.
Member functions/methods:
Mix(): default constructor to initialize the data members with legal initial values.
void feedWord(): to accept the word in uppercase.
void mixWord(Mix p, Mix q): mixes the words of objects p and q as stated above and stores the resultant word in the current object.
void display(): displays the word.

Specify the class Mix giving details of the constructor, void feedWord(), void mixWord(Mix, Mix) and void display(). Define the main() function to create objects and call the functions accordingly to enable the task.

import java.io.*;
class Mix{
    String wrd;
    int len;
    public Mix(){
        wrd = "";
        len = 0;
    }
    public void feedWord()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        wrd = br.readLine();
        wrd = wrd.trim();
        wrd = wrd.toUpperCase();
        if(wrd.indexOf(' ') > 0)
            wrd = wrd.substring(0, wrd.indexOf(' '));
        len = wrd.length();
    }
    public void mixWord(Mix p, Mix q){
        int i = 0;
        int j = 0;
        while(true){
            if(i < p.len && j < q.len){
                this.wrd += p.wrd.charAt(i);
                this.wrd += q.wrd.charAt(j);
            }
            else
                break;
            i++;
            j++;
        }
        if(i < p.len)
            this.wrd += p.wrd.substring(i);
        if(j < q.len)
            this.wrd += q.wrd.substring(i);
    }
    public void display(){
        System.out.println(wrd);
    }
    public static void main(String args[])throws IOException{
        Mix obj1 = new Mix();
        Mix obj2 = new Mix();
        Mix result = new Mix();
        System.out.print("First word: ");
        obj1.feedWord();
        System.out.print("Second word: ");
        obj2.feedWord();
        result.mixWord(obj1, obj2);
        System.out.print("Required word: ");
        result.display();
    }
}

1 thought on “ISC 2020 Computer Science Theory Mix Two Words Program

  1. Sir,3 programs are given which states that:
    1) WAP in Python to accept a number.Now check whether it is automorphic or not using Recursion.
    2) WAP in Python to accept a number.Now check whether it is unique number or not.
    3)WAP in Python to accept a number.Now count the no.of digits present using for loop

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.