Page 403 - Cs_withBlue_J_C11_Flipbook
P. 403

24            }
                  25

                  26          public static void main(int x)  // main
                  27          {

                  28              Pattern ob = new Pattern();
                  29              ob.read(x);

                  30              ob.print(1, 1);
                  31          }
                  32      }


                 When you execute the preceding program, the Method Call dialog box appears:















                 The output of the preceding program is as follows:
                 12345
                 2345
                 345
                 45
                 5

                 13.3.2   Recursive Methods on String
                 While defining a recursive method to handle strings, the base case is reached when the index position of the string
                 becomes equal to the length of the string or -1 or null.

                  Program 9      A class called Word is defined to count number of words in a sentence. The words in a
                                 sentence can be separated by space only. The class description is given below:
                                 Class name         :   Word
                                 Data Members
                                 String s           :   To store a sentence
                                 Member Methods
                                 void read (int l)   :   Accepts any sentence
                                 int count(String s)   :     Using recursive technique, counts and returns number of words in
                                                        the sentence
                                 void show()        :   Calls count(String s) and prints word count
                                 static void main()   :   Creates object and calls other methods

                   1      import java.util.*;

                   2      class Word



                                                                                                                       401
                                                                                                           Recursion   401
   398   399   400   401   402   403   404   405   406   407   408