Categories
Class 11 Class 12

Vowel Word ISC 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 display(). Also define the main() function to create an object and call the methods accordingly to enable the task.

import java.io.*;
import java.util.StringTokenizer;
class VowelWord{
    String str;
    int freq;
    public VowelWord(){
        str = "";
        freq = 0;
    }
    public void readStr()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Sentence: ");
        str = br.readLine();
        str = str.trim();
        int len = str.length();
        char ch = str.charAt(len - 1);
        if(ch != '.'){
            System.out.println("Sentence must terminate with a full stop.");
            System.exit(0);
        }
    }
    public void freqVowel(){
        StringTokenizer st = new StringTokenizer(str, " .");
        int count = st.countTokens();
        for(int i = 1; i <= count; i++){
            String word = st.nextToken();
            word = word.toUpperCase();
            char ch = word.charAt(0);
            switch(ch){
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                freq++;
            }
        }
    }
    public void display(){
        System.out.println("Original string: " + str);
        System.out.println("Required frequency: " + freq);
    }
    public static void main(String args[])throws IOException{
        VowelWord obj = new VowelWord();
        obj.readStr();
        obj.freqVowel();
        obj.display();
    }
}

Leave a Reply