Question

Write a class called MathProgressions which has a main method (NOPE! NO DRIVERS THIS WEEK), and two methods which will calculate the geometric and harmonic progressions via a recursive method.

The class MathProgressions needs to have the following:

  • Make sure to import java.util.*;
  • Static Methods
    • Main method which handles all of the input and output. The user is asked to enter a number and then for that number it gives both the geometric and harmonic progressions for every number up until (and including) that number.
    • geometric: Is a method which takes in a number and then returns that value of the number in the geometric sequence. The geometric sequence is represented by:

geometric(n) = n × 11

Which mean it?s the product of all of the numbers up until that number.

  • harmonic: Is a method which takes a number and then returns the value of the number in the harmonic sequence. The harmonic sequence is represented by:

Which means the product of the inverse of the first n numbers.

  • NOTE: MAKE SURE EACH OF THESE ARE SOLVED RECURSIVELY AND NOT WITH A LOOP OR ELSE -79 FOR YOU!!!
  • NOTE: YOU MAY USE A LOOP TO PRINT OUT THE PROGRESSION BUT NOT FOR THE CALCULATION

Example Dialog:

Enter a number and I'll give you the geometric and harmonic sequences

10

Geometric is:

1=1.0 2=2.0 3=6.0 4=24.0 5=120.0 6=720.0 7=5040.0 8=40320.0 9=362880.0 10=3628800.0

Harmonic is:

1=1.0 2=0.5 3=0.16666666666666666 4=0.041666666666666664 5=0.008333333333333333 6=0.0013888888888888887 7=1.9841269841269839E-4 8=2.4801587301587298E-5 9=2.7557319223985884E-6 10=2.7557319223985883E-7

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

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 ");
    System.out.println(
      "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();
    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);
                
    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);
        }
        
        public static int geometricRecursive (int n)
        {
                if (n == 0)
                {
                        return 1;
                }
                else
                {
                        return (geometricRecursive(n-1) * n);
                }
        }
        
        public static int geometricIterative (int n)
        {
                int product = 1;
                for (int index = 1; index <= n; index++)
                {
                        product = product * index;
                }
                return product;
        }
        
        public static double harmonicRecursive (int n)
        {
                        if (n == 0)
                {
                        return 1;
                }
                else
                {
                        return (harmonicRecursive(n-1) / (double)n);
                }
        }
        
        public static double harmonicIterative (int n)
        {
                double product = 1;
                for (int index = 1; index <= n; index++)
                {
                        product = product /(double)index;
                }
                return product;
        }
        
}
Add a comment
Answer #2
import java.util.Scanner;

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 ");
    System.out.println(
      "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();
    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);
                
    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);
        }
        
        public static int geometricRecursive (int n)
        {
                if (n == 0)
                {
                        return 1;
                }
                else
                {
                        return (geometricRecursive(n-1) * n);
                }
        }
        
        public static int geometricIterative (int n)
        {
                int product = 1;
                for (int index = 1; index <= n; index++)
                {
                        product = product * index;
                }
                return product;
        }
        
        public static double harmonicRecursive (int n)
        {
                        if (n == 0)
                {
                        return 1;
                }
                else
                {
                        return (harmonicRecursive(n-1) / (double)n);
                }
        }
        
        public static double harmonicIterative (int n)
        {
                double product = 1;
                for (int index = 1; index <= n; index++)
                {
                        product = product /(double)index;
                }
                return product;
        }
        
}
Add a comment
Know the answer?
Add Answer to:
Write a class called MathProgressions which has a main method (NOPE! NO DRIVERS THIS WEEK), and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent...

    LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent (2 pts). For example, integerPower(3, 4) returns 81. Assume thatexponent is a positive nonzero integer, and base is an integer. Method integer should use for or while loop to control the calculation. Do not use any Math library methods. Incorporate this method into a program class and invoke this method with different combinations of input values at least 4 times. Please use printf() method...

  • Write a class called Primes. This class should contain a constructor and a method called next_prime....

    Write a class called Primes. This class should contain a constructor and a method called next_prime. The next_prime method returns the next prime number. For example: >>> n = int(input('How many prime numbers?\n')) >>> p = primes() >>> prime_numbers = [ ] >>> for i in range(n): >>> prime = p.next_prime() >>> primes_numbers.append(prime) >>> print(prime_numbers) If the user types 5 in response to the prompt, then the output should be [2, 3, 5, 7, 11]. python 2.7.13

  • Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array...

    Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array of Integer and has 2 parameters: arraySize of type int and numberOfDigits of type int. This method should create an array of the specified size that is filled with random numbers. Each random numbers should have the same number of digits as specified in the second parameter In your main method (or additional private methods) do the following: Execute selection sort on quick sort...

  • Create a class named HW that has the main method. Leave the main method empty for...

    Create a class named HW that has the main method. Leave the main method empty for now. • Create a method named allPairs that takes an integer array parameter named a that contains only positive integers and does not return anything. The method should print out all pairs of numbers that have a sum that is prime. If there are no pairs that have a sum that is prime, print out "No pairs". Note that in the first example, (2,...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Implement a Java class that is called RainFall that uses a double array to store the...

    Implement a Java class that is called RainFall that uses a double array to store the amount of rainfall for each month of the year. The class should have only one instance variable of type double[]. Implement the following methods in the RainFall class: 1. A constructor that creates and initializes all entries in the array to be -1. 2. toString: a method that returns a one-line String representation of the object that includes 12 double numbers that represent the...

  • Create a class called Lab7b and in it implement all of the methods below. Also, write...

    Create a class called Lab7b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end; int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array...

  • Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a...

    Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a number of properties about an array of doubles. There should be separate static methods to calculate the min, the max, the mean, the median, the standard deviation (make sure to make use of your mean method to implement this), and the mode. For the mode, you can assume that the data set is not multimodal. This class should not have a main method. Write...

  • Write code in static void main method class, which creates a boolean array called flags, size...

    Write code in static void main method class, which creates a boolean array called flags, size 10, and using a for loop sets each element to alternating values (true at index zero, false at index one Write code in a static void main method class, which creates a float array called sales, size 5, uses an initializer list to instantiate its elements with positive values having two decimal places (example 7.25), and using a for loop reads those values of...

  • Create a class called Play that has an InputReader as an instance variable. Be sure to...

    Create a class called Play that has an InputReader as an instance variable. Be sure to initialize it in the constructor. The class has two methods. Write a method with this signature: Write a method with this signature: public void stringPlay() The method prompts the user for a string, reads it in, and then displays the string as many times as the length of that string. The output string should be formatted with the first letter uppercase and the rest...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT