Question

In: Computer Science

1Write a Java program that calculates and displays the Fibonacciseries, defined by the recursive formula F(n)...

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.

Solutions

Expert Solution

// 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

*/


Related Solutions

Write the first four terms of the sequence defined by the recursive formula a1 = 2, an = an − 1 + n.
Write the first four terms of the sequence defined by the recursive formula a1 = 2, an = an − 1 + n.
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
Write a program named MakeChange that calculates and displays the conversion of an entered number of...
Write a program named MakeChange that calculates and displays the conversion of an entered number of dollars into currency denominations—twenties, tens, fives, and ones. For example, if 113 dollars is entered, the output would be twenties: 5 tens: 1 fives: 0 ones: 3. Answer in C# (P.S i'm posting the incomplete code that i have so far below.) using System; using static System.Console; class MakeChange { static void Main() { int twenties, tens, fives, ones; WriteLine("Enter the number of dollars:");...
a.Create the logic for a program using pseudocode that calculates and displays the amount of money...
a.Create the logic for a program using pseudocode that calculates and displays the amount of money you would have if you invested $5000 at 2 percent simple interest for one year. Create a separate method to do the calculation and return the result to be displayed. b.Modify the program in Exercise 2a so that the main program prompts the user for the amount of money and passes it to the interest-calculating method. c.Modify the program in Exercise 2b so that...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2)...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2) with f(0) = 0 and f(1) = 1. Find f(54) by a program or maually. Note that this number must be positive and f(53) = 53.......73 (starting with 53 and ending with 73). I must admit that my three machines including a desktop are unable to find f(54) and they quit during computation. The answer is f(54) = 86267571272 */ The Java code: public...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
DESIGH A FLOWCHART IN FLOWGORITHM Speeding Violation Calculator. Design a program that calculates and displays the...
DESIGH A FLOWCHART IN FLOWGORITHM Speeding Violation Calculator. Design a program that calculates and displays the number of miles per hour over the speed limit that a speeding driver was doing. The program should ask for the speed limit and the driver's speed. Validate the input as follows: The speed limit should be at least 20, but not greater than 70. The driver's speed should be at least the value entered for the speed limit (otherwise the driver was not...
Let f : N → N and g : N → N be the functions defined...
Let f : N → N and g : N → N be the functions defined as ∀k ∈ N f(k) = 2k and g(k) = (k/2 if k is even, (k + 1) /2 if k is odd). (1) Are the functions f and g injective? surjective? bijective? Justify your answers. (2) Give the expressions of the functions g ◦ f and f ◦ g? (3) Are the functions g ◦ f and f ◦ g injective? surjective? bijective?...
For f: N x N -> N defined by f(m,n) = 2m-1(2n-1) a) Prove: f is...
For f: N x N -> N defined by f(m,n) = 2m-1(2n-1) a) Prove: f is 1-to-1 b) Prove: f is onto c) Prove {1, 2} x N is countable
Let f : Z × Z → Z be defined by f(n, m) = n −...
Let f : Z × Z → Z be defined by f(n, m) = n − m a. Is this function one to one? Prove your result. b. Is this function onto Z? Prove your result
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT