Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Word Mixer (OOP)

Updated 21 Oct 2026

A word mixer builds a new word by taking one character alternately from two words: first letter of the first word, first letter of the second, second letter of the first, and so on; the leftover characters of the longer word go at the end. In this version the class Mixer has a parameterised constructor Mixer(String a, String b), and the method mix() **returns the mixed word as a String**.

Definition

HELLO and WORLD give HWEOLRLLOD; ABC and 12345 give A1B2C345, because after C3 the first word has ended and 4, 5 are added. Only charAt(), length() and string concatenation are used.

Algorithm

  1. main reads the two words a and b with nextLine().
  2. main creates ob = new Mixer(a, b); the constructor stores them in word1 and word2 and sets mix to an empty string.
  3. main calls ob.isValid(), which returns false if either word is empty or a loop with charAt() finds a space in either word, and true otherwise. If it is false, main prints INVALID INPUT and stops.
  4. main calls ob.mix():
    • l1, l2 are the lengths of the words and max is the larger one;
    • for i from 0 to max - 1: if i < l1 add word1.charAt(i) to mix, then if i < l2 add word2.charAt(i);
    • the method returns mix, which main keeps in r.
  5. main calls ob.display() to print the two words and the mixed word.

Program

The words travel into the object through the constructor, validation is a separate method isValid(), and mix() returns the result.

import java.util.Scanner;

/* Class Mixer builds a new word by taking one character alternately from two words.
   The extra characters of the longer word are added at the end.
   The words are given to the parameterised constructor, and mix() RETURNS the mixed word.
   Only charAt(), length() and string concatenation are used (no StringBuilder). */
public class Mixer
{
    String word1;   // data member: the first word
    String word2;   // data member: the second word
    String mix;     // data member: the mixed word built from word1 and word2

    /* Parameterised constructor.
       a : the first word, b : the second word */
    Mixer(String a, String b)
    {
        word1 = a;
        word2 = b;
        mix = "";
    }

    /* Checks the two words. returns true if both are non-empty and contain no space,
       otherwise false */
    boolean isValid()
    {
        if (word1.length() == 0 || word2.length() == 0)     // an empty word is not allowed
        {
            return false;
        }
        for (int i = 0; i < word1.length(); i++)            // check word1 for spaces
        {
            if (word1.charAt(i) == ' ')
            {
                return false;
            }
        }
        for (int i = 0; i < word2.length(); i++)            // check word2 for spaces
        {
            if (word2.charAt(i) == ' ')
            {
                return false;
            }
        }
        return true;
    }

    /* Builds the mixed word by taking one character alternately from word1 and word2.
       When one word ends, the rest of the longer word is still added.
       returns the mixed word as a String */
    String mix()
    {
        int l1 = word1.length();            // length of the first word
        int l2 = word2.length();            // length of the second word
        int max = l1;                       // the longer length decides how many rounds are needed
        if (l2 > max)
        {
            max = l2;
        }
        mix = "";
        for (int i = 0; i < max; i++)
        {
            if (i < l1)                     // word1 still has a character at position i
            {
                mix = mix + word1.charAt(i);
            }
            if (i < l2)                     // word2 still has a character at position i
            {
                mix = mix + word2.charAt(i);
            }
        }
        return mix;
    }

    /* Prints the two words and the mixed word. returns nothing */
    void display()
    {
        System.out.println("First word: " + word1);
        System.out.println("Second word: " + word2);
        System.out.println("Mixed word: " + mix);
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);    // reads the input
        System.out.print("Enter the first word: ");
        String a = sc.nextLine();               // first word typed by the user
        System.out.print("Enter the second word: ");
        String b = sc.nextLine();               // second word typed by the user
        Mixer ob = new Mixer(a, b);             // object holding both words
        if (ob.isValid())
        {
            String r = ob.mix();                // mixed word returned by mix()
            ob.display();
        }
        else
        {
            System.out.println("INVALID INPUT");
        }
    }
}

Variable description table

VariableData TypePurpose
word1StringData member that stores the first word
word2StringData member that stores the second word
mixStringData member that stores the mixed word
aStringParameter of the constructor, and in main the first word typed by the user
bStringParameter of the constructor, and in main the second word typed by the user
iintLoop counter: the position of the character being checked or added
l1intLength of word1
l2intLength of word2
maxintThe larger of l1 and l2: the number of rounds of the loop in mix()
scScannerReads the words typed by the user
obMixerObject of the class created in main
rStringReceives the mixed word returned by mix() in main
argsString[]Command-line arguments of main (not used)

Sample input and output

Sample 1

Enter the first word: HELLO
Enter the second word: WORLD
First word: HELLO
Second word: WORLD
Mixed word: HWEOLRLLOD

Sample 2

Enter the first word: ABC
Enter the second word: 12345
First word: ABC
Second word: 12345
Mixed word: A1B2C345

Sample 3

Enter the first word: A
Enter the second word: XYZ
First word: A
Second word: XYZ
Mixed word: AXYZ

Sample 4

Enter the first word: SAME
Enter the second word: SAME
First word: SAME
Second word: SAME
Mixed word: SSAAMMEE

Sample 5

Enter the first word: HELLO
Enter the second word: WOR LD
INVALID INPUT

Sample 6

Enter the first word: HELLO
Enter the second word: 
INVALID INPUT

One value per line

Dry run

Take a = ABC and b = 12345. Here l1 = 3, l2 = 5 and max = 5. The loop in mix() runs as follows:

ii < l1 ?i < l2 ?Characters addedmix after the step
0yesyesA, 1A1
1yesyesB, 2A1B2
2yesyesC, 3A1B2C3
3noyes4A1B2C34
4noyes5A1B2C345

mix() returns A1B2C345; display() prints Mixed word: A1B2C345, as in the second sample run.

Variations

  1. Reverse mix "Mix two words by taking the characters alternately from the end of each word."
  2. Two at a time "Mix two words by taking two characters at a time from each word."