Discussion 7
Make up tutorial Official make up slot: Friday 27 Oct (4-6) (by majority of the people that send me the ) For those who cannot make it, please let me know the me on the time slots you might to see me (optional) Monday 23 Oct (6pm) Thursday 22 Oct (4-6pm) Friday 27 Oct (2-4pm)
Questions on labs? Nice Ebook on Java Regex esV1.pdf esV1.pdf
Arrays? 1D arrays? 2D arrays? ArrayList
Lab4 // Returns the number of hits between object and guessPegs public int countHits (FourPegs guessPegs) { int count = 0; boolean[] used = new boolean[4]; for (int i = 1 ; i <= 4 ; i++){ if (guessPegs.getColour(i) == getColour(i)) used[i-1] = true; } for (int i = 1 ; i <= 4 ; i++){ for (int j = 1 ;j <= 4 ; j++){ if (i != j && guessPegs.getColour(i) == getColour(j) && !used[j-1]){ count++; used[j-1] = true; } return count; }
Question 1 pg Identify problems with this code: public int searchAccount( int[25] number ) { number = new int[15]; for (int i = 0; i < number.length; i++ ) number[i] = number[i-1] + number[i+1]; return number; }
Question 1 pg Identify problems with this code: public int searchAccount( int[25] number ) { 1 number = new int[15]; for (int i = 0; i < number.length; i++ ) 2 number[i] = number[i-1] + number[i+1]; return number; 3 } 1. Parameter declaration cannot include size (25) information. 2. The for loop will cause an ArrayIndexOutOfBounds exception when i = 0 because it will access number[i – 1] which will be number[-1] and is out of bounds because you can ’ t have a negative array index. 3. The return value number is not of type int it is int[].
Question 3.What is wrong with the following algorithm that transposes an m m square matrix? Transposing a matrix means exchanging the rows with the respective columns, that is, matrix[row][col] matrix[col][row]. Below shows the result of transposing a 3 3 matrix. Before transpose:2 4 5After transpose: for (int row = 0; row < m; ++row) { for (int col = 0; col < m; ++col) { // swap matrix[row][col] with matrix[col][row] }
Question (Cont.) 3.What is wrong with the following algorithm that transposes an m m square matrix? Transposing a matrix means exchanging the rows with the respective columns, that is, matrix[row][col] matrix[col][row]. Below shows the result of transposing a 3 3 matrix. Before transpose:2 4 5After transpose: for (int row = 0; row < m; ++row) { for (int col = 0; col < m; ++col) { // swap matrix[row][col] with matrix[col][row] } Can you come up with a solution( with a display method)? Another solution? Anymore solutions?! Which 1 is the best solution?
Tic Tac Toe Download main program from /cs1101/TicTacToe.java /cs1101/Board.java /cs1101/TicTacToe.java /cs1101/Board.java We are going to implement the Board class which is to be used for TicTacToe