Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Similar presentations


Presentation on theme: "COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014."— Presentation transcript:

1 COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014

2 COMP 110: Spring 20092 Announcements Lab 6 has been graded

3 COMP 110: Spring 20093 Questions?

4 COMP 110: Spring 20094 Today in COMP 110 Some Misc. Items Multi-Dimensional Arrays Programming Demo TicTacToe

5 COMP 110: Spring 20095 Program 4 Some comments

6 COMP 110: Spring 20096 Program 4 Extended to Friday at 5pm to better match the other section

7 COMP 110: Spring 20097 Lab 7 Some comments on the reverse/wheel methods

8 COMP 110: Spring 20098 Lab 6

9 COMP 110: Spring 20099 Multi-Dimensional Arrays Section 7.5 in text

10 COMP 110: Spring 200910 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

11 COMP 110: Spring 200911 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 95 212421 333341325 21 rows columns Can be used to create a table that looks like this

12 COMP 110: Spring 200912 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 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] 99 95 212421 333341325 21

13 COMP 110: Spring 200913 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]); }

14 COMP 110: Spring 200914 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 columns for(int column = 0; column < 3; column++) { table[row][column] = 0; }

15 COMP 110: Spring 200915 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

16 COMP 110: Spring 200916 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

17 COMP 110: Spring 200917 Representation of 2D Arrays int[][] table = new int[4][3]; 122 276 975 table[0] table[1] table[2] table[3] table 579

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

19 COMP 110: Spring 200919 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(); //jump to the next line after each row is complete }

20 COMP 110: Spring 200920 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; }

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

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

23 COMP 110: Spring 200923 Pseudocode Until done display game board display game menu (move, restart, quit) if(move) ask where to move, update game board if(winner || board full) –done = true else if(restart) reset the game else if(quit) exit

24 COMP 110: Spring 200924 Decomposition Public methods Constructor sets everything up void play() starts the game Private Methods void displayBoard() Displays the entire game board void displayRow(int row) Displays a single row of the game board Used by displayBoard() void displayMenu() Displays the options menu to the user boolean getMove() 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 winner(int lastR, int lastC) Checks if the move to lastR,lastC resulted in a winner Used by getMove() boolean boardFull() Returns whether the board is full and the game is over

25 COMP 110: Spring 200925 Programming Demo Programming

26 COMP 110: Spring 200926 Friday Recitation Bring Laptop (fully charged) Textbook


Download ppt "COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014."

Similar presentations


Ads by Google