Sample Free-Response Solution (AB Exam) (a) /** Returns all the objects in this environment. * @return an array of all the environment objects **/ public Locatable[] allObjects() { Locatable[] theObjects = new Locatable[numObjects()]; int tempObjectCount = 0; // Look at all grid locations. // insert code here ListNode curNode; for (int r = 0; r < numRows(); r++) { curNode = theGrid[r]; while (curNode != null) { theObjects[tempObjectCount] = (Locatable)curNode.getValue(); curNode = curNode.getNext(); tempObjectCount++; } } // end of inserted code here return theObjects; } (b) /** Adds a new object to this environment at the location * it specifies. * (Precondition: obj.location() is a valid empty * location.) * @param obj the new object to be added * @throws IllegalArgumentException if precondition is not met **/ public void add(Locatable obj) { // Check precondition. Location should be empty. Location loc = obj.location(); if ( ! isEmpty(loc) ) throw new IllegalArgumentException("Location " + loc + " is not a valid empty location"); // Add object to the environment. // insert code here if (theGrid[loc.row()] == null || ((Locatable)theGrid[loc.row()].getValue()).location().col() > loc.col()) { theGrid[loc.row()] = new ListNode(obj, theGrid[loc.row()]); } else { ListNode curNode = theGrid[loc.row()]; while (curNode.getNext() != null && ((Locatable)curNode.getNext().getValue()).location().col() < loc.col()) { curNode = curNode.getNext(); } ListNode temp = new ListNode(obj, curNode.getNext()); curNode.setNext(temp); } objectCount++; }