Other 2008 FR Questions FR other years Be Prepared Home
AB-3
Part (a)
  /** @param loc a valid location in this grid
   *  @return a set of all objects at loc; an empty set, if no objects at loc
   *  Postcondition: the contents of this grid remain unchanged
   */
  public Set<Object> get(Location loc)
  {
    Set<Object> s = grid.get(loc);
    if (s == null)
      s = new HashSet<Object>(); 1
    return s;
  } 2
Notes:
  1. Or: new TreeSet<Object>();
  2. Many students, no doubt, wrote simply
    { return grid.get(loc); }
    However the header for the method says: "return ... an empty set, if no objects at loc."  So, strictly speaking, it is a mistake to return null

    In our view, to return an empty set is a poor and couterintuitive design decision.  Grid's get method returns null for an empty location.  Also, the remark in the opening paragraph -- "An empty location is always indicated by null, not an empty set" -- seems to imply that Multigrid's get should return null.  Finally, any object returned by get is presumably in the grid, so we shouldn't have to worry about putting it back into the grid, as in:

      public void put(Location loc, Object obj)
      {
        Set<Object> s = get(loc); // Note get(loc), not grid.get(loc)
        if (s.size() == 0)
          grid.put(loc, s);
        s.add(obj);
      }
    (see Part (b)).  We will know how this question was graded only when the rubric is out.

Part (b)
  /** Puts an object at a given location in this grid.
   *  Precondition: (1) loc is valid in this grid. (2) obj is not null.
   *  @param loc the location at which to put the object
   *  @param obj the new object to be added
   */
  public void put(Location loc, Object obj)
  {
    Set<Object> s = grid.get(loc);
    if (s == null) 1
    {
      s = new HashSet<Object>();
      grid.put(loc, s);
    }
    s.add(obj);
  }
Notes:
  1. As opposed to: if (s.size() == 0) ...

Part (c)
  /** Gets the neighboring occupants in all eight compass directions
   *  (north, northeast, east, southeast, south, southwest, west, and northwest).
   *  @param loc a location in this grid
   *         Precondition: loc is valid in this grid
   *  @return an array list of the objects in the occupied locations adjacent to loc in this grid
   */
  public ArrayList<Object> getNeighbors(Location loc)
  {
    ArrayList<Set<Object>> neighborSets = grid.getNeighbors(loc);
    ArrayList<Object> occupants = new ArrayList<Object>();

    for (Set<Object> aSet : neighborSets)
      for (Object obj : aSet)
        occupants.add(obj);

    return occupants;
  }

Other 2008 FR Questions | Back to Contents

Copyright © 2008 by Skylight Publishing
support@skylit.com