Frequency of And, An ISC 2011 Theory

Input a sentence from the user and count the number of times the words “an” and “and” are present in the sentence. Design a class Frequency using the description given below:

Class name: Frequency
Data members/variables:
text: stores the sentence.
countAnd: to store the frequency of the word “and”.
countAn: to store the frequency of the word “an”.
len: stores the length of the string.
Member functions/methods:
Frequency(): constructor to initialize the instance variables.
void accept(String n): to assign ‘n’ to ‘text’, where the value of the parameter ‘n’ should be in lowercase.
void checkAndFreq(): to count the frequency of “and”.
void checkAnFreq(): to count the frequency of “an”.
void display(): to display the number of “and” and “an” with appropriate messages.

Specify the class Frequency giving details of the constructor, void accept(String), void checkAndFreq(), void checkAnFreq() and void 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 Frequency{
    String text;
    int countAnd;
    int countAn;
    int len;
    public Frequency(){
        text = "";
        countAnd = 0;
        countAn = 0;
        len = 0;
    }
    public void accept(String n){
        text = n.toLowerCase();
        countAnd = 0;
        countAn = 0;
        len = text.length();
    }
    public void checkAndFreq(){
        StringTokenizer st = new StringTokenizer(text, "!.?, ");
        int count = st.countTokens();
        for(int i = 1; i <= count; i++){
            String word = st.nextToken();
            if(word.equals("and"))
                countAnd++;
        }
    }
    public void checkAnFreq(){
        StringTokenizer st = new StringTokenizer(text, "!.?, ");
        int count = st.countTokens();
        for(int i = 1; i <= count; i++){
            String word = st.nextToken();
            if(word.equals("an"))
                countAn++;
        }
    }
    public void display(){
        System.out.println("Number of Ands: " + countAnd);
        System.out.println("Number of Ans: " + countAn);
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the sentence: ");
        String s = br.readLine();
        Frequency obj = new Frequency();
        obj.accept(s);
        obj.checkAndFreq();
        obj.checkAnFreq();
        obj.display();
    }
}

4 thoughts on “Frequency of And, An ISC 2011 Theory

  1. Sir,a program is given which states that:
    WAP in Java to accept a word and check whether it is a Magic string or not.A ‘Magic Word’ is a word which has at any position two consecutive letters. This can be achieved by taking the ASCII values of the characters.
    Eg:
    Sample Input:ABDOMEN
    Sample output:Magic String

  2. Sir,a program is given which states that:
    WAP in Java to display all the twin prime nos from 1 to n where n is the no. of terms to be accepted by the user. (n>0)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.