Question: 1. Write a Java program that calculates and displays the Fibonacci series, defined by the recursive formula F(n) = F(n-1) + F(n-2). F(0) and F(1)
1.
Write a Java program that calculates and displays the Fibonacci
series, 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 {
// constructor
public 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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
