Page 257 - Computer science 868 Class 12
P. 257
8.12.2 Programs Implementing the Constructor
Program 8 A sequence of Fibonacci strings is generated as follows:
S = “a”, S = “b”, S = S (n-1) + S (n-2) where ‘+’ denotes concatenation. Thus the sequence is:
n
f
0
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 (‘+’, e.g., concatenation) first two strings is the third
string, e.g., “a” is the first string, “b” is the second string,
then the third will be “ba”, and the fourth 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.
1 import java.util.*;
2 class FiboString
3 {
4 String x,y,z;
5 int n;
6 FiboString() // No Argument constructor
7 {
8 x="a";
9 y="b";
10 z="ba";
11 }
12
13 void accept()
14 {
15 Scanner Sc = new Scanner (System.in);
255
Methods 255

