Practice Final

advertisement
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
public void testPutSumInDiagonal() {
int[][] twoDarray = { { 1, 2, 3, 4
{ 2, 3, 4, 5
{ 3, 4, 5, 6
{ 4, 5, 6, 7
Output
1
2
3
4
},
},
},
} };
2
3
4
5
3
4
5
6
4
5
6
7
10 0 0 0
0 14 0 0
0 0 18 0
0 0 0 22
Matrix m = new Matrix(twoDarray);
System.out.println(m.toString());
m.putSumInDiagonal();
System.out.println(m.toString());
}
public class Matrix {
private int[][] data;
private int nRows;
private int nCols;
public Matrix(int[][] table) {
nRows = table.length;
nCols = table[0].length;
data = new int[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() {
6
11. As part of the same Matrix class from the previous page, complete method mirrored()to change data to 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 arrays data should be changed like these two: (16pts)
data before
1
0
6
8
2
9
3
4
data after
4
3
9
2
public void mirrored() {
8
6
0
1
data before
data after
0 5 9 3 2 6
7 1 8 4 3 8
9 3 2 5 6 1
1 6 5 2 3 9
8 3 4 8 1 7
6 2 3 9 5 0
// 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. ___________
d. ___________
for (int j = n; j >= 0; j--)
sum++;
for( int j = 1; j <= n; j++ )
for( int k = 1; k <= n; k++ )
sum++;
for(int m = 1; m <= n; m++ )
sum++;
b. ____________
for (int j = 1; j <= n; j = j + 2)
sum++;
e. ____________
int j = 1;
sum++;
c. ____________
for(int k = 1; k <= n; k = 2 * k)
sum++;
f. ____________
for(int k = 1; k <= n; k = 2 * k)
for (int j = 1; j <= 4 * n; j = j + 2)
sum++;
7
13. Write an X in the comment to the right of all statements that compile. (3pts)
Object anObject = new Integer(3);
Object otherObject = "a string";
Integer anInt = new Object();
// a. ____
// b. ____
// c. ____
14. Write an X in the comment to the right of all statements that compile. (3pts)
Integer i1 = 3;
int i2 = new Integer(-1);
Object obj = 3;
// a. ____
// b. ____
// 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>
d._____ Binary Tree
e.____ int[]
c. ___ Comparable<E>
8
Download