Encrypt Program ISC Specimen 2023 Theory

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 uppercase
void freqVowCon(): finds the frequency of the vowels
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.

import java.util.Scanner;
class Encrypt{
    String wrd;
    int len;
    String newWrd;
    public Encrypt(){
        wrd = new String();
        len = 0;
        newWrd = new String();
    }
    public void acceptWord(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the word: ");
        wrd = in.next().toUpperCase();
        len = wrd.length();
    }
    public void freqVowCon(){
        int count = 0;
        for(int i = 0; i < len; i++){
            char ch = wrd.charAt(i);
            if("AEIOU".indexOf(ch) >= 0)
                count++;
        }
        System.out.println("Vowel frequency: " + count);
    }
    public 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;
            }
        }
    }
    public 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();
    }
}

Comments

Popular posts from this blog

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory