Page 357 - Cs_withBlue_J_C11_Flipbook
P. 357

20                  System.out.println(w);
                  21              }

                  22          }
                  23          public static void main()

                  24          {
                  25              // creating objects of Words class and calling methods

                  26                Words ob=new Words();
                  27              ob.accept();
                  28              ob.print();

                  29          }

                  30      }
                 The output of the preceding program is as follows:
                 Enter sentence
                 Hello, my name is Divya
                 The words are:
                 Hello
                 my
                 name
                 is
                 Divya


                  Program 2      To define a class called Longest which will print the longest word in the sentence. The words
                                 are separated by space, comma (,), full stop (.) and question mark (?). The class description
                                 is given as follows:
                                 Data Members
                                 String sen         :   To store sentence
                                 int len            :   To store the length of the longest word
                                 Member Methods
                                 void read()        :   Reads a sentence in variable sen
                                 void printlong()   :   Prints the word with maximum length
                                 static void main()   :   Creates objects and calls the other methods

                   1      import java.util.*;

                   2      class Longest
                   3      {
                   4          //declaring data members

                   5          String sen;
                   6          int len;

                   7          void read()



                                                                                                                       355
                                                                                 Basic Input/Output and Data File Handling    355
   352   353   354   355   356   357   358   359   360   361   362