(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.

image

Complete method arraySum below.

public static int arraySum(int[] arr) {
    int sum = 0;
    
    for (int i = 0; i < arr.length; i++) {  // iterate through each num in array
        sum += arr[i];
    }
    return sum;
}
int[] testArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // array from 1-10 
int sum = arraySum(testArray); // testing my code :D
System.out.println("Array Sum: " + sum);
Array Sum: 55

(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}.

image

image

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.

public static int[] rowSums(int[][] arr2D) {
    int rowCount = arr2D.length; // number of rows in the array
    int[] sums = new int[rowCount]; // array to store row sums

    for (int i = 0; i < rowCount; i++) { // iterate through each row of the 2D array
        int sum = 0;
        for (int j = 0; j < arr2D[i].length; j++) { // iterate through each element in the current row
            sum += arr2D[i][j]; // add current element to the sum
        }
        sums[i] = sum; // store the sum of the current row
    }

    return sums;
}
int[][] mat1 = { // using array from College Board
    {1, 3, 2, 7, 3},
    {10, 10, 4, 6, 2},
    {5, 3, 5, 9, 6},
    {7, 6, 4, 2, 1}
};

// testing my code :D
int[] sums = rowSums(mat1);
System.out.println("Row sums:");
for (int sum : sums) {
    System.out.println(sum);
}
Row sums:
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.

image

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.

image

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.

public static boolean isDiverse(int[][] arr2D) { 
    Set<Integer> set = new HashSet<>(); // set to store unique row sums
    int[] rowSums = rowSums(arr2D); // calculate row sums
    for (int sum : rowSums) {
        if (!set.add(sum)) { // if sum not added to set, not unique
            return false; 
        }
    }
    return true; // if sum added to set, is unique
}
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 is...");
System.out.println("diverse? " + isDiverse(mat1)); // should be true for mat1
System.out.println("mat2 is...");
System.out.println("diverse? " + isDiverse(mat2)); // should be true for mat2

mat1 is...
diverse? true
mat2 is...
diverse? false