Discussion 7. Make up tutorial Official make up slot: Friday 27 Oct (4-6) (by majority of the people...

10
Discussion 7

Transcript of Discussion 7. Make up tutorial Official make up slot: Friday 27 Oct (4-6) (by majority of the people...

Page 1: 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.

Discussion 7

Page 2: 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.

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)

Page 3: 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.

Questions on labs?

Nice Ebook on Java Regex http://www.javaregex.com/RegexReci

pesV1.pdf

Page 4: 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.

Arrays?

1D arrays? 2D arrays? ArrayList

0 1 2 3 4 5 6 7 8 9 10 11

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

Question 1 pg6071. Identify problems with this code:

public int searchAccount( int[25] number ) { 1number = 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[].

Page 8: 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.

Question3. What is wrong with the following algorithm that transposes a

n 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 89 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] }}

Page 9: 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.

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 33 matrix.Before transpose: 2 4 5 After transpose: 2 1 9

1 8 2 4 8 89 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?

Page 10: 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.

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

We are going to implement the Board class which is to be used for TicTacToe