Interface Classes

Only available on StudyMode
  • Download(s) : 103
  • Published : April 25, 2013
Open Document
Text Preview
Outline

COMP 102 26/27:2

Designing with Interfaces
#26/#27 COMP 102 #26/#27 2012

• • • • •

Assig 9 Doing without the UI class Multiple types Interface classes Implementing Interface classes.

Peter Andreae
Computer Science Victoria University of Wellington

Reading: Reading
• Textbook • 7.6: 2D arrays • 9.3: Interfaces (note, textbook also discusses inheritance)

Copyright: Peter Andreae, Victoria University of Wellington
© Peter Andreae

ImageProcessor
• Fade
180 255 60 0

COMP 102 26/27:3

Assignment 9: Motel Booker
• • • •
0 1 2 3 4 5 6 7 8

COMP 102 26/27:4

• 2D Array of Bookings (each Booking has name and phone)
display, add, delete. load from and save to file find free space Mon Tue Wed Thu Fri Sat Sun

255 30

© Peter Andreae

© Peter Andreae

Types again
• Only values of the specified type can be put into a place

COMP 102 26/27:9

Object types
• All object values have two types:
• the type they were created as • the Object type

COMP 102 26/27:10

Variables, fields, array elements are all defined with a type 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: Every class • Arrays : int[ ], double[ ][ ], Balloon[ ], defines a type • 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

• You can have a place of type Object:
Object value = new Balloon(100, 100); value = "not any more";

There are limits on what you can do with a place of type Object: value.pop(); value.toUpperCase();
Won't work if value contains a String Won't work if value contains a Balloon

You can only call methods on the value in a variable/field if the methods belong to the type of the variable/field variable/field. value.toString()
© Peter Andreae

© Peter Andreae

Collections of different types
• Extending the Balloon Game:

COMP 102 26/27:11

New Balloon Game
BalloonGame array of balloons

COMP 102 26/27:12

• BalloonGame needs an array to hold all the balloons

• 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 BalloonGame field with an array of balloons

RoundBalloon

BombBalloon

LongBalloon

BouncyBalloon

expand() on(int x, int y) pop() RoundBalloon BombBalloon LongBalloon BouncyBalloon

expand() on(int x, int y) pop()

expand() on(int x, int y) pop()

expand() on(int x, int y) pop()

• Each class of balloons needs the same methods:
• expand, on, pop • but, they each behave differently.

© Peter Andreae

© Peter Andreae

BalloonGame

COMP 102 26/27:13

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){….. }

COMP 102 26/27:14

Can use Object public class BalloonGame { : private int maxBalloons = 20; Object Object [maxBalloons]; [ ] balloons = new private null
RoundBalloon-2 BombBalloon-5 LongBalloon-8

null
BouncyBalloon-3

RoundBalloon-7

public void doStep(int x, int y){ for (int i = 0; i
tracking img