15-100 Lecture 17 (Wednesday, October 12, 2005)

Quick Quiz

Please make the following class (fragment) Comparable based on the total value of the money:

```  class Wallet implements Comparable {
private int ones;
private int tens;
private int twenties;

...

public int compareTo (Object o) {
Wallet w = (Comparable) w;

int thisAmount = 20*twenties + 10*tens + 5*fives + ones;
int otherAmount = 20*w.twenties + 10*w.tens + 5*w.fives + w.ones;

return (thisAmount - otherAmount);
}

}
```

The Comparable GreetingCard

Last class we produced a partial implementation of a GreetingCard. We made this class implement the Comparable interface. Let's quickly review the class definition:

```  public class GreetingCard implements Comparable {
private String occasion;
private String message;
private int length;
private int width;

...

public int compareTo (Object o) {
GreetingCard g = (GreetingCard) o;

return ( (length*width) - (g.length*g.width) );
}

// Please write a method that will determine if two GreetingCards
// are equal. It should follow the conventions for such a method
public boolean equals (Object o) {
GreetingCard g = (GreetingCard) o;

if (!occasion.equals(g.occasion))
return false;

if (!message.equals(g.message))
return false;

if (length != b.length)
return false;

if (width != b.width)
return false;

return true;

}

}
```

Notice the "implements Comparable" clause. This is what ties this GreetingCard to the requirements set for within the Comparable interface. This clause asks Java to verify that the GreetingCard does, in fact, satisfy those requirements.

The Comparable interface, deja vu

So, what are the requirements imposed by the Comparable interface? Let's review the interface specification for Comparable. Recall that we didn't write this interface specification. It was written by the kind folks at Sun and provided as part of the Java enviornment.

```  interface Comparable {
public int compareTo (Object o);
}
```

From this we see that Comparable is a fairly light-weight interface. It imposes only one requirement -- the compareTo() method. Specifically, a compareTo method has the signature:

• public int compareTo (Object o);

Using Comparables

Great. So, we have a way of enforcing requirements upon a class. This is nice. But, how is it useful? Let's take this one a single step at a time. First, let's take a look at the mechanism. Then, let's consider why it is important.

So far, we've made use of "class reference variables". These reference variables identify an object -- and are limited to identifying objects defined by a specific class. We can also make use of "interface reference variables". These variables are limited to containing references to objects that implement a specific interface, regardless of the class.

So, consider the following:

```  Comparable c = new GreetingCard ("Thanksgiving", "Cluck, cluck, cluck", 6, 8);
```

Notice that, above, we have a "Comparable" reference identifying a GreetingCard. This is useful because it allows us to write code that manipulates Objects, without unnecessarily limiting their types. In the case above, we can write code, such as that to sort a list of things, and limit the type only to those things that are Comparable. The same code will be useful for anything Comparable. We won't need to rewrite it.

Next class, we'll take a look at a specific example of this.