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
mainreads the two wordsaandbwithnextLine().maincreatesob = new Mixer(a, b); the constructor stores them inword1andword2and setsmixto an empty string.maincallsob.isValid(), which returnsfalseif either word is empty or a loop withcharAt()finds a space in either word, andtrueotherwise. If it isfalse,mainprintsINVALID INPUTand stops.maincallsob.mix():l1,l2are the lengths of the words andmaxis the larger one;- for
ifrom 0 tomax - 1: ifi < l1addword1.charAt(i)tomix, then ifi < l2addword2.charAt(i); - the method returns
mix, whichmainkeeps inr.
maincallsob.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
| Variable | Data Type | Purpose |
|---|---|---|
word1 | String | Data member that stores the first word |
word2 | String | Data member that stores the second word |
mix | String | Data member that stores the mixed word |
a | String | Parameter of the constructor, and in main the first word typed by the user |
b | String | Parameter of the constructor, and in main the second word typed by the user |
i | int | Loop counter: the position of the character being checked or added |
l1 | int | Length of word1 |
l2 | int | Length of word2 |
max | int | The larger of l1 and l2: the number of rounds of the loop in mix() |
sc | Scanner | Reads the words typed by the user |
ob | Mixer | Object of the class created in main |
r | String | Receives the mixed word returned by mix() in main |
args | String[] | 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: HWEOLRLLODSample 2
Enter the first word: ABC
Enter the second word: 12345
First word: ABC
Second word: 12345
Mixed word: A1B2C345Sample 3
Enter the first word: A
Enter the second word: XYZ
First word: A
Second word: XYZ
Mixed word: AXYZSample 4
Enter the first word: SAME
Enter the second word: SAME
First word: SAME
Second word: SAME
Mixed word: SSAAMMEESample 5
Enter the first word: HELLO
Enter the second word: WOR LD
INVALID INPUTSample 6
Enter the first word: HELLO
Enter the second word:
INVALID INPUTDry run
Take a = ABC and b = 12345. Here l1 = 3, l2 = 5 and max = 5. The loop in mix() runs as follows:
| i | i < l1 ? | i < l2 ? | Characters added | mix after the step |
|---|---|---|---|---|
| 0 | yes | yes | A, 1 | A1 |
| 1 | yes | yes | B, 2 | A1B2 |
| 2 | yes | yes | C, 3 | A1B2C3 |
| 3 | no | yes | 4 | A1B2C34 |
| 4 | no | yes | 5 | A1B2C345 |
mix() returns A1B2C345; display() prints Mixed word: A1B2C345, as in the second sample run.
Variations
- Reverse mix "Mix two words by taking the characters alternately from the end of each word."
- Two at a time "Mix two words by taking two characters at a time from each word."