Question: public class Q1Test { public static void main(String[] args) { Integer[][] matrix0 = null;

public class Q1Test {
public static void main(String[] args) {
Integer[][] matrix0 = null;
Integer[][] matrix1 = {{1}};
Integer[][] matrix2 = {{1, 2, 3}};
Integer[][] matrix3 = {{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3}};
Integer[][] matrix4 = {{1, 2, 3, 4, 5, 6, 7},
{8, 9, 10, 0, 0, 0, 5} ,
{3, 3, 3, 3, 3, 3, 3}};
Integer[][] matrix5 = {{null}};
Integer[][] matrix6 = {{null, null, null},
{null, null, null}};
Integer[][] matrix7 = {{1, null, 3},
{1, 2, null},
{null, 2, 3},
{1, null, null},
{null, 2, null},
{null, null, 3}};
Integer[][] matrix8 = {{1, 2, null, 4, 5, 6, 7},
{null, 2, 3, 4, 5, 6, null},
{1, 2, 3, null, 5, 6, null}};
Integer[][] matrix9 = {{1},
{null},
{1},
{1}};
System.out.print("Result for matrix0: ");
printArray(IntegerUtil.sumRows(matrix0));
System.out.print("Result for matrix1: ");
printArray(IntegerUtil.sumRows(matrix1));
System.out.print("Result for matrix2: ");
printArray(IntegerUtil.sumRows(matrix2));
System.out.print("Result for matrix3: ");
printArray(IntegerUtil.sumRows(matrix3));
System.out.print("Result for matrix4: ");
printArray(IntegerUtil.sumRows(matrix4));
System.out.print("Result for matrix5: ");
printArray(IntegerUtil.sumRows(matrix5));
System.out.print("Result for matrix6: ");
printArray(IntegerUtil.sumRows(matrix6));
System.out.print("Result for matrix7: ");
printArray(IntegerUtil.sumRows(matrix7));
System.out.print("Result for matrix8: ");
printArray(IntegerUtil.sumRows(matrix8));
System.out.print("Result for matrix9: ");
printArray(IntegerUtil.sumRows(matrix9));
}
public static void printArray(Integer[] array) {
if (array == null) {
System.out.println("null");
return;
}
System.out.print("[");
for (int i = 0; i if (i > 0) {
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println("]");
}
}
public class IntegerUtil {
public static Integer[] sumRows (Integer[][] matrix) {
if (matrix == null) {
return null;
}
// WRITE YOUR CODE HERE
//Remove the following line when this method has been implemented
return null;
}
}
In Question 1, you develop a static method named sumRows that takes a two-dimensional array (matrix) of Integer objects and returns a one-dimensional array of Integer objects. Each element in the returned array is the sum of the elements of the corresponding row in the input matrix. public class IntegerUtil ( public static Integer [] sumRows (Integer [][] matrix) { } if (matrix null) { return null; // All the code that you write for Question 1 goes here! The behaviour of sumRows(...) is illustrated in the figure below. As shown in the figure, the input matrix may have null values. Any null value should be treated as a zero (0) during summation. sumRows (1 02 1 2 3 0 103 1 2 3 1 1 1 sumRows null 2 1 [6 33 54] 2 3 0 1 null 3 null values are treated as zeros when computing the row sums. To test your implementation of sumRows(...), use the Q1Test.java in the template code provided to you. The output of running Q1Test should be as follows: $ java Q1Test Result for matrix: null Result for matrix1: [1] Result for matrix2: [6] Result for matrix3: [6, 6, 6, 6, 6, 6, 6, 6, 6] Result for matrix4: 28, 32," '211 Result for matrix5: [0] Result for matrix6: [0, 0] Result for matrix7: [4, 3, 5, 1, 2, 3] Result for matrix8: [25, 20, 17] Result for matrix9: [1, 0, 1, 1] $ Important Restrictions for Question 1 You cannot import anything at all. You cannot use any class other than the ones provided. You cannot declare any class or instance variables in IntegerUtil.java. All your variables should be local variables in the sumRows(...) method. QI Files IntegerUtil.java (you need to update this file) Q1Test.java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
