Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discussion 7. 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.

Similar presentations


Presentation on theme: "Discussion 7. 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."— Presentation transcript:

1 Discussion 7

2 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)

3 Questions on labs? Nice Ebook on Java Regex http://www.javaregex.com/RegexRecip esV1.pdf http://www.javaregex.com/RegexRecip esV1.pdf

4 Arrays? 1D arrays? 2D arrays? ArrayList 01234567891011

5 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; }

6 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; }

7 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 int it is int[].

8 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:2 1 9 1 8 24 8 8 9 8 05 2 0 for (int row = 0; row < m; ++row) { for (int col = 0; col < m; ++col) { // swap matrix[row][col] with matrix[col][row] }

9 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:2 1 9 1 8 24 8 8 9 8 05 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?

10 Tic Tac Toe Download main program from http://www.comp.nus.edu.sg/~lekhsian /cs1101/TicTacToe.java http://www.comp.nus.edu.sg/~lekhsian /cs1101/Board.java http://www.comp.nus.edu.sg/~lekhsian /cs1101/TicTacToe.java http://www.comp.nus.edu.sg/~lekhsian /cs1101/Board.java We are going to implement the Board class which is to be used for TicTacToe


Download ppt "Discussion 7. 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."

Similar presentations


Ads by Google