Description
Exercise 1 – Iterative vs. recursive methods
1. Create a new project called Lab9 and copy the given .java files into the src folder of this lab project folder. Refresh the project in Eclipse to show the provided files there.
2. Open FibonacciDemo.java and FactorialDemo.java and examine the code. Carefully read through the ifib() and rfib() methods in FibonacciDemo and ifact() and rfact() in FactorialDemo. Compare the iterative and recursive approaches in both classes. Why do the iterative methods require so many variables? What are the base cases of the recursive methods?
3. Run the program and when asked for numbers, enter5,10,15, and 20 . Write down the running times reported by the algorithm. Experiment with other numbers too but note that Factorial will not work for numbers over 25 as the long type will not be able to hold numbers larger than that.
4. In FibonacciDemo, modify the code to include a new variable called methodCalls2 that keeps track of the number of calls made to methodrfib()when the value of the parameternis 2 and prints it out in the main() method. Run the program and enter the value 40 . How many calls are made torfib()when the value of the parametern=2? Write down this result.
5. Why is the recursive algorithm for computing Fibonacci numbers so slow compared to the iterative algorithm? Does the value of methodCalls2 make sense with this rationale?
Darren Jacob –
Awesome work as usual!!
Ed Selee –
great work as usual thanks
Carmen Hoggard –
Work was finished long before due and tutor was considerate and engaging. Perfect!