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); } } } 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(); Copyright © 2019 Pearson Education, Inc., Hoboken NJ // 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
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);
}
} }
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();
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
// 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 geometricIterative method
// Write the harmonicRecursive method
// Write the harmonicIterative method
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images