‹ Back to the paper
A class Encode has been defined to replace only the vowels in a word by the next corresponding…
A class Encode has been defined to replace only the vowels in a word by the next corresponding vowel and form a new word.
i.e. A → E, E → I, I → O, O → U, U → A and
a → e, e → i, i → o, o → u, and u → a
Example: Input: Institution
Output: Onstotatoun
Some of the members of the class are given below:
Class name : Encode
Data members/instance variables:
word : to store a word
length : integer to store the length of the word
new_word : to store the encoded word
Methods / Member functions:
Encode( ) : default constructor to initialize data members with legal initial values
void acceptWord( ) : to accept a word
void nextVowel( ) : to replace only the vowels from the word stored in ‘word’ by the next corresponding vowel and to assign it to ‘newword’, with the remaining alphabets unchanged
void display( ) : to display the original word along with the encrypted word
Specify the class Encode giving details of the constructor( ), void acceptWord( ), void nextVowel( ) and void display( ). Define a main ( ) function to create an object and call the functions accordingly to enable the task.
Answer
Answer
AIimport java.util.Scanner;
class Encode
{
String word, new_word;
int length;
Encode()
{
word = "";
new_word = "";
length = 0;
}
void acceptWord()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
word = sc.nextLine();
length = word.length();
}
void nextVowel()
{
for (int i = 0; i < length; i++)
{
char ch = word.charAt(i);
switch (ch)
{
case 'A': new_word += 'E'; break;
case 'E': new_word += 'I'; break;
case 'I': new_word += 'O'; break;
case 'O': new_word += 'U'; break;
case 'U': new_word += 'A'; break;
case 'a': new_word += 'e'; break;
case 'e': new_word += 'i'; break;
case 'i': new_word += 'o'; break;
case 'o': new_word += 'u'; break;
case 'u': new_word += 'a'; break;
default: new_word += ch;
}
}
}
void display()
{
System.out.println("Original word : " + word);
System.out.println("Encoded word : " + new_word);
}
public static void main(String args[])
{
Encode obj = new Encode();
obj.acceptWord();
obj.nextVowel();
obj.display();
}
}Explanation: nextVowel() scans each character of `word`; if it is one of the 10 vowels (upper- or lower-case), it is replaced by the next vowel in the cycle A->E->I->O->U->A (and a->e->i->o->u->a) using a switch statement, while all other (consonant) characters are copied unchanged into `new_word`. Tested (run for real): for word="Institution", the program correctly produced new_word="Onstotatoun", matching the example given in the question.From ISC 2024 Specimen Computer Science Paper 1, question 8.