Question: Task 2 : Test the Recursive Method You are also given a file called TestFib.java, which will help you test the fib 1 method. This

Task 2: Test the Recursive Method
You are also given a file called TestFib.java, which will help you test the fib1 method. This
file asks the user for an integer and calls the fib1 method to compute the corresponding
Fibonacci number.
Test with small numbers, then larger ones. You'll notice the method becomes slow for
larger numbers because it makes many repeated recursive calls.
To understand this, add a print statement in fib1 to display when a call is being made
(e.g.,"In fib(3)" if calculating fib(3)). Run the program again to see how often values are
recalculated.
Notice how fib(5) needs fib(4) and fib(3), and fib(4) also needs fib(3). This repetition
makes it inefficient
import java.util.Scanner;
public class TestFib
{
public static void main(String[] args)
{
int n, fib;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer: ");
n = scan.nextInt();
fib = Fib.fib1(n);
System.out.println("Fib("+ n +") is "+ fib);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!