Page 442 - Computer science 868 Class 12
P. 442

obj.input();
                    obj.isDude();
                    }
                    }
                  3.  A class Fibo has been defined to generate the Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, ……. (Fibonacci series are those in which the
                    sum of the previous two terms is equal to the next term). Some of the members of the class are given below:    [ISC 2022]
                     Class name                       :  Fibo
                     Data Members/Instance variable
                     start                            :   integer to store the start value
                     end                              :  integer to store the end value
                     Member Functions/Methods
                     Fibo( )                          :   default constructor
                     void read( )                     :   to accept the numbers
                     int fibo(int n)                  :   return the nth term of a Fibonacci series using recursive technique
                     void display( )                  :   displays the Fibonacci series from start to end by invoking the function fibo()
                     Specify the class Fibo, giving details of the Constructor, void read( ), int fibo(int), and void display( ). Define the main() function
                    to create an object and call the functions accordingly to enable the task.

                Ans.  import java.util.*;
                    class Fibo
                    { int start,end;
                     Fibo()//constructor
                     { start=end=0;}
                     void read()//accept number
                     { Scanner sc=new Scanner(System.in);
                      System.out.println("Enter start &end value");
                      start=sc.nextInt();
                      end=sc.nextInt();
                      }
                     int fibo(int n)
                     { if(n==1) // base case
                       return 0;
                      else if(n==2)
                        return 1;
                      else
                       return fibo(n-1)+fibo(n-2); // recursive case
                     }
                     void display()
                     {  int x;
                       System.out.println(" Fibonacci series from "+start+" to "+end);
                       for(int i=1;i<=end;i++)
                       { x=fibo(i);
                        if(x>=start && x<=end)
                        System.out.print(x+" ");
                        if(x>end)
                         break;
                        }
                      }
                      public static void main()
                      { Fibo ob=new Fibo(); // creating object & calling methods
                       ob.read();
                       ob.display();
                      }
                    }







                440440  Touchpad Computer Science-XII
   437   438   439   440   441   442   443   444   445   446   447