Return to lecture notes index

15-100 Lecture 17 (Monday, October 10, 2005)

Quick Quiz

  public class GreetingCard {
    private String occasion;
    private String message;
    private int length;
    private int 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;

    }
    
  }
  

Grader's notes:

Interfaces and Implementing Them

Remember the interface specification from the Calculator class?

Let's do a quick review...

In Java, an interface specification, like a class specification, is a document. Unlike a class specification, it doesn't describe the behaviors of a class of Object. Instead, it provides a list of required behaviors. By "implementing" a standard, the designer of a class agrees to create matching methods -- and asks the compiler to ensure that the class specification actually satisfies the interface's requirements.

Interfaces are like industry standards. The communications industry has one standard so that all TV components can interact with or be connected to each other (it would be really annoying if a new DVD player could only connect with one brand of TV). By implementing an interface, a class desinger enables instances to be manipulated using not only "class reference variables", but also "instance reference variables". As a result, it can be manipulated through methods, by other objects, without the real type being known in advance -- and even if the real type was defined after-the-fact.

The Comparable Interface

Java defines an interface, Comparable, as follows:


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

This interface provides a uniform way to Compare implementing Objects, known as Comparables. An Object implements Comparable by doing the following:

An Example Comparable GreetingCard

Let's take the partial example from today's quiz and make it Comparable. In particular, let's compare GreetingCards based on their area (length * width):

  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;

    }

  }
  

Next Class

...we'll take a look at another Comparable object or two and also learn to make use of Comparables.