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:
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()); } }
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 !
Get Answers For Free
Most questions answered within 1 hours.