COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
-
Upload
joseph-lamb -
Category
Documents
-
view
214 -
download
0
Transcript of COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110:Introduction to Programming
Tyler JohnsonApr 1, 2009
MWF 11:00AM-12:15PMSitterson 014
COMP 110: Spring 20092
Announcements
Lab 6 has been graded
COMP 110: Spring 20093
Questions?
COMP 110: Spring 20094
Today in COMP 110
Some Misc. Items
Multi-Dimensional Arrays
Programming DemoTicTacToe
COMP 110: Spring 20095
Program 4
Some comments
COMP 110: Spring 20096
Program 4
Extended to Friday at 5pm to better match the other section
COMP 110: Spring 20097
Lab 7
Some comments on the reverse/wheel methods
COMP 110: Spring 20098
Lab 6
COMP 110: Spring 20099
Multi-Dimensional Arrays
Section 7.5 in text
COMP 110: Spring 200910
2D Arrays
Arrays having more than one index are often useful
TablesGridsBingo games
0: Open 1: High 2: Low 3: Close
0: Apple Inc. 99.24 99.85 95.72 98.24
1: Walt Disney Co.
21.55 24.20 21.41 23.36
2: Google Inc. 333.12 341.15 325.33 331.14
3: Microsoft Corp.
21.32 21.54 21.00 21.50
COMP 110: Spring 200911
Creating 2D Arrays
//create a 2D array with 4 rows and 3 columnsint[][] table = new int[4][3];
or
int[][] table;table = new int[4][3];
99 99 95
21 24 21
333 341 325
21 21 21
rows
columns
Can be used to create a table that looks like this
COMP 110: Spring 200912
Creating 2D Arrays
int[][] table = new int[4][3];
gives you access to
table[0][0] //1st row, 1st columntable[0][1] //1st row, 2nd columntable[0][2] //1st row, 3rd column table[1][0]table[1][1]table[1][2]table[2][0]table[2][1]table[2][2]table[3][0] //4th row, 1st column table[3][1] //4th row, 2nd column table[3][2] //4th row, 3rd 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 99 95
21 24 21
333 341 325
21 21 21
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]);}
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 rowsfor(int row = 0; row < 4; row++) {
//the inner loop iterates over the columns for(int column = 0; column < 3; column++) { table[row][column] = 0;
}}
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
COMP 110: Spring 200916
Representation of 2D Arrays
int[] scores = new int[5];
“scores” is a one-dimensional arrayThe 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
COMP 110: Spring 200917
Representation of 2D Arrays
int[][] table = new int[4][3];
1 2 2
2 7 6
9 7 5
table[0]
table[1]
table[2]
table[3]
table
5 7 9
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)
COMP 110: Spring 200919
Multi-Dimensional Arrays as Parameters
//a method to print a 2D array to screenpublic 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 }}
COMP 110: Spring 200920
Multi-Dimensional Arrays as Return Types
//create a 2D array of the specified size and return itpublic int[][] create2DArray(int rows, int columns) {
int[][] array = new int[rows][columns];
return array;}
COMP 110: Spring 200921
Programming Demo
TicTacToe
Create a class that allows the user to play a game of TicTacToe
COMP 110: Spring 200922
TicTacToe
Represent the game board as a 2D array of size 3x3
Type?• char
X O O
O X X
X X O
COMP 110: Spring 200923
Pseudocode
Until donedisplay game boarddisplay 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
COMP 110: Spring 200924
Decomposition
Public methodsConstructor
• sets everything upvoid play()
• starts the game
Private Methodsvoid displayBoard()
• Displays the entire game boardvoid 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
COMP 110: Spring 200925
Programming Demo
Programming
COMP 110: Spring 200926
Friday
Recitation
BringLaptop (fully charged)Textbook