Question: Task #1 Tracing Recursive Methods 1.Recursion.java 2. Run the program to confirm that the generated answer is correct. Modify the factorial method in the following

Task #1 Tracing Recursive Methods

1.Recursion.java

2. Run the program to confirm that the generated answer is correct. Modify the

factorial method in the following ways:

a. Add these lines above the first if statement int temp;

System.out.println("Method call --" +"calculating " +"Factorial of: " + n);

b. Remove this line in the recursive section at the end of the method:

return (factorial(n-1) *n);

c. Add these lines in the recursive section :temp = factorial(n-1);

System.out.println("Factorial of: " +(n-1) + " is " +temp);

return (temp * n);

3. Rerun the program and note how the recursive calls are built up on the run -time

stack and then the values are calculated in reverse order as the run-time stack unwinds.

Task #2

Writing Recursive and Iterative Versions of a Method

1.rogression.java

2. You need to write class (static ) methods for an iterative and a recursive version of each of the progressions.

You will create the following methods:

a. geometricRecursive

b. geometricIterative

c. harmonicRecursive

d. harmonicIterative

.Be sure to match these methods to the method calls in the main method.

.1 (Recursive.java)

/**

This program demonstrates factorials using recursion.

*/

public class Recursion

{

public static void main(String[] args)

{

int n = 7;

// Test out the factorial

System.out.println(n + " factorial equals ");

System.out.println(Recursion.factorial(n));

System.out.println();

}

/**

This is the factorial method.

@param n A number.

@return The factorial of n.

*/

public static int factorial(int n)

{

int temp;

if (n == 0)

{

return 1;

}

else

{

return (factorial(n-1) * n);

}

}

}

.2 (Progression.java)

import java.util.Scanner;

/**

This program calculates the geometric and harmonic progression for a number entered by the user.

*/

public class Progression

{

public static void main(String [] args)

{

Scanner keyboard = new Scanner (System.in);

System.out.println("This program will calculate " + "the geometric and harmonic " + "progression for the number " +

"you enter.");

System.out.print("Enter an integer that is " + "greater than or equal to 1: ");

int input = keyboard.nextInt();

// Match the method calls with the methods you write

int geomAnswer = geometricRecursive(input);

double harmAnswer = harmonicRecursive(input);

System.out.println("Using recursion:");

System.out.println("The geometric progression of " + input + " is " + geomAnswer);

System.out.println("The harmonic progression of " + input + " is " + harmAnswer);

// Match the method calls with the methods you write geomAnswer = geometricIterative(input);

harmAnswer = harmonicIterative(input);

System.out.println("Using iteration:");

System.out.println("The geometric progression of " + input + " is " + geomAnswer);

System.out.println("The harmonic progression of " + input + " is " + harmAnswer);

}

// ADD LINES FOR TASK #2 HERE

// Write the geometricRecursive method

// Write the geo

metricIterative method

// Write the harmonicRecursive method

// Write the harmonicIterative method

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!