Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task #2 Writing Recursive and Iterative Versions of a Method 1. Copy the file Pr

ID: 3912716 • Letter: T

Question

Task #2 Writing Recursive and Iterative Versions of a Method

1. Copy the file Progression.java (see code listing 16.2) from the Student CD 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 16.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 © 2016 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 }

Explanation / Answer

import java.util.Scanner;

/**    This program calculates the geometric and    harmonic progression for a number entered    by the user. */

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();


      // 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  
   static int geometricRecursive(int n)
   {
   if (n == 1)
   return 1;
   else
   return n*geometricRecursive(n-1); // 1*2*3*4.....*n
  
   }
  
   // Write the geometricIterative method  
   static int geometricIterative(int n)
   {
   int result = 1;
   for(int i=1;i<=n;i++)
   {
     result = result *i; // 1*2*3*4.....*n
   }
   return result;
   }
  
   // Write the harmonicRecursive method
   static double harmonicRecursive(int n)
   {
   if (n== 1)
   return 1;
   else
   return (double)1/n+harmonicRecursive(n-1); // 1/1+1/2+1/3+1/4......+1/n
   }
  
   // Write the harmonicIterative method
   static double harmonicIterative(int n)
   {
   double result = 0;
   for(int i=1;i<=n;i++)
   {
     result = result + (double) 1 /i; // 1/1+1/2+1/3+1/4......+1/n
   }
   return result;
  
   }
  
  

}

Output:

This program will calculate the geometric and harmonic progression for the number you enter.
Enter an integer that is greater than or equal to 1: Using recursion:
The geometric progression of 5 is 120
The harmonic progression of 5 is 2.283333333333333
Using iteration:
The geometric progression of 5 is 120
The harmonic progression of 5 is 2.283333333333333

Do ask if any doubt. Please upvote.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote