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 Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
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 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
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 Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT