Question: CSIT 212 Data Structures Assignment Dr. H. Johnson Example 1a: FactorialCalculation.java //The Recursive Factorial Method public class FactorialCalculation { //recursive Factorial method public long factorial(long
CSIT 212 Data Structures Assignment Dr. H. Johnson
Example 1a: FactorialCalculation.java
//The Recursive Factorial Method
public class FactorialCalculation { //recursive Factorial method
public long factorial(long number) { if (number <= 1) return 1; else return number * factorial (number 1); } //Now output the factorials of 0 through 15 public void displayFactorials() { // Calculate the factorial of o through 10
for ( int counter = 0; counter <= 10; counter++ ) System.out.print ( %d! = %d , counter, factorial (counter )); } // end of the method displayFactorials } // end of class FactorialCalculation
Example 1b: FactorialTest.java // Testing the recursive factorial method
public class FactorialTest { // calculate the factorial of 0 10
Public static void main ( String args[] ) { factorialCalculation factorialCalculation = new FactorialCalculation(); factorialCalculation.displayFactorials(); } // end of main
} // end of the class FactorialTest
Recursive and Iterative Activities:
1. Use a java editor to write and execute the example given above. Copy your source code as well as the output as an image to a word processor.
2. Write another method that uses loop, instead of recursion, to calculate the factorial of the previous example.
3. Use the time method (Java.System.currentTimeMillis()) to compare execution times for the two methods ((i) the recursive version and (ii) the loop version) when calculating 15! 30! 50! 90!
4. Draw a graph that shows the difference in time for the method for values in part 3.
Note:
In the code given above, if you encounter issues with the type long integer, try big integer instead.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
