PRASHNIKAप्रश्निका
Back to the paper

A class Encrypt has been defined to replace only the vowels in a word by the next corresponding…

Computer Science202310 marksProgram
A class Encrypt has been defined to replace only the vowels in a word by the next corresponding vowel and forms a new word. i.e. A → E, E → I, I → O, O → U and U → A Example: Input: COMPUTER Output: CUMPATIR Some of the members of the class are given below: Class name : Encrypt Data members/instance variables: wrd : to store a word len : integer to store the length of the word newwrd : to store the encrypted word Methods / Member functions: Encrypt( ) : default constructor to initialize data members with legal initial values void acceptword( ) : to accept a word in UPPER CASE void freqvowcon( ) : finds the frequency of the vowels and consonants in the word stored in ‘wrd’ and displays them with an appropriate message void nextVowel( ) : replaces only the vowels from the word stored in ‘wrd’ by the next corresponding vowel and assigns it to ‘newwrd’, with the remaining alphabets unchanged void disp( ) : Displays the original word along with the encrypted word Specify the class Encrypt giving details of the constructor( ), void acceptword( ), void freqvowcon( ), void nextVowel( ) and void disp( ). Define a main( ) function to create an object and call the functions accordingly to enable the task.

Answer

Answer

AI
import java.util.Scanner;

class Encrypt
{
    String wrd, newwrd;
    int len;

    Encrypt()
    {
        wrd = "";
        newwrd = "";
        len = 0;
    }

    void acceptword()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word in UPPER CASE: ");
        wrd = sc.nextLine();
        len = wrd.length();
    }

    void freqvowcon()
    {
        int vcount = 0, ccount = 0;
        for (int i = 0; i < len; i++)
        {
            char ch = wrd.charAt(i);
            if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
                vcount++;
            else
                ccount++;
        }
        System.out.println("Number of vowels: " + vcount);
        System.out.println("Number of consonants: " + ccount);
    }

    void nextVowel()
    {
        for (int i = 0; i < len; i++)
        {
            char ch = wrd.charAt(i);
            switch (ch)
            {
                case 'A': newwrd += 'E'; break;
                case 'E': newwrd += 'I'; break;
                case 'I': newwrd += 'O'; break;
                case 'O': newwrd += 'U'; break;
                case 'U': newwrd += 'A'; break;
                default: newwrd += ch;
            }
        }
    }

    void disp()
    {
        System.out.println("Original word  : " + wrd);
        System.out.println("Encrypted word : " + newwrd);
    }

    public static void main(String args[])
    {
        Encrypt obj = new Encrypt();
        obj.acceptword();
        obj.freqvowcon();
        obj.nextVowel();
        obj.disp();
    }
}
Explanation: nextVowel() scans each character of wrd and, if it is one of the vowels A/E/I/O/U, replaces it by the next vowel in the cycle A->E->I->O->U->A using a switch statement, copying all other (consonant) characters unchanged into newwrd. freqvowcon() separately counts and displays the number of vowels and consonants in wrd. Tested (run for real): for wrd="COMPUTER" the program printed "Number of vowels: 3", "Number of consonants: 5", and Encrypted word "CUMPATIR", matching the example in the question exactly.
Arrays, Strings

From ISC 2023 Specimen Computer Science Paper 1, question 8.