Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Make Uml Diagram for code below /** * The FibonacciTimer program calculates the fibonacci number from input of user, plus five * It calculates using

Make Uml Diagram for code below

/** * The FibonacciTimer program calculates the fibonacci number from input of user, plus five * It calculates using recursion * * @author George clony * @version 1.0 * @since 2012-4-5 */

import java.util.Scanner;//import scanner class

public class FibonacciTimer

{

public static void main (String [] arg)

{

System.out.print("Enter a positive integer: ");

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();// Take value of in from user

//initializes variables long currentTime = System.currentTimeMillis();//start the time

long prevTime;

long elapsedTime= 0;

for (int k = 0; k <= 5; k++)//loop to five, which will be added to user number

{

prevTime = currentTime;

System.out.print("The Fibonacci term at position ");

System.out.print((num + k) + " is ");//find and print fibonacci number

System.out.println(fib(num + k));

currentTime = System.currentTimeMillis();

elapsedTime= (currentTime - prevTime) / 1000;//convert time it took into seconds

System.out.println("Computed in "+ elapsedTime + " seconds. ");//display the time it took

}

}

/** * calculates nth fibonacci number * @param n * @return nth fibonacci number */

public static long fib(long n)

{

long fib[] = new long[(int) (n+1)];// uses this array to calculate fibonacci number in linear time. // if recursion is used it takes exponential time. fib[0] = 0;

fib[1] = 1;

for(int i=2;i<=(int)n;++i){//iterate from 2 to n

fib[i] = fib[i-1] + fib[i-2];// calculate fibonacci numbers using last two fibonacci values and store the result in array

}

return fib[(int)n];// return nth fibonacci value calculated.

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

More Books

Students also viewed these Databases questions

Question

25.0 m C B A 52.0 m 65.0 m

Answered: 1 week ago