In: Computer Science
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());
}
}
Sum: 15 Min: 1 Max: 5
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class SumMinMaxArgsTest {
@Test
public void testParseStringsLength0() {
assertArrayEquals(SumMinMaxArgs.parseStrings(new String[0]),
new int[0]);
}
@Test
public void testParseStringsLength1() {
assertArrayEquals(SumMinMaxArgs.parseStrings(new String[]{"1"}),
new int[]{1});
}
@Test
public void testParseStringsLength2() {
assertArrayEquals(SumMinMaxArgs.parseStrings(new String[]{"1", "42"}),
new int[]{1, 42});
}
@Test
public void testSumArrayLength0() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[0]);
assertEquals(0, obj.sum());
}
@Test
public void testSumArrayLength1() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{1});
assertEquals(1, obj.sum());
}
@Test
public void testSumArrayLength2() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{1, 2});
assertEquals(3, obj.sum());
}
@Test
public void testSumArrayLength3() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{1, 2, 3});
assertEquals(6, obj.sum());
}
@Test
public void testSumArrayLength4() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{1, 2, 3, 4});
assertEquals(10, obj.sum());
}
@Test
public void testMinArrayLength1() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{1});
assertEquals(1, obj.min());
}
@Test
public void testMinArrayLength2MinFirst() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{1, 2});
assertEquals(1, obj.min());
}
@Test
public void testMinArrayLength2MinSecond() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{2, 1});
assertEquals(1, obj.min());
}
@Test
public void testMinArrayLength3MinFirst() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{1, 2, 3});
assertEquals(1, obj.min());
}
@Test
public void testMinArrayLength3MinSecond() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{2, 1, 3});
assertEquals(1, obj.min());
}
@Test
public void testMinArrayLength3MinThird() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{3, 2, 1});
assertEquals(1, obj.min());
}
@Test
public void testMaxArrayLength1() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{1});
assertEquals(1, obj.max());
}
@Test
public void testMaxArrayLength2MaxFirst() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{2, 1});
assertEquals(2, obj.max());
}
@Test
public void testMaxArrayLength2MaxSecond() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{1, 2});
assertEquals(2, obj.max());
}
@Test
public void testMaxArrayLength3MaxFirst() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{3, 2, 1});
assertEquals(3, obj.max());
}
@Test
public void testMinArrayLength3MaxSecond() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{2, 3, 1});
assertEquals(3, obj.max());
}
@Test
public void testMaxArrayLength3MaxThird() {
SumMinMaxArgs obj = new SumMinMaxArgs(new int[]{2, 1, 3});
assertEquals(3, obj.max());
}
} // SumMinMaxArgsTest
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
// 1. Constructor as required.
public SumMinMaxArgs(int[] a) {
array = a;
}
// 2. sum method returning the sum of the array elements.
public int sum() {
int sumArray = 0;
for(int i=0; i<array.length; i++) {
sumArray += array[i];
}
return sumArray;
}
// 3. min method returning the minimum of the array elements.
public int min() {
int minArray = array[0];
for(int i=1; i<array.length; i++) {
if(array[i]<minArray)
minArray = array[i];
}
return minArray;
}
// 4. max method returning the maximum of the array elements.
public int max() {
int maxArray = array[0];
for(int i=1; i<array.length; i++) {
if(array[i]>maxArray)
maxArray = array[i];
}
return maxArray;
}
// 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());
}
}