Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions 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);
}
expand button
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".
expand button
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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education