Page 375 - Computer science 868 Class 12
P. 375

Program 8     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 initialise the data members
                                 TheString(String ds)     :   parameterised 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 void
                                 display(). Define the main() function to create an object and call the functions accordingly to
                                 enable the task.                                                           [ISC 2015]

                   1       import java.util.*;
                   2       class TheString

                   3       {
                   4           String str;
                   5           int len,wordCount,cons;

                   6           TheString()

                   7           {
                   8               str="";
                   9               len=0;

                   10              wordCount=0;
                   11              cons=0;

                   12          }
                   13          TheString(String ds)

                   14          {
                   15              str=ds;

                   16              if(str.length()>100)
                   17                  str=str.substring(0, 101);

                   18              len=str.length();








                                                                                                                       373
                                                                                                              Strings  373
   370   371   372   373   374   375   376   377   378   379   380