Task #1 Tracing Recursive Methods 1. Copy the file Recursion.java (see Code Listing 16.1) from the Student Files or as directed by your instructor. 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); Copyright © 2019 Pearson Education, Inc., Hoboken NJ 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".

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Modularization Techniques
Section: Chapter Questions
Problem 13RQ
icon
Related questions
Question
Code Listing 15.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 ();
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
/**
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);
}
Transcribed Image Text:Code Listing 15.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 (); Copyright © 2019 Pearson Education, Inc., Hoboken NJ /** 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); }
Task #1 Tracing Recursive Methods
1. Copy the file Recursion.java (see Code Listing 16.1) from the Student Files or
as directed by your instructor.
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);
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
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".
Transcribed Image Text:Task #1 Tracing Recursive Methods 1. Copy the file Recursion.java (see Code Listing 16.1) from the Student Files or as directed by your instructor. 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); Copyright © 2019 Pearson Education, Inc., Hoboken NJ 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".
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage