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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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);
      }
} }
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

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.printin ("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
Transcribed Image Text: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.printin ("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
Task #2 Writing Recursive and Iterative Versions of a Method
1. Copy the file Progression.java (sec code listing 16.2) from the Student Files or
as directed by your instructor.
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.
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:Task #2 Writing Recursive and Iterative Versions of a Method 1. Copy the file Progression.java (sec code listing 16.2) from the Student Files or as directed by your instructor. 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. 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);
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY