#### Question:

A sequence of Fibonacci Strings is generated as follows:

S_{0} = “a”, S_{1} = “b”, S_{n} = 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”, 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. Eg. “a” is first string, “b” is second string then the third string will be “ba” and 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.

#### Programming Code:

/** * The class FiboString prints the sequence of fibonacci strings * @author : www.javaforschool.com * @Program Type : BlueJ Program - Java * @Question Year : ISC 2014 Question 10 (Theory) */ import java.io.*; class FiboString { String x,y,z; int n; FiboString() // Constructor { x = "a"; y = "b"; z = "ba"; // mentioned in the question otherwise not required. z = "" is sufficient } void accept()throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nEnter the number of terms : "); n = Integer.parseInt(br.readLine()); } void generate() { System.out.print("nThe Fibonacci String Series is : "); if(n <= 1) // If no of terms is less than or equal to 1 System.out.print(x); else // If no of terms is more than or equal to 2 { System.out.print(x+", "+y); for(int i=3; i<=n; i++) { z = y+x; System.out.print(", "+z); x = y; y = z; } } } public static void main(String args[]) throws IOException { FiboString ob = new FiboString(); ob.accept(); ob.generate(); } }

#### Output:

**1) **Enter the number of terms : 2

The Fibonacci String Series is : a, b

**2)** Enter the number of terms : 5

The Fibonacci String Series is : a, b, ba, bab, babba

Helpfull website

Nice program…very simple code..easy to understand….and remember