Question
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 lower case
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 occuring consonants of the shifted word to upper case, 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.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class ConsChange
{
String word;
int len;
Scanner sc=new Scanner(System.in);
ConsChange()
{
len=0;
word="";
}
void readword()
{
System.out.println("Enter word in Lowercase");
word=sc.next();
len=word.length();
}
void shiftcons()
{
String consonants="",vowels="",sortedWord="";
char ch=' ';
for(int i=0;i< len;i++)
{
ch=word.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
vowels+=ch;
}
else
{
consonants+=ch;
}
}
sortedWord=consonants+vowels;
System.out.println("Sorted Word="+sortedWord);
word=sortedWord;
}
void changeword()
{
char ch=' ';
String newWd="";
for(int i=0;i< len;i++)
{
ch=word.charAt(i);
if(ch!='a'&&ch!='e'&&ch!='i'&&ch!='o'&&ch!='u')
{
newWd+=Character.toUpperCase(ch);
}
else
{
newWd+=ch;
}
}
System.out.println("Changed word="+newWd);
}
void show()
{
System.out.println("Originalword="+word);
shiftcons();
changeword();
}
public static void main()
{
ConsChange ob1=new ConsChange();
ob1.readword();
ob1.show();
}
}