In: Computer Science
1Write a Java program that calculates and displays the Fibonacciseries, defined by the recursive formula
F(n) = F(n-1) + F(n-2).
F(0) and F(1) are given on the command line.Define and use a class Fib with the following structure:
public class Fib {
// constructorpublic Fib(int f0, int f1){.....}
// computes F(n) using an ***iterative*** algorithm, where F(n) =
F(n-1) +
F(n-2) is the recursive definition.
// use instance variables that store F(0) and F(1).
// check parameter and throw exception if n < 0. Don't worry about
arithmetic overflow.
public int f(int n) {....}
// computes F(n) using the ***recursive*** algorithm, where F(n) =
F(n-1)
+ F(n-2) is the recursive definition.
// use instance variables that store F(0) and F(1).// check parameter and throw exception if n < 0. Don't worry about
arithmetic overflow.
public int fRec(int n) {....}
public static void main(String[] args){
// get numbers F(0) and F(1) from args[0] and args[1].// use
either the Scanner class or Integer.parseInt(args[...])// you must
handle possible exceptions !....
// get n from args[2]:....
// create a Fib object with params F(0) and F(1)....
// calculate F(0), ..., F(n) and display them with
System.out.println(...) using
// the iterative methode f(i)....
// calculate F(0), ..., F(n) and display them with
System.out.println(...) using
// the recursive methode fRec(i)....
}
// instance variables store F(0) and F(1):....
};
Write javadoc comments for the Fib class.
// Fib.java
/**
* This class provide iterative as well as recursive mode of
* finding nth value of fibonaci series
*
* To run this program please run like java Fib 1 1 10
*
* This means f(0) = 1, f(1) = 1 and you want to compute series for
10 numbers
*
* @author Anonymous
*
*/
public class Fib {
public Fib(int first, int second)
{
f0 = first;
f1 = second;
}
static int f0;
static int f1;
public static int f(int n) throws Exception
{
if (n < 0)
{
throw new
Exception ("invalid value: n should be greater than equal to
0");
}
if (n == 0) return f0;
if (n == 1) return f1;
int prev = f1;
int prevprev = f0;
int result = 0;
for (int i = 2 ; i <= n;
i++)
{
result = prev +
prevprev;
prevprev =
prev;
prev =
result;
}
return result;
}
public static int fRec(int n) throws
Exception
{
if (n < 0)
{
throw new
Exception ("invalid value: n should be greater than equal to
0");
}
if ( n == 0)
{
return f0;
}
if (n == 1)
{
return f1;
}
return fRec(n-1) + fRec(n-2);
}
public static void main(String[] args) throws
Exception{
int first = 0;
int second = 0;
int n = 0;
try
{
first =
Integer.parseInt(args[0]);
second =
Integer.parseInt(args[1]);
n =
Integer.parseInt(args[2]);
}
catch (Exception e)
{
System.out.println("Please enter integer number only: " + e);
throw e;
}
Fib fib = new Fib(first,
second);`
System.out.println("Printing using
iterative method");
for (int i = 0; i <= n;
i++)
{
System.out.println(f(i));
}
System.out.println("Printing using
recursive method");
for (int i = 0; i <= n;
i++)
{
System.out.println(fRec(i));
}
}
};
/*
Sample run
java 1 1 30
Printing using iterative method
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
Printing using recursive method
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
*/