icse-promo

Question

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.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner; 
public class Rearrange
{
    String wrd,newwrd;
    Scanner sc=new Scanner(System.in);
    Rearrange()
    {
        wrd="";
        newwrd="";
    }

    void readword()
    {
        System.out.println("Enter a word" );
        wrd=sc.next(); 
        wrd=wrd.toUpperCase();
    }
    
    void freq_vow_con()
    {
        int vowelFrequency=0,consonantFrequency=0;
        char ch=' ';
        for(int  i=0;i< wrd.length();i++)
        {
            ch=wrd.charAt(i);
            if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
            {
                vowelFrequency++;
            }
        }
        consonantFrequency= wrd.length()- vowelFrequency ;
        System.out.println("vowels = "+ vowelFrequency);
        System.out.println("consonants = " + consonantFrequency);
    }

    void arrange()
    { 
        char ch=' ';
        String vowel="",consonants="";
        for(int i=0;i< wrd.length();i++)
        {
            ch=wrd.charAt(i);
            if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
            {
                vowel=vowel+ch;
            }
            else
            {
                consonants =consonants+ch;
            }
        }
        newwrd= vowel+consonants;
    }

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

    public static void main()
    { 
        Rearrange ob1=new Rearrange(); 
        ob1.readword();
        ob1.freq_vow_con(); 
        ob1.arrange();
        ob1.display();
    }
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *