(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.
Complete method arraySum below.
-
/ * * Returns the sum of the entries in the one-dimensional array arr.
-
- /
public static int arraySum (int [ ] arr)
(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}.
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)
(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.
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.
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)
Working code
public class ArrayOperations {
public static int calculateSum (int[] array)
{
int total = 0;
for (int element : array)
{
total += element;
}
return total;
}
public static int[] calculateRowSums(int[][] twoDArray) {
int[] rowSums = new int[twoDArray.length];
for (int i = 0; i < twoDArray.length; i++) {
rowSums[i] = calculateSum(twoDArray[i]);
}
return rowSums;
}
public static boolean checkDiversity(int[][] twoDArray)
{
int[] rowSums = calculateRowSums(twoDArray);
Set<Integer> uniqueSums = new HashSet<>();
for (int sum : rowSums) {
if (!uniqueSums.add(sum)) {
return false;
}
}
return true;
}
}
public class Main {
public static void main(String[] args)
{
int[] singleArray = {1, 3, 2, 7, 3};
System.out.println(ArrayOperations.calculateSum(singleArray));
int[][] doubleArray = {
{1, 1, 5, 3, 4},
{12, 7, 6, 1, 9},
{8, 11, 10, 2, 5},
{3, 2, 3, 0, 6}
};
int[] rowSums = ArrayOperations.calculateRowSums(doubleArray);
for (int sum : rowSums)
{
System.out.print(sum + " ");
}
System.out.println();
System.out.println(ArrayOperations.checkDiversity(doubleArray));
}
}
Main.main(null);
16
14 35 36 14
false
The ArrayOperations class has three static methods:
calculateSum(int[] array): This method calculates the sum of all elements in a one-dimensional array. calculateRowSums(int[][] twoDArray): This method calculates the sum of each row in a two-dimensional array. It uses the calculateSum method to compute the sum of each row. checkDiversity(int[][] twoDArray): This method checks if all rows in a two-dimensional array have unique sums. It uses the calculateRowSums method to get the sum of each row, then it checks if all sums are unique. If they are, it returns true; otherwise, it returns false. The Main class has a main method which is the entry point of the program. It creates a one-dimensional array and a two-dimensional array, then it calls the methods from the ArrayOperations class to perform various operations on these arrays and prints the results. Specifically, it:
Calculates and prints the sum of the elements in the one-dimensional array. Calculates and prints the sum of each row in the two-dimensional array. Checks if all rows in the two-dimensional array have unique sums and prints the result.