Other 2008 FR Questions | FR other years | Be Prepared Home |
/** @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; } 2Notes:
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:
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; } |
Copyright © 2008 by Skylight Publishing
support@skylit.com