Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110: Spring 20091 Announcements Lab 7 was due today Binary Expression Assignment due Friday.

Similar presentations


Presentation on theme: "COMP 110: Spring 20091 Announcements Lab 7 was due today Binary Expression Assignment due Friday."— Presentation transcript:

1 COMP 110: Spring 20091 Announcements Lab 7 was due today Binary Expression Assignment due Friday

2 COMP 110: Spring 20092 Today in COMP 110 Multi-Dimensional Arrays Programming Demo TicTacToe

3 COMP 110: Spring 20093 2D Arrays Arrays having more than one index are often useful Tables Grids Bingo games? 0: Open1: High2: Low3: Close 0: Apple Inc. 99.2499.8595.7298.24 1: Walt Disney Co. 21.5524.2021.4123.36 2: Google Inc. 333.12341.15325.33331.14 3: Microsoft Corp. 21.3221.5421.0021.50

4 COMP 110: Spring 20094 Creating 2D Arrays //create a 2D array with 4 rows and 3 columns int[][] table = new int[4][3]; or int[][] table; table = new int[4][3]; 99.2499.8595.72 21.5524.2021.41 333.12341.15325.33 21.3221.5421.00 rows columns

5 COMP 110: Spring 20095 Creating 2D Arrays int[][] table = new int[4][3]; gives you access to table[0][0] //1 st row, 1 st column table[0][1] //1 st row, 2 nd column table[0][2] //1 st row, 3 rd column table[1][0] table[1][1] table[1][2] table[2][0] table[2][1] table[2][2] table[3][0] //4 th row, 1 st column table[3][1] //4 th row, 2 nd column table[3][2] //4 th row, 3 rd column 99.2499.8595.72 21.5524.2021.41 333.12341.15325.33 21.3221.5421.00 table[0][0]table[0][1]table[0][2] table[1][0]table[1][1]table[1][2] table[2][0]table[2][1]table[2][2] table[3][0]table[3][1]table[3][2]

6 COMP 110: Spring 20096 Using 1D Arrays We used a single loop to process a 1D array int[] scores = { 13, 57, 93, 60, 102 }; for(int i = 0; i < scores.length; i++) { System.out.println(scores[i]); } 6

7 COMP 110: Spring 20097 Using 2D Arrays When processing 2D arrays, we usually used two nested loops int[][] table = new int[4][3]; //the outer loop iterates over the rows for(int row = 0; row < 4; row++) { //the inner loop iterates over the rows for(int column = 0; column < 3; column++) { table[row][column] = 0; }

8 COMP 110: Spring 20098 Multi-Dimensional Arrays You can have more than two dimensions int[][][] table = new int[4][3][5]; Use more nested loops to access all elements 8

9 COMP 110: Spring 20099 Representation of 2D Arrays int[] scores = new int[5]; scores is a one-dimensional array The type of scores[0], scores[1] etc is int int[][] table = new int[4][3]; Internally table is also represented as a one- dimensional array The type of table[0], table[1] etc is int[] (an array!) We still refer to table as a two-dimensional array

10 COMP 110: Spring 200910 Representation of 2D Arrays int[][] table = new int[4][3]; 1.12.62.7 7.26.4 9.47.35.1 table[0] table[1] table[2] table[3] table 5.17.59.5

11 COMP 110: Spring 200911 Length field for 2D Arrays int[][] table = new int[4][3]; table.length gives the number of rows (4) table[0].length, table[1].length etc gives the number of columns (3)

12 COMP 110: Spring 200912 Multi-Dimensional Arrays as Parameters //a method to print a 2D array to screen public void print2DArray(int[][] arr) { for(int row = 0; row < arr.length; row++) { for(int column = 0; column < arr[row].length; column++) { System.out.print(arr[row][column] + " "); } System.out.println(); }

13 COMP 110: Spring 200913 Multi-Dimensional Arrays as Return Types //create a 2D array of the specified size and return it public int[][] create2DArray(int rows, int columns) { int[][] array = new int[rows][columns]; return array ; }

14 COMP 110: Spring 200914 Programming Demo TicTacToe Create a class that allows the user to play a game of TicTacToe

15 COMP 110: Spring 200915 TicTacToe Represent the game board as a 2D array of size 3x3 Type? char XOO OXX XXO

16 COMP 110: Spring 200916 Pseudocode Until game over display game board ask user for move until valid one is entered make move check for a winner check if board full

17 COMP 110: Spring 200917 Decomposition Public methods Constructor sets everything up void play() starts the game Possible Methods void displayBoard() Displays the entire game board boolean askForInput() Allows the user to update the game board by making a move Returns true if the move resulted in a winner and false if not boolean isMoveValid(int row, int column) Return whether or not the move is valid int getWinner() boolean boardFull() Returns whether the board is full and the game is over

18 COMP 110: Spring 200918 Programming Demo Programming

19 COMP 110: Spring 200919 Friday Recitation Bring Laptop (fully charged)


Download ppt "COMP 110: Spring 20091 Announcements Lab 7 was due today Binary Expression Assignment due Friday."

Similar presentations


Ads by Google