A stamp has a descriptive name, a face value, and an issuing country. Stamps have a measurable value (the face value), hence it makes sense for the Stamp class to implement the Measurable interface from Chapter 9. You can also compare two stamps to find which has higher value, so it makes sense to implement the Comparable interface from the standard Java library as well.

In fact, any two measurable objects can be compared, simply by comparing their measures. We can implement the compareTo method of the Comparable interface in a class AbstractMeasurable that implements both the Comparable and Measurable interfaces. Then the Stamp class simply extends that class.

Complete the following file:

AbstractMeasurable.java

/** Describes any class whose objects can be measured, and adds a comparison. */ public abstract class AbstractMeasurable implements ..., ... { /** Computes the measure of the object. @return the measure */ public abstract double getMeasure(); /** Compares this measurable object against another. @param otherObject another measurable object @return -1 if this object measures less than the other, 0 if they measure the same, 1 otherwise. */ public int compareTo(Object otherObject) { ... } }

Use the following files:

Measurable.java

/**
   Describes any class whose objects can be measured.
*/
public interface Measurable
{
   /**
      Computes the measure of the object.
      @return the measure
   */
   double getMeasure();
}

Stamp.java

/**
   Stamp class represents a single postage stamp.
*/
public class Stamp extends AbstractMeasurable
{
   private String description;
   private double faceValue;
   private String issuingCountry;

   public Stamp(String aDesc, double aValue, String aCountry)
   {
      description = aDesc;
      faceValue = aValue;
      issuingCountry = aCountry;
   }

   public double getValue()
   {
      return faceValue;
   }
   
   public String getCountry()
   {
      return issuingCountry;
   }

   public String getDescription()
   {
      return description;
   }

   public double getMeasure()
   {
      return faceValue;  
   }
}

StampTester.java

public class StampTester
{
   public static void main(String[] args)
   {
      Measurable m1 = new Stamp("Bicentennial", 38.0, "U.S.");
      System.out.println(m1.getMeasure());
      System.out.println("Expected: 38.0");
      Comparable c1 = new Stamp("Boxing Day",35.0,"U.K.");
      System.out.println(c1.compareTo(m1) < 0);
      System.out.println("Expected: true");      
   }
}