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

A class Rearrange has been defined to modify a word by bringing all the vowels in the word at the…

Computer Science201910 marksProgram
A class Rearrange has been defined to modify a word by bringing all the vowels in the word at the beginning followed by the consonants. Example: ORIGINAL becomes OIIARGNL Some of the members of the class are given below: Class name : Rearrange Data member/instance variable: wrd : to store a word newwrd : to store the rearranged word Member functions/methods: Rearrange( ) : default constructor void readword( ) : to accept the word in UPPER case void freq_vow_con( ) : finds the frequency of vowels and consonants in the word and displays them with an appropriate message void arrange( ) : rearranges the word by bringing the vowels at the beginning followed by consonants void display( ) : displays the original word along with the rearranged word Specify the class Rearrange, giving the details of the constructor( ), void readword( ), void freq_vow_con( ), void arrange( ) and void display( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.

Answer

Answer

AI
import java.util.Scanner;

class Rearrange
{
    String wrd, newwrd;

    Rearrange()
    {
        wrd = "";
        newwrd = "";
    }

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

    void freq_vow_con()
    {
        int vcount = 0, ccount = 0;
        for (int i = 0; i < wrd.length(); 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 arrange()
    {
        String vowels = "";
        String consonants = "";
        for (int i = 0; i < wrd.length(); i++)
        {
            char ch = wrd.charAt(i);
            if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
                vowels = vowels + ch;
            else
                consonants = consonants + ch;
        }
        newwrd = vowels + consonants;
    }

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

    public static void main(String args[])
    {
        Rearrange obj = new Rearrange();
        obj.readword();
        obj.freq_vow_con();
        obj.arrange();
        obj.display();
    }
}
Explanation: freq_vow_con() scans wrd once and counts vowels and consonants, printing both counts. arrange() scans wrd character by character, appending each vowel to a separate 'vowels' string and each consonant to a 'consonants' string (preserving their original relative order within each group), then sets newwrd = vowels + consonants so all vowels come first followed by all consonants. Tested (run for real): for wrd="ORIGINAL" the program printed "Number of vowels: 4", "Number of consonants: 4", and Rearranged word "OIIARGNL", matching the example in the question exactly.
Objects

From ISC 2019 Computer Science Paper 1, question 9.