CSE 131 Module 10: Class Hierarchies

Practice Problems


You may find it easier to look at these exercises on paper than on the screen.
import java.util.*;

abstract class Bird {
  String name;
  boolean canFly = true;
  Flock flock;
  public Bird(String name) { this.name = name; }
  public String getName() { return name; }
  public abstract String preferredClimate();
  public boolean likes(Bird b) {return true;}
  public Flock getFlock() {
    if (flock == null) {
      flock = new Flock();
      flock.add(this);
    }
    return flock;
  }
  public void joinFlockOf(Bird b) {
    if (likes(b)) {
      if (flock != null)
        flock.remove(this);
      flock = b.getFlock();
      flock.add(this);
    }
  }
  public String toString() {
    String result = name + " lives in " + preferredClimate();
    if (canFly)
      return "flying " + result;
    else
      return result;
  }
}

class BirdPuppet extends Bird {
  public BirdPuppet(String name) {
    super(name);
    canFly = false;
  }
  public String preferredClimate() { return "room temperature"; }
  public boolean likes(Bird b) {
    return (b instanceof BirdPuppet);
  }
  public void joinFlockOf(Bird b) {
    b.joinFlockOf(this);
  }
}

class SongBird extends Bird {
  public SongBird(String name) {
    super(name);
  }
  public String preferredClimate() { return "warm weather"; }

  public boolean migrates() {
    return true;
  }
  public boolean likes(Bird b) {
    return ((b instanceof SongBird) && ((SongBird) b).migrates());
  }
}

class Warbler extends SongBird {
  public Warbler() {
    super("Warbler");
  }
  public boolean likes(Bird b) {
    return super.likes(b) || (b instanceof BirdPuppet);
  }
}

class BigBird extends BirdPuppet {
  public BigBird() {
    super("BigBird");
  }
  public String preferredClimate() { return "Sesame Street"; }
}

class Flock extends LinkedList {  // LinkedList is from package java.util
  public boolean add(Object x) {
    return super.add((Bird) x);
  }
  public String toString() {
    String result = "";
    Iterator it = iterator();
    while (it.hasNext())
      result += " " + ((Bird) it.next()).getName();
    return result;
  }   
}
  1. Draw the class hierarchy for the six classes given above.

  2. 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.
    Bird b1, b2, b3, b4;
    b1 = new Bird("Cardinal");
    b2 = new SongBird("Goldfinch");
    b3 = new BigBird();
    Warbler w = new Warbler();
    SongBird sb1 = b2;
    SongBird sb2 = (SongBird) b2;
    SongBird sb3 = b3;
    SongBird sb4 = (SongBird) b3;
    SongBird sb5 = w;
    SongBird sb6 = (SongBird) w;
    
    Warbler warbler = new Warbler();
    Bird big = new BigBird();
    String pigeon = "pigeon";
    warbler.joinFlockOf(big);
    warbler.joinFlockOf(pigeon);
    warbler.getFlock().add(pigeon);
    warbler.migrates();
    big.migrates();
    warbler = big;
    warbler = (Warbler) big;
    
  3. What output that would be printed by executing the following procedure?
    void birdsOfAFeatherFlockTogether() {
      SongBird sb = new SongBird("Goldfinch");
      Warbler w = new Warbler();
      BigBird bb = new BigBird();
      System.out.println("sb = " + sb);
      System.out.println("w = " + w);
      System.out.println("bb = " + bb);
    
      System.out.println("sb likes w? " + sb.likes(w));
      System.out.println("sb likes bb? " + sb.likes(bb));
      System.out.println("w likes sb? " + w.likes(sb));
      System.out.println("w likes bb? " + w.likes(bb));
      System.out.println("bb likes sb? " + bb.likes(sb));
      System.out.println("bb likes w? " + bb.likes(w));
    
      sb.joinFlockOf(w);
      bb.joinFlockOf(sb);
      System.out.println("Flock of sb: " + sb.getFlock());
      System.out.println("Flock of bb: " + bb.getFlock());
    
      bb.joinFlockOf(w);
      System.out.println("Flock of w: " + w.getFlock());
      System.out.println("Flock of bb: " + bb.getFlock());
    }
    

Solutions