Java_Interfaces_2 - CI S3023: Programming Fundamentals for...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CI S3023: Programming Fundamentals for CI S Majors I I Summer 2010 Ganesh Viswanathan I nterfaces (Part I I ) Course Lecture Slides 28 June 2010 A picture is worth a thousand words. An inter face is worth a thousand pictures. -Ben Shneiderman Interface Language construct that specifies functionality without any hint at implementation. An interface contains method specifications but no method implementations. 22 Comparable Interface // Interface defined in java.lang package public interface Comparable { public int compareTo(Object o); } 33 compar eTo( ) Compares this object with the specified object for order. Returns a negative integer, zero or a positive integer when this object is less than, equal to or greater than the specified object. Thr ows: ClassCastException if the specified object's type prevents it from being compared to this Object. Declaring Classes to Implement 44 O bj1 0 55 public class ComparableRectangle extends Rectangle implements Comparable { public ComparableRectangle(double width, double height){ super(width, height); } // Implement the compareTo method defined in Comparable public int compareTo(Object o) { ComparableRectangle cr = (ComparableRectangle)o; if (getArea() > cr.getArea()) return 1; else if(getArea() < cr.getArea()) return -1; else return 0; } } Using Arrays.sort() If you have an array of objects of some class C and you want to use Arrays.sort(Object a) or Arrays.sort(Object a, int fromIndex, int toIndex) to sort this array, you must make class C implement Comparable interface The above sort() methods use the compareTo() method to compare two 66 Arrays.Sort() What if you want to sort this array of objects in order of different attributes of class C at different times? Solution: Use Arrays. sor t (Object a, Comparator c) 77 Comparator Interface public interface Compar ator { public int compar e (Object o1, Object o2) } Retur ns: a negative integer , zer o , or a positive integer when the first argument is less than , equal to , or greater than the 88 Solution Create a class that implements the Comparator interface Code the compare() method to define the ordering between objects of this class Pass an instance of this class to the Arrays.Sort() method 99 Another scenario...
View Full Document

This note was uploaded on 05/27/2011 for the course CIS 3023 taught by Professor Dobbins during the Summer '08 term at University of Florida.

Page1 / 32

Java_Interfaces_2 - CI S3023: Programming Fundamentals for...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online