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) ; "1 Copyright © 2019 Pearson Education, Inc., Hoboken NJ
Code 16-1
/**
This class has a recursive method.
*/
public class EndlessRecursion {
public static void message() {
System.out.println("This is a recursive method.");
message();
}
}
Code 16.2
/**
This class has a recursive method message,
which displays a message n times.
*/
public class Recursive {
public static void messge (int n) {
if (n>0) {
System.out.println (" This is a recursive method.");
message(n-1);
}
}
}
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
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();
/** 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); } } } Code Listing 15.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();
![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
"1
+
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".
Code Listing 15 1 (Doaunais
+
+
Task #2 Writing Recursive and Iterative Versions of a Method
1. Copy the file Progression.java (see code listing 16.2) from the Student Files or
as directed by your instructor.
inval
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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ff81bba9f-d161-440a-a9b5-0173500310be%2Fa960b708-bcb0-4ae4-8c7b-c8429f9a7909%2Fj07dusn_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)