C Programs Tutorials | IT Developer
IT Developer

Java Programs - Solved 2018 Theory Paper ISC Computer Science



Share with a Friend

Solved 2018 Theory Paper ISC Computer Science

Class 12 - ISC Computer Science Solved Theory Papers

Words Beginning with Capital Letter - ISC 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 the details of the constructor, void input(), boolean isCap(String) and void display(). Define the main() function to create an object and call the function accordingly to enable the task.

Program:

import java.io.*; import java.util.StringTokenizer; class Capital{ private String sent; private int freq; public Capital(){ sent = new String(); freq = 0; } public void input()throws IOException{ InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); System.out.print("Sentence: "); sent = br.readLine(); } public boolean isCap(String w){ char ch = w.charAt(0); if(Character.isUpperCase(ch)) return true; return false; } public void display(){ StringTokenizer st = new StringTokenizer(sent, " ,?!."); int count = st.countTokens(); for(int i = 1; i <= count; i++){ String word = st.nextToken(); if(isCap(word)) freq++; } System.out.println("Sentence: " + sent); System.out.println("Frequency: " + freq); } public static void main(String args[]) throws IOException{ Capital obj = new Capital(); obj.input(); obj.display(); } }

Output

OUTPUT 1:
Sentence: I am a Student of ITDeveloper
Sentence: I am a Student of ITDeveloper
Frequency: 3


OUTPUT 2:

Sentence: I love to play Cricket
Sentence: I love to play Cricket
Frequency: 2