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 20123T1.

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 20123T1."— Presentation transcript:

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

2 © Peter Andreae COMP 102 25:2 Menu Assignment 8: BalloonGame and Genealogy Some more array details 2D arrays Administration Did you miss the test? Friday is the last day to drop the course (unless you have special permission from the dean) Marks for the test will not be back in time for Friday 

3 © Peter Andreae COMP 102 25:3 Assignment 8 Same ideas as last week: arrays and ArrayLists BalloonGame Array of Balloon objects Balloon is defined for you You have to implement the interface and the game logic Fill the array with balloons Respond to the mouse: find the balloon they clicked on check if it is touching any other balloon recompute the score 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)..  ?? if (b == c)..  ?? if (a.equals(b) )..  ?? if (Arrays.equals(a, b) )..  ?? if (this.arrayEquals(a, b) )..  ?? 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

17 © Peter Andreae COMP 102 25:17 Processing multiple arrays public double[ ][ ] matrixAdd(double[ ][ ] a, double[ ][ ] b){ int rows = a.length; int cols = a[0].length; if (b.length!=rows || b[0].length!=cols) { return null; } double[ ][ ] ans = new double[rows][cols]; for (int row=0; row<rows; row++){ for (int col=0; col<cols; col++){ ans[row][col] = a[row][col] + b[row][col]; } return ans; } 242 454 242 632 514 730 874 968 972 +=

18 © Peter Andreae COMP 102 25:18 Saving a 2D array to a file Write the grade table to a file: A B B+ A- B B+ B- C+ A A- B- B+ Writing the dimensions of the array first will help. Three people with 4 assignments each or Four people with 3 assignments each or Six people with 2 assignments each

19 © Peter Andreae COMP 102 25:19 Saving a 2D array to a file Writing the dimensions in the file helps: public void saveTable( String[][] grades, String fileName){ try{ PrintStream file =new PrintStream(new File(fileName)); int rows = grades.length; int cols = grades[0].length; file.println(rows + " " + cols); for (int row=0; row<rows; row++){ for (int col=0; col<cols; col++){ file.println(grades[row][col]); } }catch(IOException e){UI.println("Table saving failed: "+e);} } As long as each element is a token, could also write each row on one line. Print number of rows and columns on first line of file.

20 © Peter Andreae COMP 102 25:20 Saving a 2D array to a file Alternate design: assume file has been opened elsewhere and passed as argument use "foreach" because not modifying the array. public void saveTable( String[ ][ ] grades, PrintStream file){ file.println(grades.length + " " + grades[0].length); for (String[ ] row : grades){ for (String gr : row){ file.println(gr); } Note, you could pass System.out to the method to make it print to the terminal window! (useful for debugging)

21 © Peter Andreae COMP 102 25:21 Loading 2D array from a file Assume the file has the dimensions as the first two tokens: public String[ ][ ] loadTable( ){ try { Scanner sc = new Scanner(new File(UIFileChooser.open())); int rows =sc.nextInt(); int cols = sc.nextInt(); String[ ][ ] ans = new String[ rows ][ cols ]; for (int row=0; row<rows; row++){ for (int col=0; col<cols; col++){ ans[row][col] = sc.next(); } return ans; } catch(IOException e){UI.out.println("Table loading failed: "+e);} return null; }

22 © Peter Andreae COMP 102 25:22 Loading 2D array from a file Alternate, assuming scanner is passed as argument array is stored in a field public void loadTable(Scanner sc ){ this.dataArray = new String[ sc.nextInt() ] [ sc.nextInt() ]; for (int row=0; row<ans.length; row++){ for (int col=0; col<ans[row].length; col++){ this.dataArray[row][col] = sc.next(); }


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

Similar presentations


Ads by Google