DOC PREVIEW
UMD CMSC 131 - Lecture 24: Interfaces

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

10/25/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 24:InterfacesLast time:1. Design patterns: Model-View-Controller2. Polymorphism3. InterfacesToday:1. Project #5 is due 10/31 at 11 pm2. Interfaces (cont.)3. WrappersCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Project #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 automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Example: min / maxpublic class MinMax {static String min (String s, String t){if (s.compareTo(t) <= 0)return s;elsereturn 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 2006Rance Cleaveland©2006 University of Maryland3min / max Revisited Recall motivating example for interfaces Wanted polymorphic min / max routines  Needed compareTo method similar to String One argument Return values -1 if < 0 if == 1 if > Can interfaces help? Yes! Java includes interface Comparable as part of java.lang Interface contains one abstract method:int compareTo(Object o) Return type: int Argument type: Object (We’ll see about this in a minute) We can use this interface to redefine min / max!See PolyMinMax.javaCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Adapting 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 semesterCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Implementation of compareTofor Datepublic 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 2006Rance Cleaveland©2006 University of Maryland6Using Polymorphic min / max See Driver.java for last class Note that same min / max methods used on Date, StringCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7What 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 themCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Wrappers 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 BooleanCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9The 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 2006Rance Cleaveland©2006 University of Maryland10Wrappers 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 compareToexists It also implements toString, so System.out.println prints integer value correctlyCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Another 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


View Full Document

UMD CMSC 131 - Lecture 24: Interfaces

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 24: Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 24: Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 24: Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?