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 _____________________________{ _____________________________________ _____________________________________...
● Write code to read the content of the text file input.txt using JAVA. For each...
● Write code to read the content of the text file input.txt using JAVA. For each line in input.txt, write a new line in the new text file output.txt that computes the answer to some operation on a list of numbers. ● If the input.txt has the following: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 Your program should generate output.txt as follows: The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is...
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...
Networking questions: 1. You need to perform a basic ping scan and write the output file...
Networking questions: 1. You need to perform a basic ping scan and write the output file to a text file named 10-PingSweep.txt. You must utilize a “for loop” with the ping command in windows. It should be for the following sweep – 192.168.111.1-254. Provide the command to be placed into a cmd.exe shell. (Note – only valid Reply responses should be in the text file. Your exact command will be tested.) 2. You are working on a potential incident response...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth root of x is the number when raised to the power n gives x. In particular, please fill in the method findNthRoot(int number, int n, int precision) within the Main class. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should still fill in the answer with decimal...
Create an m-file myidftloop.m that takes as input a column vector Fr of DFT co- efficients...
Create an m-file myidftloop.m that takes as input a column vector Fr of DFT co- efficients and produces as output the corresponding column vector fk of samples, according to the formula 1 N0−1 2πrk fk= ?FrejN0 , k=0,1,...,N0−1. Again, your code should automatically determine N0 based on the length of the input vector Fr. To compute the inverse DFT, you should use a nested loop. Hand in your m-file and make sure it is clearly documented. Validate your m-file by...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT