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