Categories
Class 11 Class 12

Word Wise Vowel Frequency ISC 2019 Specimen

Design a class WordWise to separate words from a sentence and find the frequency of the vowels in each word.

Some of the members of the class are given below:

Class name: WordWise
Data members/instance variables:
str: to store a sentence.
Member functions/methods:
WordWise(): default constructor.
void readSent(): to read a sentence.
int freqVowel(String w): returns the frequency of vowels in the parameterized string ‘w’.
void arrange(): displays each word of the sentence in a separate line along with the frequency of vowels for each word by invoking the function freqVowel().

Define the class WordWise, giving details of the constructor, void readSent(), int freqVowel(String) and void arrange(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.io.*;
import java.util.StringTokenizer;
class WordWise{
    private String str;
    public WordWise(){
        str = new String();
    }
    public void readSent()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Sentence: ");
        str = br.readLine();
        str = str.toUpperCase();
        str = str.trim();
    }
    public int freqVowel(String w){
        int count = 0;
        for(int i = 0; i < w.length(); i++){
            char ch = w.charAt(i);
            switch(ch){
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                count++;
            }
        }
        return count;
    }
    public void arrange(){
        StringTokenizer st = new StringTokenizer(str, " ,.?!");
        int count = st.countTokens();
        for(int i = 1; i <= count; i++){
            String word = st.nextToken();
            int f = freqVowel(word);
            System.out.println(word + "\t" + f);
        }
    }
    public static void main(String args[])throws IOException{
        WordWise obj = new WordWise();
        obj.readSent();
        obj.arrange();
    }
}

Leave a Reply