Question
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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started