InterviewSolution
| 1. |
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: RearrangeData 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 casevow 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» import java.io.*; import java.util.*; class Rearrange { private String wrd; private String newwrd; public Rearrange() { wrd = new String(); newwrd = new String(); } public void readword() throws IOException { Scanner sc = new Scanner(System.in); System.out.print("Enter the word:"); wxd = sc.next(); } public void freq_vow_con() { wrd = wrd.toUpperCase(); int v = 0; int c = 0; forint i = 0; i < wrd.length(); i++) { char ch = wrd. char At(i); if(Character.isLetter(ch)) { switch(ch) { } case 'A': case'E': case T: case 'O': case 'U': v++; break; default: C++; } } } System.out.println("Frequency of vowels: "+v); System.out.println("Frequency of consonants: "+c); } public void arrange() { String v = new String(); String c = new String(); wrd = wrd.toUpperCase(); for(int i = 0; i < wrd. length(); i++) { char ch = wrd.charAt(i); if(Character.isLetter!ch)) { switch(ch) { case 'A': case'E': case T: case 'O': case 'U': v += ch; break; default: c + = ch; } } } newwrd = v + c; } public void display() { System.out.println("Original word:" + wrd); System.out.prfntln("Rearranged word:" + newwrd); } public static void main(String args[])throws IOException { Rearrange obj = new Rearrange(); obj.readword(); obj.freq_vow_con(); obj.arrange(); obj.display(); } } |
|