Question 1
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.
(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.
(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.
public class DiverseArray {
public static int arraySum (int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
public static int[] rowSums(int[][] arr2D) {
int[] sums = new int[arr2D.length];
for (int i = 0; i < arr2D.length; i++) {
int sum = 0;
for(int k = 0; k < arr2D[i].length; k++) {
sum = sum + arr2D[i][k];
}
sums[i] = sum;
}
return sums;
}
public static boolean isDiverse(int[][] arr2D) {
int[] sums = rowSums(arr2D);
HashSet<Integer> set = new HashSet<>();
for (int i : sums) {
if (set.add(i) == false) {
return false;
}
set.add(i);
}
return true;
}
public static void main(String[] args) {
int[] arr = {1, 3, 2, 7, 2};
System.out.println(arraySum(arr));
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}
};
int[] sums = rowSums(mat1);
for (int i = 0; i < sums.length; i++) {
System.out.print(sums[i] + " ");
}
System.out.println();
int[] sums1 = rowSums(mat2);
for (int i = 0; i < sums1.length; i++) {
System.out.print(sums1[i] + " ");
}
System.out.println();
System.out.println(isDiverse(mat1));
System.out.println(isDiverse(mat2));
}
}
DiverseArray.main(null);
15
16 32 28 20
14 35 36 14
true
false
Reflection
This question was about arrays/ArrayLists, and doing the FRQ helped me realize how much I learned about Java during these 2 trimesters. The first parts where I find the sum of a 1D Array and the sum of each row in a 2D Array was much easier than I thought and I was able to do them pretty quickly. The third part was much harder for me. At first, I tried using a nested for loop to determine whether or not the row sums were diverse, but I kept getting something wrong and I was stuck. I remember that I could use a hashset, but I couldn’t remember the format so I used the Internet to find out the syntax for a hashset, and I was ultimately able to figure out part c. While this FRQ did boost my confidence on Java, it helped me realize that I still have some ways to go before I am comfortable coding entirely on my own. I will practice more FRQs in a timely manner so that I will be prepared for the AP Exam.