Capital Word ISC Computer Science 2018 Theory

A class Capital has been defined to check whether a sentence has words beginning with a capital letter or not.

Some of the members of the class are given below:

Class name: Capital

Data members/instance variables:
sent: to store a sentence
freq: stores the frequency of words beginning with a capital letter

Member functions/methods:
Capital(): default constructor
void input(): to accept the sentence
boolean isCap(String w): checks and returns true if word begins with a capital letter, otherwise returns false
void display(): displays the sentence along with the frequency of the words beginning with a capital letter

Specify the class Capital, giving details of the constructor, void input(), boolean isCap(String) and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.util.Scanner;
import java.util.StringTokenizer;
class Capital{
    String sent;
    int freq;
    public Capital(){
        sent = "";
        freq = 0;
    }
    public void input(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the sentence: ");
        sent = in.nextLine();
        sent = sent.trim();
        sent += " ";
    }
    public boolean isCap(String w){
        return Character.isUpperCase(w.charAt(0));
    }
    public void display(){
        System.out.println("Sentence: " + sent);
        StringTokenizer st = new StringTokenizer(sent);
        int count = st.countTokens();
        for(int i = 1; i <= count; i++){
            if(isCap(st.nextToken()))
                freq++;
        }
        System.out.println("Frequency: " + freq);
    }
    public static void main(String[] args){
        Capital obj = new Capital();
        obj.input();
        obj.display();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory