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

Slides:



Advertisements
Similar presentations
MiniDraw Testing COMP 102 # T1
Advertisements

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Arrays COMP 102.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: File Output; Reference Semantics reading: , 7.1, 4.3, 3.3 self-checks:
2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington User Interface COMP 112 #30.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Patterns with.
2011-T1 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington “For each” & Patterns with.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Creating a GUI Class An example of class design using inheritance and interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
2011-T1 Lecture 12 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Dealing with Files COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input COMP.
Files The UI text pane window is transient:
Outline ArrayLists standard for : shorthand version of while Admin:
Writing to a File Open a File Wrap it in a new PrintStream object.
Introduction to Computing Using Java
Presentation transcript:

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

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

© Peter Andreae COMP :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;♗♖♘♙♚♛♜♝♞♟ } ♖ ♔♘ ♗ ♙♜ ♝ ♚ ♜

© Peter Andreae COMP :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; }} } ♖ ♔♘ ♗ ♙♜ ♝ ♚ ♜ ♜ ♚ ♝ ♙♜ ♗ ♔♘ ♖

© Peter Andreae COMP :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 ♖ ♔♘ ♗ ♙♜ ♝ ♚ ♜ ♗ ♜ ♝ ♔♖ ♙ ♚ ♜♘ º

© Peter Andreae COMP :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; } =

© Peter Andreae COMP :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; } *=

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

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

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

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

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

© Peter Andreae COMP :13 Suppose the array has only a few entries and many nulls ie, a "sparse" array Jan lects 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

© Peter Andreae COMP :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 lects 1 11 end T3 2 1 T break :

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

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

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

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

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

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

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