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 Types and Interfaces COMP.

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP."— Presentation transcript:

1 Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP 112 #28 2015

2 © Peter Andreae COMP 112 28:2 Outline Multiple types Interface classes Implementing Interface classes. Admin: Last assignment on Wednesday Tutorial: Tues 4-5

3 © Peter Andreae COMP 112 28:3 BalloonGame Simple game Collection of Balloons Clicking on a Balloon expands it If two Balloons touch, they pop. Score = total area of live balloons minus area of popped Balloons. Key data structure: Like DominoGame and Genealogy: needs a list of Balloon objects: ArrayList

4 © Peter Andreae COMP 112 28:4 BalloonGame

5 © Peter Andreae COMP 112 28:5 Design of BalloonGame

6 © Peter Andreae COMP 112 28:6 Types and Interface Variables, fields, array elements are all defined with a type Only values of the specified type can be put into a place What types are there primitive types: int, double, boolean, long, float, char,.. note: java will "coerce" some types into other types: double number = 4; Object types: Arrays : int[ ], double[ ][ ], Balloon[ ], Predefined: String, Color, File, Scanner, … Programmer defined: CartoonFigure, Balloon, BalloonGame, … Type of object determined at its creation: new Balloon(100, 300) ⇒ a Balloon object new File ("data.txt") ⇒ a File object new int[100][100] ⇒ an array of arrays of int. "Hello" ⇒ a String Every class defines a type

7 © Peter Andreae COMP 112 28:7 Object types All object values have two types: the type they were created as the Object type You can have a place of type Object: Object value = new Balloon(100, 100); value = "not any more"; ArrayList things = new ArrayList (); things.add("more", new Balloon(100, 100), new Double(4.5)); for (Object thing : things){ UI.println(thing.toString()); System.out.println(thing); }

8 © Peter Andreae COMP 112 28:8 Object types There are limits on what you can do with a place of type Object: value.pop(); value.toUpperCase(); for (Object thing : things){ if (thing.startsWith(“Ball”){ System.out.println(thing); } } You can only call methods on the value in a variable/field if the methods belong to the type of the variable/field. value.toString() Won't work if value contains a String Won't work if value contains a Balloon

9 © Peter Andreae COMP 112 28:9 Collections of different types Extending the Balloon Game: several kinds of balloons: ordinary balloons – expand uniformly, and pop on touching bomb balloons – blow up, taking out lots of balloons at max size long balloons – expand into sausage shapes, popping other balloons only, up to limit bouncy balloons – don't expand, but bounce around and move other balloons T

10 © Peter Andreae COMP 112 28:10 New Balloon Game BalloonGame needs an array to hold all the balloons Each class of balloons needs the same methods: expand, on, pop but, they each behave differently. BalloonGame array of balloons RoundBalloon expand() on(int x, int y) pop() BombBalloon expand() on(int x, int y) pop() LongBalloon expand() on(int x, int y) pop() BouncyBalloon expand() on(int x, int y) pop()

11 © Peter Andreae COMP 112 28:11 BalloonGame public class BalloonGame { : private int maxBalloons = 20; private [ ] balloons = new [maxBalloons]; public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ if (balloons[i] != null && this.balloons[i].on(x, y) ) { this.balloons[i].expand(); : RoundBalloon-2BombBalloon-5 LongBalloon-8 BouncyBalloon-3 RoundBalloon-7 null Java will complain! No such method for Object Java compiler will complain! No such method for Object Can use Object Object

12 © Peter Andreae COMP 112 28:12 Different Balloon classes public class RoundBalloon { private int x, y; private int radius = 10; private Color col; public void expand(int amt){….. public boolean on(int x, int y){….. } public class LongBalloon { private int x, y; private int length = 10; private double direction = 1.26; private Color baseCol, tubeCol; public void expand(int amt){….. public boolean on(int x, int y){….. }

13 © Peter Andreae COMP 112 28:13 "Generic" values with Object public class BalloonGame { : private int maxBalloons = 20; private Object [ ] balloons = new Object [maxBalloons]; public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ Object b = this.balloons[i]; RoundBalloon rb = (RoundBalloon) b; if (rb != null && rb.on(x, y) ) { rb.expand(); RoundBalloon-2BombBalloon-5 LongBalloon-8 BouncyBalloon-3 RoundBalloon-7 null Cast it to a RoundBalloon Now compiler is happy But if it is a BombBalloon or … the program will fall over!

14 © Peter Andreae COMP 112 28:14 Casting Can sometimes convert a value from one type to another numbers: int col = (int) (x /cellWidth); double x = 100 * col; Must be an explicit cast if information might be lost. anything to a string: (by adding to a string) "value is " + x For an Object value, it automatically calls the toString() method "value is " + x.toString() any object value from one of its types to another Object balloon = (Object) ( new RoundBalloon(34, 45) ); RoundBalloon rb = (RoundBalloon) balloon; object must be of that type, otherwise a runtime error

15 © Peter Andreae COMP 112 28:15 "Generic" values with Object public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ Object b = this.balloons[i]; if (b == null) { continue; } else if (b instanceof RoundBalloon) { RoundBalloon rb = (RoundBalloon) b; if (rb.on(x, y) ) { rb.expand();…..} } else if (b instanceof LongBalloon) { LongBalloon lb = (LongBalloon) b; if (lb.on(x, y) ) { lb.expand();…..} } … RoundBalloon-2BombBalloon-5 LongBalloon-8 BouncyBalloon-3 RoundBalloon-7 null Not nice code!

16 © Peter Andreae COMP 112 28:16 A Balloon type RoundBalloon, LongBalloon, BombBalloon, BouncyBalloon are all kinds of Balloon. ⇒ should all be of a Balloon type. RoundBalloon-2 should be a a RoundBalloon, a Balloon, and an Object public class BalloonGame { private int maxBalloons = 20; private Balloon [ ] balloons = new Balloon [maxBalloons]; public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ Balloon b = this.balloons[i]; if (b != null && b.on(x, y) ) { b.expand();

17 © Peter Andreae COMP 112 28:17 Making a Balloon Type Problem: What is "a Balloon" ? Answer 1: Something you can expand, public void expand() ask if a point is on it,public boolean on(int x, int y) poppublic void pop() ask for its sizepublic double size() ask if touching a balloon public boolean touches(Balloon other) Answer 2: a RoundBalloon, or a LongBalloon, or a BombBalloon, or a BouncyBalloon. We need to make both answers true.

18 © Peter Andreae COMP 112 28:18 Interface classes An interface class describes a supertype of other types. It specifies a name for the type the headers for the methods that any object of the type can respond to. constants (public static final double GRAVITY = 9.8) nothing else! (no fields, constructors, or method bodies) public interface Balloon { method headers constants } You cannot make a new instance of an interface. Balloon b = new Balloon(); An interface just says "what"; not "how" Latest version of Java has extensions

19 © Peter Andreae COMP 112 28:19 A Balloon Interface class. public interface Balloon { public void expand(); public boolean on(int x, int y); public void pop(); public double size(); public boolean touches(Balloon other); } Declares a type that you can use for fields,variables, arrays: public class BalloonGame { private int maxBalloons = 20; private Balloon [ ] balloons = new Balloon [maxBalloons]; public void doStep(int x, int y){ for (int i = 0; i<balloons.length; i++){ Balloon b = this.balloons[i]; if (b != null && b.on(x, y) ) { b.expand(); Java will NOT complain! Balloons DO have these methods Note the ; instead of { …. }

20 © Peter Andreae COMP 112 28:20 Making a Balloon Type Problem: What is "a Balloon" ? Answer 1: Defined by interface Balloon { Something you can expand, ask if a point is on it, pop ask for its size ask if touching a balloon Answer 2: a RoundBalloon, or a LongBalloon, or a BombBalloon, or a BouncyBalloon. We need to make both answers true.

21 © Peter Andreae COMP 112 28:21 Making classes have another type If you define some class, all instances are automatically of type Object also. To make instances be of some interface type: declare that the class implements the interface type: make sure that the class defines all the methods specified by the interface type: public class RoundBalloon implements Balloon { private int x, y;private int radius = 10; private Color col; public void expand(int amt){ …… } public boolean on(int x, int y){ …… } public void pop() { …… } public double size() { …… } public boolean touches(Balloon other) { …… } } Must provide method bodies, not just headers! implements is a “ promise ” that these objects will be of the interface type. ie, will have all the methods.

22 © Peter Andreae COMP 112 28:22 More classes implementing Balloon public class LongBalloon implements Balloon { private int x, y;private int length = 10; private double dir = 1.26; private Color baseCol, tubeCol; public void expand(int amt){ …… } public boolean on(int x, int y){ …… } public void pop() { …… } public double size() { …… } public boolean touches(Balloon other) { …… } } public class BombBalloon implements Balloon { private int x, y, maxRad;private int radius = 10; private Color col; public void expand(int amt){ …… } public boolean on(int x, int y){ …… } public void pop() { …… } public double size() { …… } public boolean touches(Balloon other) { …… } }

23 © Peter Andreae COMP 112 28:23 Making a Balloon Type Problem: What is "a Balloon" ? Answer 1: Defined by interface Balloon { Something you can expand, ask if a point is on it, pop ask for its size ask if touching a balloon Answer 2: Specified by … implements Balloon a RoundBalloon, or a LongBalloon, or a BombBalloon, or a BouncyBalloon. We have now made both answers true.

24 © Peter Andreae COMP 112 28:24 BalloonGame class structure RoundBalloon expand pop … Balloon LongBalloon expand pop … BombBalloon expand pop … BouncyBalloon expand pop … BalloonGame User Interface Array of Balloon

25 © Peter Andreae COMP 112 28:25 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 Balloon(…) // 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 fields/variables of that type. When defining an interface class: You should include headers of methods You may include static final constants You may not include fields, constructors, or method bodies.


Download ppt "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP."

Similar presentations


Ads by Google