Page 373 - Computer science 868 Class 12
P. 373

Program 7     A sequence of Fibonacci strings is generated as follows:
                                 S0 = “a”, SF = “b”, Sn = S(n-1) + S(n-2) where ‘+’ denotes concatenation. Thus the sequence is:
                                 a, b, ba, bab, babba, babbabab,……., n terms.
                                 Design a class FiboString to generate Fibonacci strings. Some of the members of the class are
                                 given below.
                                 Class name               :   FiboString
                                 Data Members/Instance variables

                                 x                        :   to store the first string
                                 y                        :   to store the second string
                                 z                        :   to store the concatenation of the previous two strings
                                 n                        :   to store the number of terms
                                 Member Functions/Methods
                                 FiboString()             :   constructor to assign x=“a”, y=“b” and z=“ba”
                                 void accept()            :   to accept the number of terms ‘n’
                                 void generate()          :    to generate and  print the Fibonacci  strings.  The sum of
                                                              (‘+’ i.e., concatenation) first two strings is the third string. For
                                                              example, “a” is first string, “b” is second string then the third
                                                              string will be “ba”, and fourth string will be “bab” and so on
                                 Specify the class FiboString giving  details  of the constructor(),  void  accept()  and  void
                                 generate(). Define the main() function to create an object and call the functions accordingly
                                 to enable the task.                                                        [ISC 2014]

                   1       import java.util.*;
                   2       class FiboString

                   3       {

                   4           String x,y,z;
                   5           int n;
                   6           FiboString()

                   7           {
                   8               x="a";

                   9               y="b";
                   10              z="ba";

                   11          }
                   12

                   13          void accept()
                   14          {











                                                                                                                       371
                                                                                                              Strings  371
   368   369   370   371   372   373   374   375   376   377   378