In: Computer Science
Step 1: Edit SumMinMaxArgs.java
Download the SumMinMaxArgs.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, parses them as ints, and then displays:
If you're unsure how to pass command-line arguments to a program with jGrasp, see this tutorial. Example output of this program with the command-line arguments 1 2 3 4 5 is shown below:
Sum: 15 Min: 1 Max: 5
Further example output for the command-line arguments 87 23 42 91 -4 is shown below:
Sum: 239 Min: -4 Max: 91
_____________________________
public class SumMinMaxArgs {
private int[] array;
// You will need to write the following:
//
// 1. A constructor that takes a reference to an array,
// and initializes an instance variable with this
// array reference
//
// 2. An instance method named sum, which will calculate
// the sum of the elements in the array. If the array
// is empty (contains no elements), then this will
// will return 0. You will need a loop for this.
//
// 3. An instance method named min, which will return
// whichever element in the array is smallest.
// You can assume that min will only be called if the
// array is non-empty (contains at least one element).
// You will need a loop for this. You may use the
// Math.min method here (see link below for more)
// https://www.tutorialspoint.com/java/lang/math_min_int.htm
//
// 4. An instance method named max, which will return
// whichever element in the array is largest.
// You can assume that max will only be called if the
// array is non-empty (contains at least one element).
// You will need a loop for this. You may use the
// Math.max method here (see link below for more)
// https://www.tutorialspoint.com/java/lang/math_min_int.htm
//
// TODO - write your code below
// DO NOT MODIFY parseStrings!
public static int[] parseStrings(String[] strings) {
int[] retval = new int[strings.length];
for (int x = 0; x < strings.length; x++) {
retval[x] = Integer.parseInt(strings[x]);
}
return retval;
}
// DO NOT MODIFY main!
public static void main(String[] args) {
int[] argsAsInts = parseStrings(args);
SumMinMaxArgs obj = new SumMinMaxArgs(argsAsInts);
System.out.println("Sum: " + obj.sum());
System.out.println("Min: " + obj.min());
System.out.println("Max: " + obj.max());
}
}
Below is the Java Solution with output screenshot
Code : SumMinMaxArgs.java
public class SumMinMaxArgs {
private int[] array;
// 1. A constructor that takes a reference to an array,
// and initializes an instance variable with this array reference
public SumMinMaxArgs(int[] array){
this.array = array;
}
// 2. An instance method named sum, which will calculate
// the sum of the elements in the array. If the arra
public int sum(){
int sum = 0;
for(int i=0;i<array.length;i++){
sum += array[i];
}
return sum;
}
// 3. An instance method named min, which will return
// whichever element in the array is smallest.
public int min(){
int min = array[0];
for(int i=0;i<array.length;i++){
if(array[i]<min){
min = array[i];
}
}
return min;
}
// 4. An instance method named max, which will return
// whichever element in the array is largest.
public int max(){
int max = array[0];
for(int i=0;i<array.length;i++){
if(array[i]>max){
max = array[i];
}
}
return max;
}
// DO NOT MODIFY parseStrings!
public static int[] parseStrings(String[] strings) {
int[] retval = new int[strings.length];
for (int x = 0; x < strings.length; x++) {
retval[x] = Integer.parseInt(strings[x]);
}
return retval;
}
// DO NOT MODIFY main!
public static void main(String[] args) {
int[] argsAsInts = parseStrings(args);
SumMinMaxArgs obj = new SumMinMaxArgs(argsAsInts);
System.out.println("Sum: " + obj.sum());
System.out.println("Min: " + obj.min());
System.out.println("Max: " + obj.max());
}
}
Output 1 : (with 1 2 3 4 5 as a command argument)

Output 2 : (with 87 23 42 91 -4 as s command line argument)
