Page 332 - computer science (868) class 11
P. 332

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()



                330330  Touchpad Computer Science-XI
   327   328   329   330   331   332   333   334   335   336   337