100 likes | 115 Views
Join the official makeup tutorial slot on Friday 27th Oct. Have questions on labs? Check out a Java Regex ebook. Solve array lab problems and Java code issues. Discover solutions for transposing a square matrix.
E N D
Make up tutorial • Official make up slot: Friday 27 Oct (4-6) (by majority of the people that send me the email) • For those who cannot make it, please let me know the email 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 • http://www.javaregex.com/RegexRecipesV1.pdf
0 1 2 3 4 5 6 7 8 9 10 11 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 pg607 1. 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 pg607 1. 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 intit is int[].
Question 3. What is wrong with the following algorithm that transposes an mm 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 33 matrix. Before transpose: 2 4 5 After transpose: 2 1 9 1 8 2 4 8 8 9 8 0 5 2 0 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 mm 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 33 matrix. Before transpose: 2 4 5 After transpose: 2 1 9 1 8 2 4 8 8 9 8 0 5 2 0 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 fromhttp://www.comp.nus.edu.sg/~lekhsian/cs1101/TicTacToe.javahttp://www.comp.nus.edu.sg/~lekhsian/cs1101/Board.java • We are going to implement the Board class which is to be used for TicTacToe