Week 9 - Monday
What did we talk about last time? Method practice Lab 8
Just as it is possible to make a one dimensional list out of a single data type, it is also possible to make a table out of one data type We can extend the arrays you know to have two dimensions with very similar syntax
To declare a two dimensional array, we just use two sets of square brackets ( [][] ): Doing so creates a variable that can hold a 2D array of int s As before, we still need to instantiate the array to have a specific size: int [][] table; table = new int[5][10];
Like matrices, we usually visualize the first dimension as the rows and the second dimension as the columns Second Dimension First Dimension
Let’s write a little code to put data into the table int [][] table = new int[5][10]; int label = 1; for( int i = 0; i < 5; i++ ) for( int j = 0; j < 10; j++ ) { table[i][j] = label; label++; } int [][] table = new int[5][10]; int label = 1; for( int i = 0; i < 5; i++ ) for( int j = 0; j < 10; j++ ) { table[i][j] = label; label++; }
The result of that code is: Second Dimension First Dimension
We could represent a chessboard as an 8 x 8 array of char s Use the following encoding: 'P' = pawn 'N' = knight 'B' = bishop 'R' = rook 'Q' = queen 'K' = king Use upper case characters for black pieces and lower case characters for white ones
Imagine there is a pawn randomly set on the board and a queen of the opposite color on the board Write a program to see if the queen can capture the pawn in the next move
Find the row and column location of both the queen and the pawn The pawn is in danger if: 1. The queen and the pawn have the same row 2. The queen and the pawn have the same column 3. If the absolute value of the differences between their rows and the absolute value of the differences between their columns are the same
A cell is represented by a block in a grid Each cell has 8 neighbors Simple rules for a cell “coming to life” or “dying”: 1. A live cell with fewer than 2 live neighbors dies from loneliness 2. A live cell with more than 3 live neighbors dies from overcrowding 3. A live cell with exactly 2 or 3 neighbors keeps living 4. A dead cell with exactly 3 neighbors comes to life
We can represent the grid of cells with a 2D array of boolean values true means alive false means dead Each iteration, we draw the grid onto the screen with StdDraw Black means alive White means dead Then, we update the grid to contain the new values The grid stores the state of the game We still have to use StdDraw to draw that state
It doesn’t have to stop at 2 dimensions! You can have 3 or more Here’s an example with 3 dimensions: int[][][] rubiksCube = new int[3][3][3]; int count = 1; for( int i = 0; i < 3; i++ ) for( int j = 0; j < 3; j++ ) for( int k = 0; k < 3; k++ ) { rubiksCube[i][j][k] = count; count++; } int[][][] rubiksCube = new int[3][3][3]; int count = 1; for( int i = 0; i < 3; i++ ) for( int j = 0; j < 3; j++ ) for( int k = 0; k < 3; k++ ) { rubiksCube[i][j][k] = count; count++; }
It looks like whatever you want it to You can visualize it in 3D if you want There are other techniques It’s just a way to store data It doesn’t actually look like anything inside the computer
Sometimes you have data categorized in several different ways For example, E-town might keep some statistics according to Year, Gender, and Race 0 – Freshman 1 – Sophomore 2 – Junior 3 – Senior Perfect candidate for a 3D array 0 – Male 1 – Female 0 – African American 1 – Asian 2 – Caucasian 3 – Other
Too many brackets Too much stuff Total size used is the product of the length of all the dimensions 100 x 100 x 100 = 1,000,000 Hard to visualize, hard to imagine Up as high as 4 is sometimes useful Don’t go beyond 2 on a regular basis
Finish Game of Life Overloading methods More method practice
Keep reading Chapter 8 of the textbook Keep working on Project 3 Due this Friday