10. In the Matrix class, complete method putSumInDiagonal that places the sum of each row on the diagonal with all other elements set to 0. It should work for any initial values set in the constructor (even if initial values are set to random integers). The output shown to the right of the method that generates that output shows the expected result (12pts)


@Test

publicvoid testPutSumInDiagonal() {

int[][] twoDarray = { { 1, 2, 3, 4 },

{ 2, 3, 4, 5 },

{ 3, 4, 5, 6 },

{ 4, 5, 6, 7 } };

Matrix m = new Matrix(twoDarray);

System.out.println(m.toString());

m.putSumInDiagonal();

System.out.println(m.toString());

}

publicclass Matrix {

privateint[][] data;

privateint nRows;

privateint nCols;

public Matrix(int[][] table) {

nRows = table.length;

nCols = table[0].length;

data = newint[nRows][nCols];

for (int i = 0; i < nRows; i++) {

for (int j = 0; j < nCols; j++) {

data[i][j] = table[i][j]; // Makes a clone of the 2D array argument

}

}

}

public String toString() {

String result = "";

for (int row = 0; row < nRows; row++) {

for (int col = 0; col < nCols; col++) {

result += " " + data[row][col];

}

result += "\n";

}

return result;

}

public void putSumInDiagonal() {

11. As part of the same Matrixclass from the previous page, complete method mirrored()to change datato the mirror image of itself with the top row moved to the bottom row, the 2nd row to the 2nd to last row and so on. All elements in each row must also be the mirror image. For example, the 2D arraysdata should be changed like these two: (16pts)

databefore data after data before data after

1 2 4 80 5 9 3 2 61 6 5 2 3 9

09 3 67 1 8 4 3 8 8 3 4 8 1 7

63 9 09 3 2 5 6 1 6 2 3 9 5 0

8 4 2 1

publicvoidmirrored() { // you have access to data, nRows, and nCols

12. Determine the tightest upper bound runtimes of the following loops. Express your answer in the Big-O notation we have been using in class (assume the initialization int sum = 0;). (20pts: 3pts each for order, 2pts for overall style)

a. ______
for (int j = n; j >= 0; j--)
sum++;
b. ______
for (int j = 1; j <= n; j = j + 2)
sum++;
c. ______
for(int k = 1; k <= n; k = 2 * k)
sum++; / d. ______
for( int j = 1; j <= n; j++ )
for( int k = 1; k <= n; k++ )
sum++;
for(int m = 1; m <= n; m++ )
sum++;
e. ______
int j = 1;
sum++;
f. ______
for(int k = 1; k <= n; k = 2 * k)
for (int j = 1; j <= 4 * n; j = j + 2)
sum++;

13. Write an X in the comment to the right of all statements that compile. (3pts)

Object anObject = new Integer(3); // a. ____

Object otherObject = "a string"; // b. ____

Integer anInt = new Object(); // c. ____

14. Write an X in the comment to the right of all statements that compile. (3pts)

Integer i1 = 3; // a. ____

int i2 = new Integer(-1); // b. ____

Object obj = 3; // c. ____

15. Write an X in the comment to the right of all statements that compile. (3pts)

Object[] elements = new Object[5]; // a. ____

elements[0] = 12; // b. ____

elements[1] = "Second"; // c. ____

16. Will this code compile, yes or no _____? (2pts)

Object obj = 3;

Integer anInt = obj;

17. Will this code generate a compiletime error, yes or no _____? (2pts)

Object obj = 3;

String str = (String)obj;

18. Will this code generate a runtime error (an exception) when it is run, yes or no _____? (2pts)

Object obj = 3;

String str = (String)obj

19 Write DS if it's a data structure, COLL if it's a collection class or ADT if an abstract data type (5pts)

a.______Bag b._____ PriorityList<E> c. ___ Comparable<E>

d._____ Binary Treee.____ int[]

1