Vowel Word ISC Computer Science 2012 Theory
Design a class VowelWord to accept a sentence and calculate the frequency of words that begin with a vowel. The words in the input string are separated by a single blank space and terminated by a full stop. The description of the class is given below:
Class name: VowelWord
Data members/instance variables:
str: to store a sentence
freq: store the frequency of the words beginning with a vowel
Member functions:
VowelWord(): constructor to initialize data members to legal initial values
void readStr(): to accept a sentence
void freqVowel(): counts the frequency of the words that begin with a vowel
void display(): to display the original string and the frequency of the words that begin with a vowel
Specify the class VowelWord giving details of the constructor, void readStr(), void freqVowel() and void display(). Also define the main() function to create an object and call the methods accordingly to enable the task.
class VowelWord{
String str;
int freq;
public VowelWord(){
str = new String();
freq = 0;
}
public void readStr(){
Scanner in = new Scanner(System.in);
System.out.print("Enter the sentence: ");
str = in.nextLine();
}
public void freqVowel(){
String w = new String();
for(int i = 0; i < str.length(); i++){
char ch = str.charAt(i);
if(Character.isLetterOrDigit(ch))
w += ch;
else{
char first = w.charAt(0);
if("AEIOUaeiou".indexOf(first) >= 0)
freq++;
w = new String();
}
}
}
public void display(){
System.out.println("Original string: " + str);
System.out.println("Frequency: " + freq);
}
public static void main(String[] args){
VowelWord obj = new VowelWord();
obj.readStr();
int len = obj.str.length();
char last = obj.str.charAt(len - 1);
if(last != '.'){
System.out.println("INVALID INPUT");
return;
}
obj.freqVowel();
obj.display();
}
}
Comments
Post a Comment