Download presentation
Presentation is loading. Please wait.
Published byLindsey Merritt Modified over 9 years ago
1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27 2015 T1
2
© Peter Andreae COMP 102 27:2 Menu 2D arrays The assignment questions System.out and doing without the UI class. Administration Test returned on Wednesday this week Tutorial on Tuesday 4-5 AM 104: go over some test questions.
3
© Peter Andreae COMP 102 27:3 Moving value in a 2D array public void moveUp(ChessPiece[ ][ ] board, int row, int col){ if (row > 0 && row < board.length && col >= 0 && col < board[row].length && board[row][col] != null && board[row-1][col] == null){ board[row-1][col] = board[row][col]; board[row][col] = null;♗♖♘♙♚♛♜♝♞♟ } ♖ ♔♘ ♗ ♙♜ ♝ ♚ ♜ 12 3 45670
4
© Peter Andreae COMP 102 27:4 Moving all values in a 2D array public void flipBoard(ChessPiece[ ][ ] board){ int rows = board.length; int cols = board[0].length; for (int row=0; row<rows/2; row++){ for (int col=0; col<cols; col++) { ChessPiece temp = board[row][col]; board[row][col] = board[rows-row-1][col]; board[rows-row][col] = temp; }} } ♖ ♔♘ ♗ ♙♜ ♝ ♚ ♜ ♜ ♚ ♝ ♙♜ ♗ ♔♘ ♖ 12 3 45670 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0
5
© Peter Andreae COMP 102 27:5 Rotating (cw) all values in a 2D array public void rotateBoard(ChessPiece[ ][ ] board){ int rows = board.length; int cols = board[0].length; ChessPiece[][] temp= new ChessPiece[rows][cols]; for (int row=0; row<rows; row++){ for (int col=0; col<cols; col++) { temp[row][col] = board[row][col]; }} for (int row=0; row<rows; row++){ for (int col=0; col<cols; col++) { int srow = cols-col-1; int scol = row; board[row][col] = temp[srow][scol]; }} } temp is a temporary copy of the board ♖ ♔♘ ♗ ♙♜ ♝ ♚ ♜ ♗ ♜ ♝ ♔♖ ♙ ♚ ♜♘ 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 12 3 45670 90º
6
© Peter Andreae COMP 102 27:6 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 +=
7
© Peter Andreae COMP 102 27:7 Processing multiple arrays public double[ ][ ] matrixMultiply(double[ ][ ] a, double[ ][ ] b){ int rows = a.length; int cols = b[0].length; if (cols != b.length) { return null; } double[ ][ ] ans = new double[rows][cols]; for (int row=0; row<rows; row++){ for (int col=0; col<cols; col++){ for (int i=0; i<b.length; i++){ ans[row][col] = ans[row][col]+a[row][ i ] * b[ i ][col]; } } } return ans; } 242 454 242 632 514 730 12+20+146+4+64+16+0 24+25+2812+5+128+20+0 12+4+146+4+64+16+0 *=
8
© Peter Andreae COMP 102 27:8 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 ABB+A- BB+B-C+ AA-B-B+ AB A-BB+ B-C+A A-B-B+ AB A- AB+ B-C+ AA- B-B+
9
© Peter Andreae COMP 102 27:9 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.
10
© Peter Andreae COMP 102 27:10 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 grd : row){ file.println(grd); } Note, you could pass System.out to the method to make it print to the terminal window! (useful for debugging)
11
© Peter Andreae COMP 102 27:11 Loading 2D array from a file Assume first two tokens of file are the dimensions: 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; }
12
© Peter Andreae COMP 102 27:12 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<dataArray.length; row++){ for (int col=0; col<dataArray[row].length; col++){ this.dataArray[row][col] = sc.next(); }
13
© Peter Andreae COMP 102 27:13 Suppose the array has only a few entries and many nulls ie, a "sparse" array Jan 0 1 2 3 4 5 lects 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Another file format for 2D arrays T1 lects Study FebMarAprMayJunJulAug lects SepOctNovDec Exam end T3T2 end ExGrad Study Break Better to save just the non-null entries
14
© Peter Andreae COMP 102 27:14 File format size (months, days) month day entry To load: read size and create calendar array read month, day, entry, and assign entry to calendar[month][day] To save: print size for each non-null entry, print month, day, entry 12 32 0 5 lects 1 11 end T3 2 1 T2 3 18 break :
15
© Peter Andreae COMP 102 27:15 Load sparse array public void loadSparse(){ try{ Scanner sc = new Scanner( new File(UIFileChooser.open())); int months= sc.nextInt(); int days = sc.nextInt(); this.calendar = new String[months][days]; while (sc.hasNext()){ int month = sc.nextInt(); int day = sc.nextInt() String entry = sc.next(); this.calendar[month][dayl] = entry; } sc.close(); } catch (IOException e){UI.println("Fail: " + e);} } Don't know how many entries there will be!
16
© Peter Andreae COMP 102 27:16 Save sparse array public void saveSparse(){ try{ PrintStream ps = new PrintStream(new File( UIFileChooser.save() )); ps.println(this.calendar.length+" "+ this.calendar[0].length); for (int month = 0 ; month < this.calendar.length; month++){ for (int day = 0 ; day< this.calendar[month].length; day++){ if (this.calendar[month][dayl] != null){ ps.println(month+" "+day+" "+this.calendar[month][dayl]); } sc.close(); } catch (IOException e){UI.println("Fail: " + e);} }
17
© Peter Andreae COMP 102 27:17 Makeup Assignment: GoGame Very simple program One field Constructor One button One Mouse action redisplay method Builds on Pattern exercise (A5) to draw a grid board DrawChessboard (lect 25) to draw the stones WordGrid exercise (A9) to select a square Key idea: program from scratch – no template code.
18
© Peter Andreae COMP 102 27:18 Doing without UI & ecs100 library #1 UI class lets you print and read from the text pane What do you do without the UI? Output: System.out is the "terminal window". You can print/println/printf to System.out, just like UI System.out.println("Here is a message for you"); Input: System.in is the keyboard via the "terminal window" You have to wrap it with a Scanner to read: Scanner userInput= new Scanner(System.in); : System.out.print("Enter age: "); int age = userInput.nextInt(); No "ask…()" methods
19
© Peter Andreae COMP 102 27:19 Doing without UI & comp102 library #2 How do you make a window with a text area, and buttons? Have to make a JFrame object and set its size Make a JTextArea object and add to frame Make a JPanel and add to frame Make JButtons and add to the panel Make the frame visible Make a buttonPerformed method Writing to the text area is different.
20
© Peter Andreae COMP 102 27:20 DemoInterface import javax.swing.*; import java.awt.event.*; import java.awt.BorderLayout; public class DemoInterface implements ActionListener{ private JFrame frame = new JFrame("Demo program"); private JTextArea textOut = new JTextArea(30, 30); public DemoInterface () { this.frame.setSize(600, 400); this.frame.add(this.textOut, BorderLayout.CENTER); JPanel panel = new JPanel(); this.frame.add(panel, BorderLayout.SOUTH); JButton button1 = new JButton("Doit"); button1.addListener(this); panel.add(button1); JButton button2 = new JButton("Clear"); button2.addListener(this); panel.add(button2); this.frame.setVisible(true); }
21
© Peter Andreae COMP 102 27:21 DemoInterface public void actionPerformed(ActionEvent e) { String button = e.getAction(); if (button.equals("Clear")){ this.textOut.setText(""); } else if (button.equals("Doit")){ for (int i = 1; i <=10; i++){ this.textOut.append("square of " + i+ " is " + (i*i)); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.