Lecture 24 Interfaces Last time 1 Design patterns Model View Controller 2 Polymorphism 3 Interfaces Today 1 Project 5 is due 10 31 at 11 pm 2 Interfaces cont 3 Wrappers 10 25 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 5 Assigned Project due Tuesday 10 31 at 11 pm Project is closed You must complete the project by yourself Assistance can only be provided by teaching assistants TAs and instructors You must not look at other students code Start now Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly as much of grading is automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Example min max public class MinMax static String min String s String t if s compareTo t 0 return s else return t static String max String s String t if s compareTo t 0 return s else return t Works well for Strings What about other objects e g Date CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 min max Revisited Recall motivating example for interfaces Wanted polymorphic min max routines Needed compareTo method similar to String One argument Return values if if if Can interfaces help Yes Java includes interface Comparable as part of java lang Interface contains one abstract method int compareTo Object o 1 0 1 Return type Argument type int Object We ll see about this in a minute We can use this interface to redefine min max See PolyMinMax java CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Adapting Date to Implement Comparable Must implement compareTo method int compareTo Object o What is Object Type of all possible objects in any class Shortcoming of earlier Java no good way to say same type as this Instead Implementation must take any object An error should be raised if argument is not of correct type How to raise error We can use type casting This is a topic for later in semester CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Implementation of compareTo for Date public class Date implements Printable Comparable public int compareTo Object o Date d Date o if isBefore d return 1 else if d isBefore this return 1 else return 0 Note two interfaces now specified for Date Any number of interfaces are allowed CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Using Polymorphic min max See Driver java for last class Note that same min max methods used on Date String CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 What about int char Polymorphic min max can be used on any class implementing Comparable What about primitive types int char double etc They are not classes They do not implement Comparable Hence min max cannot be used on them CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Wrappers Generic routines can be implemented using interfaces but they are not usable on primitive types To overcome this problem Java provides wrappers for primitive types Wrappers classes whose objects contain single values of the wrapped type Wrappers also contain other useful conversion operations to from String etc Wrappers included in java lang Byte Short Integer Long Float Double Character Boolean CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 The Integer Wrapper The documntation is on line at http java sun com j2se 1 5 0 docs api Notes Constructors Implements Comparable Documentation says Comparable Integer Comparable in Java 5 0 is a generic interface We ll understand this more later Has equals method etc CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Wrappers Allow Use of Max Min What is printed as result of following Integer i new Integer 1 Integer j new Integer 2 System out println PolyMinMax min i j Answer 1 Integer implements Comparable so compareTo exists It also implements toString so System out println prints integer value correctly CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Another Example findMin Wanted operation findMin for finding least String in String array Assumption arrays are non empty Method Store initial array element in temporary variable Iterate through array comparing each element to temporary variable If element is less than temporary variable set temporary variable to it How can we make this method polymorphic See FindMin java for this class CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11
View Full Document