C Programs Tutorials | IT Developer
IT Developer

Java Programs - Solved 2015 Theory Paper ISC Computer Science



Share with a Friend

Solved 2015 Theory Paper ISC Computer Science

Class 12 - ISC Computer Science Solved Theory Papers

String Frequency Program - ISC 2015 Theory

A class TheString accepts a string of a maximum of 100 characters with only one blank space between the words.

Some of the members of the class are as follows:

Class name: TheString
Data members/instance variables:
str: to store a string.
len: integer to store the length of the string.
wordCount: integer to store the number of words.
cons: integer to store the number of consonants.

Member functions/methods:
TheString(): default constructor to initialize the data members.
The String(String ds): parameterized constructor to assign str = ds.
void countFreq(): to count the number of words and the number of consonants and store them in wordCount and cons respectively.
void display(): to display the original string, along with the number of words and the number of consonants.

Specify the class TheString giving the details of the constructors, void countFreq() and display(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.io.*; class TheString{ private String str; private int len; private int wordCount; private int cons; public TheString(){ str = new String(); len = 0; wordCount = 0; cons = 0; } public TheString(String ds){ str = ds; if(str.length() > 100) str = str.substring(0, 100); str = str.trim(); len = str.length(); wordCount = 0; cons = 0; } public void countFreq(){ if(len > 0) wordCount = 1; String s = str.replace(" ", ""); wordCount += len - s.length(); s = new String(str); for(int i = 0; i < len; i++){ char ch = str.charAt(i); ch = Character.toUpperCase(ch); if(Character.isLetter(ch)){ switch(ch){ case 'A': case 'E': case 'I': case 'O': case 'U': continue; default: cons++; } } } } public void display(){ System.out.println("Original string: " + str); System.out.println("Number of words: " + wordCount); System.out.println("Number of consonants: " + cons); } public static void main(String args[]) throws IOException{ InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); System.out.print("Enter the string: "); String s = br.readLine(); TheString obj = new TheString(s); obj.countFreq(); obj.display(); } }

Output

 
OUTPUT 1:

Enter the string: IT Developer
Original string: IT Developer
Number of words: 2
Number of consonants: 6


OUTPUT 2:

Enter the string: Mahatma Gandhi
Original string: Mahatma Gandhi
Number of words: 2
Number of consonants: 8

OUTPUT 3:

Enter the string: Mohan Das Karmchand Gandhi
Original string: Mohan Das Karmchand Gandhi
Number of words: 4
Number of consonants: 16