Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Notes:

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.

A table is shown with two columns, arr 1 on the left and Value returned by arraySum open parentheses arr 1 close parentheses. In the left column an array is shown. It is a horizontal row of 5 boxes with the numbers 0 through 4 above the boxes from left to right. The boxes read 1, 3, 2, 7, and 3. In the right column is the value 16.

Complete method arraySum below.

/**
 * Returns the sum of the entries in the one-dimensional array arr.
 */
public static int arraySum(int[] arr) {
    // Method implementation here
}

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

A table titled mat1 is shown with five columns and four rows. From the upper left the columns are labeled 0 through 4 and the rows 0 through 3. The first row reads 1, 3, 2, 7, 3. The second row reads 10, 10, 4, 6, 2. The third row reads 5, 3, 5, 9, 6. The fourth row reads 7, 6, 4, 2, 1.

A box with text at the top that reads, Methods written in this question. A 3-line code segment reads as follows. Line 1: public static int array Sum, open parenthesis, int, open square bracket, close square bracket, arr, close parenthesis. Line 2: public static int, open square bracket, close square bracket, row Sums, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, arr 2 D, close parenthesis. Line 3: public static boolean is Diverse, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, arr 2 D, open parenthesis.

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) {
    // Method implementation here
}

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

A table titled mat1 is shown with five columns and four rows. From the upper left the columns are labeled 0 through 4 and the rows 0 through 3. The first row reads 1, 3, 2, 7, 3. The second row reads 10, 10, 4, 6, 2. The third row reads 5, 3, 5, 9, 6. The fourth row reads 7, 6, 4, 2, 1. To the right of the table is a column with the title Row sums, with an entry lined up with each row of the table. The entries in the column are 16, 32, 28, and 20. Below the first table, table titled mat1 is shown with five columns and four rows. From the upper left the columns are labeled 0 through 4 and the rows 0 through 3. The first row reads 1, 1, 5, 3, 4. The second row reads 12, 7, 6, 1, 9. The third row reads 8, 11, 10, 2, 5 The fourth row reads 3, 2, 3, 0, 6. To the right of the table is a column with the title Row sums, with an entry lined up with each row of the table. The entries in the column are 14, 35, 36, and 14. 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.

A box with text at the top that reads, Methods written in this question. A 3-line code segment reads as follows. Line 1: public static int array Sum, open parenthesis, int, open square bracket, close square bracket, arr, close parenthesis. Line 2: public static int, open square bracket, close square bracket, row Sums, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, arr 2 D, close parenthesis. Line 3: public static boolean is Diverse, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, arr 2 D, open parenthesis.

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 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) {
    // Method implementation here
}

FRQ Type: Array/ArrayList, 2D Array

Key Algorithm:

The algorithm implemented in the isDiverse method of the DiverseArray class matches the FRQ type of Array/ArrayList and 2D Array. Let’s break down how the algorithm aligns with this FRQ type:

  1. Array/ArrayList Usage: The algorithm primarily operates on arrays. It utilizes a one-dimensional array to store row sums and then traverses through this array to check for duplicates. This demonstrates proficiency in handling arrays effectively, which is a key aspect of Array/ArrayList FRQs.

  2. 2D Array Handling: The algorithm receives a two-dimensional array as input and computes the sums of each row. It then examines these sums to determine whether they are diverse. This showcases competence in manipulating and processing 2D arrays, which is a fundamental skill in dealing with such FRQs.

  3. Control Structure: The algorithm employs nested loops to iterate through the row sums array and compare each sum with every other sum, checking for duplicates. This demonstrates mastery of control structures, specifically nested loops, to perform iterative tasks efficiently.

  4. Method Abstraction: The isDiverse method encapsulates the logic for determining array diversity, providing a clean and reusable solution. This exemplifies the use of methods for modularizing code and promoting code maintainability and readability.

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

    public static int[] rowSums(int[][] arr2D) {
        int[] sums = new int[arr2D.length];
        for (int i = 0; i < arr2D.length; i++) { // Loop through each row in the 2D array
            sums[i] = arraySum(arr2D[i]); // Calculate the sum of each row and store it in the sums array
        }
        return sums;
    }

    public static boolean isDiverse(int[][] arr2D) {
        int[] sums = rowSums(arr2D);
        for (int i = 0; i < sums.length - 1; i++) { // Loop through each sum except the last one
            for (int j = i + 1; j < sums.length; j++) { // Compare each sum with all after sums
                if (sums[i] == sums[j]) { // If any pair of sums are equal
                    return false; // The array is not diverse
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[] arr1 = {1, 3, 2, 7, 3};
        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("Sum of arr1: " + DiverseArray.arraySum(arr1));
        System.out.println("Row sums of mat1: " + java.util.Arrays.toString(DiverseArray.rowSums(mat1)));
        System.out.println("Row sums of mat2: " + java.util.Arrays.toString(DiverseArray.rowSums(mat2)));
        System.out.println("Is mat1 diverse?" + DiverseArray.isDiverse(mat1));
        System.out.println("Is mat2 diverse? " + DiverseArray.isDiverse(mat2));
    }
}
DiverseArray.main(null);
Sum of arr1: 16
Row sums of mat1: [16, 32, 28, 20]
Row sums of mat2: [14, 35, 36, 14]
Is mat1 diverse?true
Is mat2 diverse? false