1 / 10

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

Download Presentation

Discussion 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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/RegexRecipesV1.pdf

  4. 0 1 2 3 4 5 6 7 8 9 10 11 Arrays? • 1D arrays? • 2D arrays? • ArrayList

  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 intit 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 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] } }

  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 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?

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

More Related