This broke the notebook so I had to put it here. I showed Rachit the working ipynb file but it breaks the blog when pushed.
Question 1
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.
Complete method arraySum below.
public static int arraySum(int[] arr) {[^17^][17][^18^][18]
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
public class DiverseArray1 {
public static int arraySum(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
public static void main(String[] args) {
int[] testArray = {1, 2, 3, 4, 5};
System.out.println(arraySum(testArray)); // It should print 15
}
}
DiverseArray1.main(new String[]{});
(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.
Complete method rowSums below.
public static int[] rowSums(int[][] arr2D) {
int[] sums = new int[arr2D.length];
for (int i = 0; i < arr2D.length; i++) {
sums[i] = arraySum(arr2D[i]);
}
return sums;
}
public class DiverseArray2 {
public static int arraySum(int[] arr) {
int sum = 0;
for (int i : arr) {
sum += i;
}
return sum;
}
public static int[] rowSums(int[][] arr2D) {
int[] sums = new int[arr2D.length];
for (int i = 0; i < arr2D.length; i++) {
sums[i] = arraySum(arr2D[i]);
}
return sums;
}
public static void main(String[] args) {
int[][] arr2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[] sums = rowSums(arr2D);
for (int sum : sums) {
System.out.println(sum);
}
}
}
DiverseArray2.main(new String[]{});
(c) Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse.
Complete method isDiverse below.
public static boolean isDiverse(int[][] arr2D) {
int[] rowSums = rowSums(arr2D);
for (int i = 0; i < rowSums.length - 1; i++) {
for (int j = i + 1; j < rowSums.length; j++) {
if (rowSums[i] == rowSums[j]) {
return false;
}
}
}
return true;
}
public class DiverseArray {
public static int arraySum(int[] arr) {
int sum = 0;
for (int i : arr) {
sum += i;
}
return sum;
}
public static int[] rowSums(int[][] arr2D) {
int[] sums = new int[arr2D.length];
for (int i = 0; i < arr2D.length; i++) {
sums[i] = arraySum(arr2D[i]);
}
return sums;
}
public static boolean isDiverse(int[][] arr2D) {
int[] rowSums = rowSums(arr2D);
for (int i = 0; i < rowSums.length - 1; i++) {
for (int j = i + 1; j < rowSums.length; j++) {
if (rowSums[i] == rowSums[j]) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
int[][] arr2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(isDiverse(arr2D));
}
}
DiverseArray.main(new String[]{});
This broke the notebook so I had to put it here. I showed Rachit the working ipynb file but it breaks the blog when pushed.
Question 1
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Notes:
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.Complete method
arraySum
below.(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.Complete method
rowSums
below.(c) Write a static method
isDiverse
that determines whether or not a given two-dimensional array is diverse.Complete method
isDiverse
below.