100 likes | 115 Views
Java. The Comparable Interface. Comparable Interface. A class implements the Comparable interface when its objects are expected to be arranged into a particular order.
E N D
Java The Comparable Interface
Comparable Interface • A class implements the Comparable interface when its objects are expected to be arranged into a particular order. • For example, the class String implements Comparable, because strings are often compared with each other and arranged into alphabetical order. • Numeric classes (such as Integer or Double) implement Comparable since number are often sorted into numeric order. • Chapter Topics: • The Comparable Interface • compareTo() method • natural order • collating sequence • This chapter describes the Java 5.0 version of Comparable, which uses generics. Earlier versions of Java are somewhat different. QUESTION 1: Arrange these strings into order: "orange", "apple", "plum".
Answer: "apple", "orange", "plum". compareTo() could be used to make the same arrangement. Comparable Interface • In general, an interface consists of constants and method declarations. • A class that implements an interface must implement each of the methods listed in the interface. • The Comparable interface consists of just one method: • int compareTo( ClassName obj ) // Compare this object with obj. • // Return a negative integer, zero, or a positive integer, • // when this object is less than, equal, or greater than obj. • In the above, " ClassName " stands for the type of the objects. • For example, if the objects are Strings, then " ClassName" is String. • If some objects are instances of a class that implements Comparable, then each object is less than, equal, or greater than any object of that class. • compareTo() returns an integer to show which of these three relations hold.
For example, Only the sign of the returned integer matters if the return value is not zero. The magnitude of a returned integer does not signify anything. QUESTION 2: Examine the following declarations. They use the wrapper class Integer. An Integer object holds an integer as its data, plus provides several useful methods (such as compareTo) for working with integers Integer minusTen = new Integer( -10 ); Integer minusFive = new Integer( -5 ); Integer five = new Integer( 5 ); Integer ten = new Integer( 10 ); Integer fifteen = new Integer( 15 ); What is the result of each of the following? five.compareTo( ten ) _______________ ten.compareTo( five ) _______________ five.compareTo( five ) _______________ ten.compareTo( fifteen ) _______________ minusFive.compareTo( ten ) _______________ minusFive.compareTo( minusTen ) _______________ Negative Positive Zero Negative Negative Positive
Natural Order • Objects that implement compareTo() can be arranged into a natural order. • This order can be visualized as an arrangement of objects from left to right, like on a number line. • If an object A is left of another object B in this ordering, then • objectA.compareTo(objectB) • is negative. For example, X.compareTo("orange") is negative for all the fruit X left of "orange". QUESTION 3: What is "grape".compareTo( "banana" );
Rules for compareTo() Answer: positive • With all objects, compareTo() works the way number comparisons work in ordinary arithmetic. Here are a few rules. Most of these are fairly clear if you think about numbers. Say that A, B, and C are Integers. • If A.compareTo(B) > 0 then B.compareTo(A) < 0. • If A.compareTo(B) > 0 and B.compareTo(C) > 0 then A.compareTo(C) > 0. • If A.compareTo(B) == 0 then A.compareTo(Z) and B.compareTo(Z) should give the same result, no matter what Z is. • The classes that come with Java follow these rules. If you write a class that implements Comparable, you need to follow these rules. This is not hard to do because most sensible compareTo() methods will do this naturally. QUESTION 4: Say that X.compareTo(Y)==0. Is it then true that X.equals(Y)?
Strings!! • All the Rules • Here are all the rules for comparing strings, in one annoying list: • Rule 1: If A.compareTo(B) == 0, then A and B are the same length (counting all characters, including blanks and punctuation) and each character in A is identical (including case) to the character in B at the same location. • Rule 2: Otherwise, if string A is a prefix of string B, then A.compareTo(B) < 0. If B is a prefix of string A, then A.compareTo(B) > 0. • Rule 3: Otherwise, find the first differing pair of characters in the strings A and B. Call them Achar and Bchar. Then A.compareTo(B) is negative if Achar comes before Bchar in the alphabet used by Java (and otherwise is positive). • The rules about what character comes first in the alphabet depend on what country you are in. This is one of the aspects of internationalization, which is the subject of customizing programs for use in different countries. Lets not worry about that
Sample Implementations public int compareTo( SampleClass other ) { return getWord().compareTo( other.getWord() ); }
Pop Quiz • http://chortle.ccsu.edu/java5/Notes/chap53/chap53quiz.html
To Do • Complete the Interface Assignment • Practice AP Test • Elevens Lab • On Thursday: • Practice Exam: Finish Multiple Choice • Start Free Response