InterviewSolution
| 1. |
A class ConsChange has been defined with the following details:Class name: ConsChange Data members/instance variables: word: stores the word len: stores the length of the word Member functions/methods: ConsChange(): default constructor void readword(): accepts the word in lowercase void shiftcons(): shifts all the consonants of the word at the beginning followed by the vowels (e.g. spoon becomes spnoo) void changeword(): changes the case of all occurring consonants of the shifted word to uppercase, for e.g. (spnoo becomes SPNoo) void show(): displays the original word, shifted word and the changed word Specify the class ConsChange giving the details of the constructor ), void readword ( ), void shiftcons (), void changeword() and void show(). Define the main() function to create an object and call the functions accordingly to enable the task. |
|
Answer» import java.io.*; class ConsChange { String word; int len; public ConsChange() { word = new String (); len = word, length (); } public void readword () throws IOException { InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader (isr); System.out.println ("Enter the word"): word = br.readLine(); word = word, trim().toLowerCase(); if (word.indexOf (") > 0) word = word.substring (0, word.indexOf (")); len = word, length(); } public void shiftcons() { String cons = new String(); String vowel = new String(); for (int i= 0; i < len; i++) { char ch = word.charAt (i); switch (ch) { case 'a'; case 'e'; case 'i'; case 'o'; case 'u'; vowel+ = ch; break; default: cons + = ch ; } } word = cons + vowel; System.out.println (''Shifted Word+word): changeword(); System.outprintln("Changed Word: "+ word); } public void changeword() { int pos = -1; for (int i = 0; i < len; i++) char ch = word.charAt (i); switch (ch) { case 'a'; case 'e'; case 'o'; case 'u'; break; default: pos = i; } word = word.substring (0, pos + 1). toUpperCase ()+word.substring (pos + 1); } } |
|