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 More 2D arrays COMP 102 #27.

Similar presentations


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

1 Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27 2014 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 later this week Friday: review parts of the test

3 © Peter Andreae COMP 102 27:3 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 +=

4 © Peter Andreae COMP 102 27:4 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 *=

5 © Peter Andreae COMP 102 27:5 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+

6 © Peter Andreae COMP 102 27:6 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.

7 © Peter Andreae COMP 102 27:7 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)

8 © Peter Andreae COMP 102 27:8 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; }

9 © Peter Andreae COMP 102 27:9 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(); }

10 © Peter Andreae COMP 102 27:10 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

11 © Peter Andreae COMP 102 27:11 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 :

12 © Peter Andreae COMP 102 27:12 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!

13 © Peter Andreae COMP 102 27:13 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);} }

14 © Peter Andreae COMP 102 27:14 Assignment 9: Image processing Simple photoshop actions Grey-scale images only (simpler: integer 0-255, instead of Color) Works on jpg or png images, jpg/png images are compressed, so can't easily read the files. We have provided methods to read the images and convert to 2D array of greyscale values. Methods to work on individual values: lighten, contrast Methods to move values around array flip rotate Methods to combine arrays merge, Others: zoom.

15 © Peter Andreae COMP 102 27:15 Assignment 9: Timetable 2D Array of Events (each Event has name and place) display, add, delete. load from and save to file ("sparse" format) find free space Mon 8 TueWedThuFriSatSun 9 10 11 12 13 14 15 16 17

16 © Peter Andreae COMP 102 27:16 Makeup Assignment: GoGame Very simple program One field Constructor buttonPerformed (one button) mousePerformed (one action) redisplay method Builds on BoardGame (A5) to draw a grid board DrawChessboard (lect 24) to draw the stones WordGrid exercise (A9) to select a square Key idea: program from scratch – no template code.

17 © Peter Andreae COMP 102 27:17 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

18 © Peter Andreae COMP 102 27:18 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.

19 © Peter Andreae COMP 102 27:19 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); }

20 © Peter Andreae COMP 102 27:20 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)); }


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

Similar presentations


Ads by Google