Question: Enhance the Fibonacci program of Fig. 18.5 so that it calculates the approximate amount of time required to perform the calculation and the number of

Enhance the Fibonacci program of Fig. 18.5 so that it calculates the approximate amount of time required to perform the calculation and the number of calls made to the recursive method. For this purpose, call static System method current-TimeMillis, which takes no arguments and returns the computer’s current time in milliseconds.

Call this method twice—once before and once after the call to fibonacci. Save each value and calculate the difference in the times to determine how many milliseconds were required to perform the calculation. Then, add a variable to the Fibonacci Calculator class, and use this variable to determine the number of calls made to method fibonacci. Display your results.

Fig. 18.5I // Fig. 18.5: FibonacciCalculator.java // Recursive fibonacci method. import java.math.BigInteger; 23456780

I // Fig. 18.5: FibonacciCalculator.java // Recursive fibonacci method. import java.math.BigInteger; 23456780 5 public class FibonacciCalculator { 9 10 II 12 13 14 15 16 17 18 19 2222 20 21 22 23 24 25 26 27 } private static BigInteger TWO = BigInteger.value0f (2); ... // recursive declaration of method fibonacci public static BigInteger fibonacci (BigInteger number) { if (number.equals(BigInteger.ZERO) || { // base cases } } } else { // recursion step return fibonacci (number.subtract (BigInteger.ONE)) .add( fibonacci (number.subtract (TWO))); } number. equals(BigInteger.ONE)) return number; public static void main(String[] args) { // displays the fibonacci values from 0-40 for (int counter = 0; counter

Step by Step Solution

3.41 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To modify the existing Fibonacci program to include the calculation of how much time it takes to ... View full answer

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 Java How To Program Late Objects Questions!