Return to lecture notes index

15-100 Lecture 19 (Friday, October 14, 2005)

Quick Quiz

No quiz today -- it is clear that we need to revisit interfaces.

Today's Example

Today we are going to create a larger example to show the utility of interfaces. Here's the story line. We're loading a truck. And, when loading a truck, the heaviest things must be loaded first. In other words, items can be loaded in any other, so long as lighter items are added after heavier ones.

It doesn't matter what the item is -- so long as we can get its weight, it can work with our truck. In order to make the happen, we construct a Weighable interface that defines methods for getting the weight and mass of an item.

Then, we implement the truck class. It has a method, loadTruck() that attempts to add an item to the truck. It returns true on success and false on failure. It can load the item so long as doing so won't put the truck over its limit and as long as the item is lighter than the most recently loaded item.

In order to do this, the Truckmust be able to check the weight of the item. This is where the Weighable interface comes into play. Instead of limiting our truck to holding some particular type of item, such as a Box, our truck is limited to those items that are Weighable.

Since our Appliance, Furniture, and Box classes implement the Weighable interface, they can be referenced using a Weighable reference. By using Weighable as the type of argument for the loadTruck() method, the designer is able to ensure that only those items that can be weighed are passed in -- while allowing anything weighable. That includes Weighable things that may have been invented after the Truck!

And, actually, when we wrote these in class, we created the Furniture specification only after compiling and testing the Truck, just to prove this point.

The Example Code

In the code below, notice the requirements set out in the Weighable interface. Notice that the only method used by the Truck upon those things it is loading is required by this interface.

Notice also that each of the classes below, except the Truck itself, "implements Weighable" -- see the clause at the top? Notice that they implement both of the methods required by the Weighable interface, not just the one that happens to be used by the Truck. Notice also that the signatures exactly match those defined within the interface specification.

Lastly, notice the use of the Weighable reference by the Truck's loadTruck() method.

Weighable


Weighable.java

    interface Weighable {

      public double getWeightInLbs();
      public double getMassInKgs();

    }
    


Weighable.java

Appliance


Appliance.java

  class Appliance implements Weighable {

    private int length;
    private int width;
    private int height;
    private double weightInLbs;

    private static final double KG_PER_LB = 2.2;
  

    public Appliance (int length, int width, int height, double weightInLbs) {
  
      this.length = length;
      this.width = width;
      this.height = height;
    
      this.weightInLbs = weightInLbs;
    }
    
    
    public String toString() {
      return "" + width + " x " + length + " x " + height + " " + 
             weightInLbs + " lbs";
    }
    
    
    public boolean equals (Object o) {
    
      Appliance b = (Appliance) o;
      
      if (this.width != b.width) return false;
      if (this.height != b.height) return false;
      if (this.length != b.length) return false;
      if (this.weightInLbs != b.weightInLbs) return false;
      
      return true;
    }
    
      
    public double getWeightInLbs() {
    
      return weightInLbs;
    }
    
    public double getMassInKgs() {
      return  weightInLbs/ KG_PER_LB;
    }
  }
      


Appliance.java

Box


Box.java

    class Box implements Weighable {

      private int length;
      private int width;
      private int height;
      private double massInKgs;

      private static final double KG_PER_LB = 2.2;
      
    
      public Box (int length, int width, int height, double massInKgs) {
      
        this.length = length;
        this.width = width;
        this.height = height;
  
        this.massInKgs = massInKgs;
      }
  
  
      public void addToBox (int massInKgs) {
  
        this.massInKgs += massInKgs;
      
      }
  
  
      public String toString() {
        return "" + width + " x " + length + " x " + height + " " + 
               massInKgs + " kgs";
      }
  
  
      public boolean equals (Object o) {
  
        Box b = (Box) o;
    
        if (this.width != b.width) return false;
        if (this.height != b.height) return false;
        if (this.length != b.length) return false;
        if (this.massInKgs != b.massInKgs) return false;
    
        return true;
      }
  
  
      public double getMassInKgs() {
  
        return massInKgs;
      }
  
      public double getWeightInLbs() {
        return KG_PER_LB * massInKgs;
      }
  
    }
    


Box.java

Furniture


Furniture.java

    class Furniture implements Weighable {

      private double weightInLbs;
      private int peopleToMove;
      private double longestDimensionFt;

      private static final double KG_PER_LB = 2.2;
  
  
      public Furniture (double weightInLbs, int peopleToMove, 
      	                  double longestDimensionFt) {
        this.weightInLbs = weightInLbs;
        this.peopleToMove = peopleToMove;
        this.longestDimensionFt = longestDimensionFt;
      }
  
  
      public String toString() {
  
        return "" + weightInLbs + "lbs , " + peopleToMove + " people , " +
               longestDimensionFt + " ft";
      }
  
  
      public boolean equals(Object o) {
      
        Furniture f = (Furniture) o;
  
        if (weightInLbs != f.weightInLbs) return false;
        if (peopleToMove != f.peopleToMove) return false;
        if (longestDimensionFt != f.longestDimensionFt) return false;
    
        return true;
      }
  
  
      public double getMassInKgs() {
  
        return KG_PER_LB * weightInLbs;
      }
  
  
      public double getWeightInLbs() {
        return weightInLbs;
      }
    }
    


Furniture.java

Truck


Truck.java

    class Truck {

      private double totalWeight;
      private double lastItemWeight;
      private double maxWeight;

  
      public Truck (double maxWeight) {
        this.totalWeight = 0.0;
        this.lastItemWeight = 0.0; 
    
        this.maxWeight = maxWeight;
       }
   
   
       public double getTotalWeight() {
   
         return totalWeight;
   
       }
   
   
       public boolean loadTruck (Weighable item) {
         if (item.getWeightInLbs() > lastItemWeight) return false;
         
         if ( (totalWeight+item.getWeightInLbs()) > maxWeight) return false;
  
         //totalWeight = totalWeight + item.getWeightInLbs();
	 //the line above is equivalent to the line below!  
         totalWeight += item.getWeightInLbs();
         lastItemWeight = item.getWeightInLbs();
       
         return true;
   
       }
    }
    


Truck.java