Sheet 02
Sheet 02
Introduction to Assignment
In our previous laboratory exercise, we analyzed the time complexity of a polynomial function
using Excel as our primary tool. In this current experiment, we will implement a Java program
to compute the nth Fibonacci number through both iterative and recursive implementations.
Our objectives include comparing the time and space complexities of these two approaches and
drawing meaningful conclusions about their efficiency trade-offs.
Investigation Steps
Part A: Iterative Implementation
1. Write a program, FibonacciIterative, to compute the nth Fibonacci number using an
iterative approach.
2. Perform a theoretical analysis to determine the Big-O time complexity of this Java
program.
3. Perform a theoretical analysis to determine the Big-O space complexity of this Java
program.
4. Introduce a static variable inside the innermost loop to count the number of iterations
for a given n. Determine its Big-O complexity, providing justification. You may compute
this for individual values of n or modify the program to analyze a range of n values. Use
Excel or any other tool to plot a complexity graph.
5. Modify the program to compute the space complexity. Follow a similar graphing mecha-
nism as item (4).
March 2025
Part B: Recursive Implementation
1. Write a program FibonacciRecursive to compute the nth Fibonacci number using a
recursive approach.
2. Perform a theoretical analysis to determine the Big-O time complexity of this Java
program.
3. Perform a theoretical analysis to determine the Big-O space complexity of this Java
program.
4. Introduce a static variable at the appropriate location in the program to count the number
of recursive calls for a given n. Follow a similar approach as in the iterative case.
5. Modify the program to compute its space complexity and use a similar graphing approach
as in item (4).
Submission Guidelines
Use the IC02.docx template provided on CourseWEB for your submission. Copy and paste the
Excel diagrams and tables into the template, adjusting section sizes as needed. Only one sub-
mission per group is required. Before uploading, rename the file to IC02 ¡Group Number¿.docx
and convert it to PDF before uploading.
Grading Scheme
This assignment accounts for 5% of your final grade. Each subpart is worth 10 marks, distributed
across the following criteria: accuracy of the estimates, methodology, and the explanation
provided.
1. One way to measure the running time of a Java program is by using the Runtime interface
through System.nanoTime() or System.currentTimeMillis(). Modify the programs
to include additional code for measuring execution time. Analyze both the iterative and
recursive cases, graph the results, and compare them with the findings from Parts A and
B. Explain the results and determine whether this method produces the same conclusions.
If not, provide an explanation.
2. There are multiple ways to determine the memory usage of a Java program. One approach
is to use the Runtime.getRuntime() interface. Modify the programs in Sections A and B
to include this runtime interface for measuring memory usage during execution. Graph the
2
results and compare them with the findings from Parts A and B. Explain the results and
assess whether this method yields the same conclusions. If not, provide an explanation.
3. Another method for measuring execution time and memory usage is by using a profiling tool
such as VisualVM. Install VisualVM, conduct the above experiment again, and summarize
your conclusions.