Name (print CLEARLY) ___________________________________   Lab Section you attend______________

CSE131 Quiz 10 Class Hierarchies 5 Dec 2008

// Point is from lab 4, not shown here
public interface Trackable {
   public Point getLocation();
}
public interface Poppable extends Trackable {
   //  includes everything in Trackable plus ....
   public void pop();
}
public class Trajectory implements Trackable {
   public Point getLocation() { return new Point(0,0); }
}
public class Balloon implements Poppable {
   public Point getLocation() { return new Point(0,0); }
   public void pop() { }
}
abstract public class Dart implements Trackable {
    public Trackable track;
    public Dart(Trackable t) { 
       this.track = t; 
    }
    abstract public int getDartRadius();
    public Point getLocation() { return track.getLocation(); }
    public boolean popsBalloon(Poppable p) {
       return this.getLocation().distance(p.getLocation()) < getDartRadius();
    }
}
public class PlainDart extends Dart {
   public PlainDart(Trackable t) { super(t); }
   public int getDartRadius()    { return 2; }
}
  1. In the following code, put an "X" by each line that would result in a compilation error, and put two stars ("**") by each line that would compile but would result in a run-time error. In considering each line, assume that all the correct lines above it have executed.
    If you mark a line, say a few words (using line number and on back) about why it won't work.
    /* 1. */  Trackable a  = new Trackable();
    /* 2. */  Dart      b  = new Dart(null);
    /* 3. */  Dart   dart1 = new PlainDart(null);
    /* 4. */  Dart   dart2 = new PlainDart(new Trajectory());
    /* 5. */  Poppable  p  = new Balloon();
    /* 6. */  boolean popped1 =  dart1.popsBalloon(dart2);
    /* 7. */  boolean popped2 =  dart2.popsBalloon(p);
    
  2. On the back of this page, write the class BigDart that extends Dart and provides a dart radius of 10:
  3. Consider an attempted stable match of A and B with a and b, resulting in the matching A-a and B-b. Below, fill in the preference matrix so that the matching above is not stable:
            #1     #2   |        #1     #2
                        |
         A              |     a
                        |
         B              |     b