Question 1

  • Assume that the classes listed in the Java Quick Reference have been imported where appropriate.

  • Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.

  • In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.

This question involves reasoning about one-dimensional and two-dimensional arrays of integers. You will write three static methods, all of which are in a single enclosing class, named DiverseArray (not shown). The first method returns the sum of the values of a one-dimensional array; the second method returns an array that represents the sums of the rows of a two-dimensional array; and the third method analyzes row sums.


(a) Write a static method arraySum that calculates and returns the sum of the entries in a specified one-dimensional array. The following example shows an array arr1 and the value returned by a call to arraySum.

Screenshot 2024-02-22 at 12 05 51 PM

Complete method arraySum below.

/ * * Returns the sum of the entries in the one-dimensional array arr.

  • /

public static int arraySum (int [ ] arr)

public class DiverseArray {
    public static int arraySum(int[] arr) {
        int sum = 0;
        // iterate through elements in the array
        for (int currentValue : arr) {
            // adding the current element to sum
            sum += currentValue;
        }
        return sum;
    }
    
    public static void main(String[] args) { // calls the arraySum function
        // testing the method
        int[] arr1 = {1, 3, 2, 7, 3}; // defining the integer array 
        System.out.println("array sum : " + arraySum(arr1));
    }
}

DiverseArray.main(null); 
array sum : 16

(b) Write a static method rowSums that calculates the sums of each of the rows in a given two-dimensional array and returns these sums in a one-dimensional array. The method has one parameter, a two-dimensional array arr2D of int values. The array is in row-major order: arr2D [ r ] [ c ] is the entry at row r and column c. The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. As a reminder, each row of a two-dimensional array is a one-dimensional array.

For example, if mat1 is the array represented by the following table, the call rowSums(mat1) returns the array {16, 32, 28, 20}.

Screenshot 2024-02-22 at 12 06 26 PM

Assume that arraySum works as specified, regardless of what you wrote in part (a). You must use arraySum appropriately to receive full credit.

Complete method rowSums below.

/ * * Returns a one-dimensional array in which the entry at index k is the sum of

  • the entries of row k of the two-dimensional array arr2D.

  • /

public static int [ ] rowSums(int [ ] [ ] arr2D)

public class DiverseArray1 {

    public static int arraySum(int[] arr) {
        int sum = 0; // initializing the variable to store the sum of the array elements
        // itereate through each element
        for (int currentValue : arr) {
            // add current elemnt to sum
            sum += currentValue;
        }
        return sum;
    }
    
    public static int[] rowSums(int[][] arr2D) {
        int[] result = new int[arr2D.length]; // initializing array to store sum of row 
        // iterating though rows in 2d array
        for (int k = 0; k < arr2D.length; k++) {
            // finding sum via method and storing as result
            result[k] = arraySum(arr2D[k]); 
        }
        return result;
    }

    public static void main(String[] args) {
       // define 2d array
        int[][] mat1 = {
            {1, 3, 2, 7,3},
            {10, 10, 4, 6, 2},
            {5, 3, 5, 9, 6},
            {7, 6, 4, 2, 1}
        };
        // computing using rowsum method
        int[] sums = rowSums(mat1);
        System.out.print("sum of mat1 : ");
        for (int sum : sums) {
            System.out.print(sum + " ");
        }
        System.out.println();
}
}

DiverseArray1.main(null);
sum of mat1 : 16 32 28 20 

(c) A two-dimensional array is diverse if no two of its rows have entries that sum to the same value. In the following examples, the array mat1 is diverse because each row sum is different, but the array mat2 is not diverse because the first and last rows have the same sum.

Screenshot 2024-02-22 at 12 07 00 PM

Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse. The method has one parameter: a two-dimensional array arr2D of int values. The method should return true if all the row sums in the given array are unique; otherwise, it should return false. In the arrays shown above, the call isDiverse (mat1) returns true and the call isDiverse(mat2) returns false.

Screenshot 2024-02-22 at 12 07 25 PM

Assume that arraySum and rowSums work as specified, regardless of what you wrote in parts (a) and(b). You must use rowSums appropriately to receive full credit. Complete method isDiverse below.

/ * * Returns true if all rows in arr2D have different row sums;

  • false otherwise.

  • /public static boolean isDiverse(int [ ] [ ] arr2D)

public class DiverseArray2 {

    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int currentValue : arr) {
            sum += currentValue;
        }
        return sum;
    }

    public static int[] rowSums(int[][] arr2D) {
        int[] result = new int[arr2D.length];
        for (int k = 0; k < arr2D.length; k++) {
            result[k] = arraySum(arr2D[k]);
        }
        return result;
    }

    // diverse determination
    public static boolean isDiverse(int[][] arr2D) {
        int[] rowSumsArray = rowSums(arr2D);
        for (int i = 0; i < rowSumsArray.length; i++) { // comparing pair of row sums, checkign duplicates
            for (int j = i + 1; j < rowSumsArray.length; j++) {
                if (rowSumsArray[i] == rowSumsArray[j])
                    return false; // if any 2 row sums are the same return false
            }
        }
        return true;// none are equal return true
    }

    public static void main(String[] args) {
        int[][] mat1 = {
            {1, 3, 2, 7,3},
            {10, 10, 4, 6, 2},
            {5, 3, 5, 9, 6},
            {7, 6, 4, 2, 1}
        };

        int[][] mat2 = {
            {1, 1, 5, 3, 4},
            {12, 7, 6, 1, 9},
            {8, 11, 10, 2, 5},
            {3, 2, 3, 0, 6}
        };

        System.out.println("mat1 : " + isDiverse(mat1)); // returns true
        System.out.println("mat2 : " + isDiverse(mat2)); // returns false
    }
}

// Run the main method
DiverseArray2.main(null);
mat1 : true
mat2 : false

Notes