Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 #25 2014T1.

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 #25 2014T1."— Presentation transcript:

1 Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 #25 2014T1

2 © Peter Andreae COMP 102 25:2 Menu Assignment 8: DiskGame and Genealogy Some more array details 2D arrays Administration Did you miss the test?

3 © Peter Andreae COMP 102 25:3 Assignment 8 Same ideas as last week: arrays and ArrayLists DiskGame Array of Disk objects Disk is defined for you You have to implement the interface and the game logic Fill the array with disks Respond to key events: ← and → move the gun; “space” fires the gun move the shot up the screen in a loop check if it is touching a disk, and damage it. Doing things to each element of the array Comparing an element to every other element Program design: breaking program into multiple methods

4 © Peter Andreae COMP 102 25:4 Assignment 8 Genealogy ArrayList of Person objects Person is defined for you You have to implement methods to search the “database” of Persons Load the ArrayList from a file Searching the ArrayList for items Doing things to each element of the array Program design: breaking program into multiple methods

5 © Peter Andreae COMP 102 25:5 Arrays as Parameters You can pass an array as a parameter to a method : int[ ] myNums = new int[ ] { 3,5,7,11,13,17,19,23,29,31,37,43,47}; UI.println(“Prime sum = ” + this.sum(myNums)); this.makeSquare(myNums); : public int sum(int[] a) { int s = 0; for (int i = 0; i < a.length; i++) s += a[i]; return s; } public void makeSquare(int[] a) { for (int i = 0; i < a.length; i++) a[i] = a[i] * a[i]; } a: 35711131719232931374347myNums:

6 © Peter Andreae COMP 102 25:6 Comparing arrays. Be careful when comparing arrays (as with all objects) int[ ] a = new int[ ]{ 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 43, 47}; int[ ] b = new int[ ]{ 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 43, 47}; int[ ] c = b; if (a == b)..  ?? no if (b == c)..  ?? yes if (a.equals(b) )..  ?? no if (Arrays.equals(a, b) )..  ?? yes if (this.arrayEquals(a, b) )..  ?? yes public boolean arrayEquals(int[] a, int[] b) { if ( a.length != b.length ) { return false; } for (int i = 0; i < a.length; i++) { if ( a[i] != b[i] ) { return false; } } return true; }

7 © Peter Andreae COMP 102 25:7 Main method main takes an array of strings as a parameter: Can be used to provide “command line inputs” to a program, eg. the name of a file to process. May be optional: public static void main(String[ ] args) { String fname; if ( args.length > 0 ) { fname = args[0]; } else { fname = UIFileChooser.open("Data files"); } try { Scanner scan = new Scanner (new File(fname)); while (scan.hasNext()) { … } } catch (IOException e){ …… } } > java Plotter mynumbers.txt

8 © Peter Andreae COMP 102 25:8 2D Data XOX O MonTueWedThuFriSatSun 9-10 10-11 11-12 12-1 1-2 2-3 3-4 4-5 IDA1A2A3A4 30012 30031 30048 30056 30080 30118 30185 30302 30345 30382 30495 30515 30545 242 484 242

9 © Peter Andreae COMP 102 25:9 2D arrays: Creating 2D arrays require two indices Declaring and creating: int[ ] [ ] marks = new int [200][4]; ChessPiece[ ][ ] board = new ChessPiece [8][8]; Color[ ][ ] image = new Color [100][150]; int[ ][ ] matrix = new int [ ][ ]{{2, 4, 2},{4, 8, 4}, {2, 4, 2}}; Which is the row and which is the column? You choose! Choose your variable names carefully.

10 © Peter Andreae COMP 102 25:10 2D arrays: accessing Assigning and accessing: marks[10][3] = 72; board[row][col] = board[row][col-1]; board[row][col-1] = null; for (int row=0; row<100; row++){ for (int col=0; col<100; col++){ image[row][col] = image[row][col].darker(); } In Java, can’t use commas image[row, col]

11 © Peter Andreae COMP 102 25:11 2D arrays 2D arrays are actually arrays of arrays: int[ ] [ ] marks = new int[200] [4]; is the same as int[ ] [ ] marks = new int[200] [ ]; for (int i = 0; i<200; i++){ marks[i] = new int[4]; }

12 © Peter Andreae COMP 102 25:12 2D arrays: length Number of rows and columns in a 2D array? int[ ] [ ] marks = new int[200] [4]; marks.length  number of rows (200) marks[row].length  number of columns (4)

13 © Peter Andreae COMP 102 25:13 2D arrays Can have non-square arrays: int[ ] [ ] table = new int[7] [ ]; for (int row = 0; row<7; row++){ table[row] = new int[row+1]; }

14 © Peter Andreae COMP 102 25:14 Processing 2D arrays Typically use nested for loops to process each item public void printTable( String[ ][ ] grades){ for (int row=0; row< grades.length; row++){ for (int col=0; col< grades[row].length; col++){ UI.printf(" %-2s ", grades[row][col]); } UI.println(); } } public void printTable( String[ ][ ] grades){ for (String[ ] row : grades){ for (String grade : row){ UI.printf(" %-2s ", grade); } UI.println(); } } '-' flag means left justified A+ B- A- B B+ C A B- A D A+ A A- B+ B+ B A A- C+ C+

15 © Peter Andreae COMP 102 25:15 Drawing a 2D array public void drawBoard(ChessPiece[ ][ ] board){ int rows = board.length; int cols = board[0].length; for (int row=0; row<rows; row++){ int y = this.top+this.size*row; for (int col=0; col<cols; col++) { int x = this.left+this.size*col; UI.setColor( (row%2==col%2) ? Color.gray : Color.white); UI.fillRect(x, y, this.size, this.size); if (board[row][col] !=null) { board[row][col].draw(x, y); } }♔♕♗♖♘♙♚♛♜♝♞♟ } UI.drawRect(x, y, this.size*rows, this.size*cols); } ♖ ♔♘ ♗ ♙♜ ♝ ♚ ♜ Shorthand for: if (row%2==col%2) { UI.setColor(Color.gray); } else { UI.setColor(Color.white); }

16 © Peter Andreae COMP 102 25:16 Printing a table with headers public void printTable(long[ ] IDs, String[ ][ ] grades){ int rows = grades.length; int cols = grades[0].length; UI.print(" ID |"); for (int col=0; col<cols; col++) { UI.printf(" A%d |", col); } UI.println(); for (int col=-1; col<cols; col++) { UI.print("----+"); } UI.println(); for (int row=0; row<rows; row++){ UI.printf("%4d|", IDs[row]) for (int col=0; col<cols; col++) { UI.printf(" %-2s |", grades[row][col]); } UI.println(); } for (int col=-1; col<cols; col++) { UI.print("----+"); } UI.println(); } ID | A1 | A2 | A3 | A4 | ----+----+----+----+----+ 3012| A+ | B- | A- | B | 3052| B+ | C | A | B- | 3029| A | D | A+ | A | 3172| A- | B+ | B+ | B | 3094| A | A- | C+ | C+ | ----+----+----+----+----+ Assumes all rows same length


Download ppt "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 #25 2014T1."

Similar presentations


Ads by Google