Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 9 - Monday.  What did we talk about last time?  Method practice  Lab 8.

Similar presentations


Presentation on theme: "Week 9 - Monday.  What did we talk about last time?  Method practice  Lab 8."— Presentation transcript:

1 Week 9 - Monday

2  What did we talk about last time?  Method practice  Lab 8

3

4

5

6  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

7  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];

8  Like matrices, we usually visualize the first dimension as the rows and the second dimension as the columns 0 1 2 3 4 5 6 7 8 9 0123401234 Second Dimension First Dimension

9  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++; }

10  The result of that code is: 0 1 2 3 4 5 6 7 8 9 0123401234 Second Dimension First Dimension

11  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

12  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

13  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

14  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

15  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

16

17  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++; }

18  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

19  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

20  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

21

22  Finish Game of Life  Overloading methods  More method practice

23  Keep reading Chapter 8 of the textbook  Keep working on Project 3  Due this Friday


Download ppt "Week 9 - Monday.  What did we talk about last time?  Method practice  Lab 8."

Similar presentations


Ads by Google