Question

In: Computer Science

public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...

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

Solutions

Expert Solution

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());
    }
}

Related Solutions

public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m = new int[x][y]; } public Matrix(int x, int y, int z) { m = new int[x][y]; for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { m[i][j] = z; } } } public int rowsum(int i) throws IndexOutOfBoundsException { if (i < 0 || i > m.length-1) { throw new IndexOutOfBoundsException("Invalid Row"); } int sum =...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT