Question

Step 1: Get OperationsBetween.java Compiling Download the OperationsBetween.java file, and open it in jGrasp (or a...

Step 1: Get OperationsBetween.java Compiling

Download the OperationsBetween.java file, and open it in jGrasp (or a text editor of your choice). This program takes two command-line arguments representing the minimum and maximum number in a numeric range. Currently, the code does not compile. As a first step, you need to get this code to compile. To this end, you'll need to implement the following:

  • Instance variables
  • Constructor
  • A “stub” for the sum method, which will allow the call to sum in the main method to compile. A stub has already been written for the product method; you should write something similar for sum.

Once the code has the above elements and is compiling, you're ready to move on to the next step.

Step 2: Implement the sum Method

In the previous step, you wrote a stub for sum to get the code compiling. Now replace the code in the stub with a loop that will compute the sum between min and max. You will need to use a loop for this purpose, and a for loop is likely the best fit. You should start from min, and then iterate up to max, using a temporary variable that you increment along the way. You will also need another variable to store the result. Comments in the file contain further details.

Once sum (and only sum) is correctly implemented, you should see the following output if run with the command-line arguments 3 4:

Sum: 7
Product: 1

Step 3: Implement the product Method

Replace the code in the stub for product with a loop that will compute the product between min and max. This code should look very similar to the code you wrote for sum. Further details are in the file.

Once both product and sum are correctly implemented, you should see the following output if run with the command-line arguments 5 8:

Sum: 26
Product: 1680

Further example output is shown below for the command-line arguments 3 2 (note that the minimum end of the range is greater than the maximum end of the range):

Sum: 0
Product: 1

Further example output is shown below for the command-line arguments 3 3 (the minimum end of the range is equal to the maximum end of the range):

Sum: 3
Product: 3

__________________________________________________________________________

public class OperationsBetween {
    // You must define the following:
    // 1.) Two private instance variables, min and max
    //
    // 2.) A constructor which takes initial values for
    //     min and max
    //
    // 3.) An instance method named sum, which sums the
    //     values between min and max and returns the
    //     result.  For example, if min = 3 and max = 5,
    //     then this should return 12 (3 + 4 + 5).  If
    //     min is greater than max, then this should
    //     return 0.
    //
    // 4.) An instance method named product, which
    //     multiplies the values between min and max and
    //     returns the result.  For example, if min = 6
    //     and max = 9, then this should return 3024
    //     (6 * 7 * 8 * 9).  If min is greater than
    //     max, then this should return 1.  A stub has
    //     been provided that simply returns 1; this is
    //     just to allow things to compile while you're
    //     working on the sum method.

    public int product() {
        // You'll need to replace this code with something
        // else to make product() work; this is just to make
        // things compile while you work on sum()
        return 1;
    }

    // DO NOT MODIFY main!
    public static void main(String[] args) {
        OperationsBetween between =
            new OperationsBetween(Integer.parseInt(args[0]),
                                  Integer.parseInt(args[1]));
        System.out.println("Sum: " + between.sum());
        System.out.println("Product: " + between.product());
    }
}

Homework Answers

Answer #1

Java Code including required method:

-----------------------------------------------------------------------------------

public class OperationsBetween {
   // You must define the following:
// 1.) Two private instance variables, min and max
   private int min, max;
  
   // 2.) A constructor which takes initial values for
// min and max
   public OperationsBetween(int min, int max)
   {
       this.min = min;
       this.max = max;
   }
  
   // 3.) An instance method named sum, which sums the
// values between min and max and returns the
// result. For example, if min = 3 and max = 5,
// then this should return 12 (3 + 4 + 5). If
// min is greater than max, then this should
// return 0.
   private int sum()
   {
       if(min > max)
           return 0;
      
       int sum=0;
      
       for(int i=min; i<=max; i++)
           sum += i;
      
       return sum;
   }
  
// 4.) An instance method named product, which
// multiplies the values between min and max and
// returns the result. For example, if min = 6
// and max = 9, then this should return 3024
// (6 * 7 * 8 * 9). If min is greater than
// max, then this should return 1. A stub has
// been provided that simply returns 1; this is
// just to allow things to compile while you're
// working on the sum method.
public int product() {
   if(min > max)
           return 1;
      
       int product=1;
      
       for(int i=min; i<=max; i++)
           product *= i;
      
       return product;
}

// DO NOT MODIFY main!
public static void main(String[] args) {
OperationsBetween between = new OperationsBetween(Integer.parseInt("4"), Integer.parseInt("9"));
System.out.println("Sum: " + between.sum());
System.out.println("Product: " + between.product());
}
}

------------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------------------------

Output :

your-directory>javac OperationsBetween.java

your-directory>java OperationsBetween
Sum: 39
Product: 60480

Hope you like it

Any Query? Comment Down!

I have written for you, Please up vote the answer as it encourage us to serve you Best !

Know the answer?
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
Similar Questions
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text editor of your choice). This program will “explode” a String into an array of characters (char[]), and then print out the array. The String comes from the first command-line argument. Example output with the command-line argument foo is below: f o o --------------------------------------------------------------------- public class StringExplode { // TODO - write your code below this comment. // You will need to write a method...
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice)....
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold: Enter first integer: 3 Enter second integer: 4 Enter third integer: 5 Product: 60 More details about the method you need to write are...
All the code below is in the file CatFish.java. Complete the classes below. Keep it very...
All the code below is in the file CatFish.java. Complete the classes below. Keep it very simple. The code you write must produce the output shown at the bottom of the page. Read the whole problem first - how you follow later instructions may affect early choices. Assume you have any import atatements you need. You may not need all blank lines. interface Movers{ public void move(); //this method will print (see output below) } class Cat _____________________________{ _____________________________________ _____________________________________...
Subject: Shell Scripting Practice A File: practice-script-a Create File practice-script-a that: 1. Accepts any number of...
Subject: Shell Scripting Practice A File: practice-script-a Create File practice-script-a that: 1. Accepts any number of userids on the command line 2. For each userid, display the userid, PID, CPU time, and current command for each process owned by that userid Your output should look similar to this example: practice-script-a doug.jones User: doug.jones PID: 8748 TIME: 00:00:00 COMMAND: /usr/lib/systemd/systemd --user User: doug.jones PID: 8749 TIME: 00:00:00 COMMAND: (sd-pam)    User: doug.jones PID: 8750 TIME: 00:00:00 COMMAND: sshd: doug.jones@pts/5 User: doug.jones...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far toString String toString() returns the string "[min,max]" As...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance:...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance: ⦁   A base class called Pet ⦁   A mix-in class called Jumper ⦁   A Dog class and a Cat class that each inherit from Pet and jumper ⦁   Two classes that inherit from Dog: BigDog and SmallDog ⦁   One classes that inherit from Cat: HouseCat The general skeleton of the Pet, Dog, and BigDog classes will be given to you (see below, "Skeleton", but...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....