Download presentation
Presentation is loading. Please wait.
Published byRoderick Wade Modified over 9 years ago
1
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces COMP 102 #30 2014T2
2
© Xiaoying Gao, Peter Andreae COMP 102 30:2 Outline More examples of Interface classes UIButtonListener revisited Optional: GUI without ecs100 library Assignment 9 The model solutions for exercise questions are handed out
3
©Xiaoying Gao, Peter Andreae COMP 102 29:3 DiskGame class structure RoundDisk damage explode … Disk FatDisk damage explode … BombDisk damage explode … BouncyDisk damage explode … DiskGame User Interface Array of Disk
4
© Peter Andreae COMP 102 29:4 A Disk Interface class. public interface Disk { public void damage(); public boolean on(int x, int y); public void explode(); public double score(); public boolean withinRange(Disk other); } Declares a type that you can use for fields,variables, arrays: public class DiskGame { private int maxDisks = 20; private Disk [ ] disks = new Disk [maxDisks]; public void hitDisk(int x, int y){ for (int i = 0; i<disks.length; i++){ Disk disk = this.disks[i]; if (disk != null && disk.on(x, y) ) { disk.damage(); Java will NOT complain! Disks DO have these methods Note the ; instead of { …. }
5
© Peter Andreae COMP 102 29:5 More classes implementing Disk public class FatDisk implements Disk { private int x, y; private int layers = 5; public void damage( ){ …… } public boolean on(int x, int y){ …… } public void explode() { …… } public double score() { …… } public boolean withinRange(Disk other) { …… } } public class BombDisk implements Disk { private int x, y, maxRad; private int radius = 10; public void damage(){ …… } public boolean on(int x, int y){ …… } public void explode() { …… } public double score() { …… } public boolean withinRange(Disk other) { …… } }
6
© Xiaoying Gao, Peter Andreae COMP 102 30:6 Interface class summary If you define an interface class: You have defined a type You can declare variables (or arrays) to have that type. You cannot make an object of the interface class new Disk(…) // NOT ALLOWED (there is no constructor) Objects from classes implementing the interface are of that type. You can call any of the specified methods on values in those variables
7
© Xiaoying Gao, Peter Andreae COMP 102 30:7 Interface example public interface Tutor { public void printPayment(); } public class TutorAdmin{ private ArrayList tutors = new ArrayList (); public TutorAdmin(){ this.tutors.add(new SeniorTutor("Alan")); this.tutors.add(new StudentTutor("Sam", 5)); this.tutors.add(new StudentTutor("Tom", 7)); this.tutors.add(new SeniorTutor("Bob")); for(Tutor t: tutors) t.printPayment(); }
8
© Xiaoying Gao, Peter Andreae COMP 102 30:8 The SeniorTutor and StudentTutor class public class SeniorTutor implements Tutor{ private String name; private double salary = 2000; public SeniorTutor(String c){ this.name = c; } public void printPayment(){ UI.println("pay fortnightly: $" +salary); } public class StudentTutor implementsTutor { private String name; private double hours; public StudentTutor(String s, double h){ this.name = s; this.hours = h; } public void printPayment(){ UI.println("pay for "+ this.hours+ ": $"+ this.hours*20); } public void addHours(double n){ this.hours = this.hours + n; }
9
© Xiaoying Gao, Peter Andreae COMP 102 30:9 Summary of Types in Java Predefined, primitive types (not objects) boolean, int, double, float, char,… Object types String, Scanner, PrintStream, Color, …. Flower, Rectangle, …. Each defined by a class. Arrays int[ ], double[ ], String[ ], Shape [ ] … Array types do not have a name They are object types (you only have a reference to them) Interface types Disk, Tutor, UIButtonListener, UIMouseListener, ButtonListener, … interface type may encompass several different object types
10
© Xiaoying Gao, Peter Andreae COMP 102 30:10 Interfaces: More examples We have used interfaces before! public class Plotter implements UIButtonListener{ public Plotter(){ UI.addButton(“Read”, this); UI.addButton(“Plot”, this); UI.addButton(“Stats”, this); } Read Plot Stats Button-7 name: aListener: : “ Read ” Button-11 name: aListener: : “ Plot ” Button-8 name: aListener: : “ Stats ” Plotter-54 numbers: count: public void buttonPerformed( …
11
© Xiaoying Gao, Peter Andreae COMP 102 30:11 UIButtonListener is an Interface Every button object contains a field of type UIButtonListener UIButtonListener specifies one method: public interface UIButtonListener{ public void buttonPerformed(String button); } When a button is pressed. the JVM looks for the object in the button’s buttonListener field It then calls the buttonPerformed method on the object passing the name of the button. It can only do this because the object is an UIButtonListener. (and this is the only thing it can do on the object)
12
© Xiaoying Gao, Peter Andreae COMP 102 30:12 Interfaces: More examples The listener can be a Plotter, or a Genealogy, or a … as long as it is an UIButtonListener DoB Parents Children Button-18 name: aListener: : “ DoB ” Button-21 name: aListener: : “ Parents ” Button-5 name: aListener: : “ Children ” Genealogy-2 persons: public void bottonPerformed( … Quit Button-5 name: aListener: : “ Quit ”
13
©Xiaoying Gao, Peter Andreae COMP 102 30:13 Standard library (without ecs100 library) MouseListener is an interface public interface MouseListener { public void mouseClicked(MouseEvent e); public void mousePressed(MouseEvent e); public void mouseReleased(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseExited(MouseEvent e); } ActionListener is an interface public interface ActionListener{ public void actionPerformed(ActionEvent e); }
14
©Xiaoying Gao, Peter Andreae COMP 102 30:14 Doing without UI & ecs100 library How do you make a window with a text area, and buttons? 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 actionPerformed method Writing to the text area is different.
15
©Xiaoying Gao, Peter Andreae COMP 102 30:15 Demo Interface 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); }
16
©Xiaoying Gao, Peter Andreae COMP 102 30:16 Demo Interface 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)); }
17
©Xiaoying Gao, Peter Andreae COMP 102 30: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
© Xiaoying Gao, Peter Andreae COMP 102 30:18 Designing MiniDraw (Assig 10) A simple drawing editor Allows user to create drawings consisting of shapes: rectangles, ovals, lines, dots, trees, different colours. can add a new shape, delete a shape move a shape to a different position resize a shape save the current drawing to a file load a previous drawing from a file and edit it further.
19
© Xiaoying Gao, Peter Andreae COMP 102 30:19 Designing MiniDraw What Types of Data? Program must remember all the shapes so it can edit them ⇒ Rectangles, Lines, Ovals, Dots, Trees ⇒ List of the shapes (The user interface) (Contains an arrayof shapes) MiniDraw LineOvalDotTreeRectangle
20
© Xiaoying Gao, Peter Andreae COMP 102 30:20 Designing MiniDraw What Methods? MiniDraw Interaction fields Array of shapes Rectangle redraw moveBy : Same methods, but not the same effects. (eg, will draw themselves differently) Need different fields to store different information Line redraw moveBy : Oval redraw moveBy : Dot redraw moveBy : Tree redraw moveBy :
21
© Xiaoying Gao, Peter Andreae COMP 102 30:21 Array of shapes Can't be an array of Line Can't be an array of Object (well…, yucky if we do) Need an interface class for the Shape type public Interface Shape{ public void redraw(); public void moveBy(double dx, double dy); public boolean on(double x, double y); public String toString(); : } Can use Shape as a type. Lines, Rectangles, etc must be Shapes also
22
© Xiaoying Gao, Peter Andreae COMP 102 30:22 Implementing the Shape Interface If you define classes that implement the interface: public class Rectangle implements Shape{ private int x; …… public class TextField implements Shape{ private String text; …… These classes must define all the specified methods (including the right parameters)
23
© Xiaoying Gao, Peter Andreae COMP 102 30:23 MiniDraw class diagram With the Shape Interface: Rectangle render moveBy … Shape Line render moveBy … Oval render moveBy … Dot render moveBy … Tree render moveBy … MiniDraw User Interface Array of Shape
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.