CS101 Quiz 9: Solutions

  1. This is Java's way of defining and API: a collection of methods and thier type signatures, without any code for the methods. So, the phrase at-hand defines the API for Set.
  2. This is Java's way of saying that the class Foo provides an implementation of the Set API. Any instance of Foo can be treated as a Set.
  3. public interface Relation {
        public void   map(Object domain, Object range);
        public Object lookup(Object domain);
        public Object drop(Object domain);
  4. public class ListBasedRelation extends ListOfObjects implements Relation {
       public ListBasedRelation() { super(); }
       public void map(Object d, Object r) {
          DomainRange found = locate(d);
          if (found == null) {
             append(new DomainRange(d, r));  // placement is arbitrary
          else found.setRange(r);                  // replace what was there
       public Object lookup(Object d) {
          DomainRange dr = locate(d);
          if (dr != null) return dr.getRange();
          else return null;
       public Object drop(Object d) {
          Object ans = null;
          if (locate(d) != null) { // NB:  count on marker positioned correctly
             DomainRange dropped = (DomainRange) delete();
             ans = dropped.getRange();
          return ans;
       private DomainRange locate(Object d) {
          while (!atEnd()) {
             DomainRange dr = (DomainRange) getItem();
             if (dr.matchesDomain(d)) return dr;
          return null;