AP Java Marine Biology Simulation Answers -Worksheet Ch 5 – Pages 79-92 1. The fish being represented are not all identical. 2. Analysis Question Set 1. 3. An interface doesn't have any instance variables and doesn't have code for all of its methods. 4. Create a class that provides implementations for all the methods that the interface specifies. 5. Using BoundedEnv references would require them to be rewritten if the implementation were to change (such as using an UnboundedEnv). Their parameters and instance variables of type Environment actually refer to an object of some class that implements the interface. That object is constructed in the driver which is the only place that must explicitly specify the environment's implementation class. 6. -1 indicates that the number of rows and number of columns are not fixed. 7. The numCellSides method, which defines the shape of the cells in the environment, can be set to 6 sides and the numAdjacentNeighbors can also be set to 6. There would also have to be some adjustments to getDirection (at least) to work with the new arrangement. 8. Analysis Question Set 2 (pg. 83) 9. SquareEnvironment is an abstract class which does not implement all of the methods specified by Environment. An instance can't be created since there are unimplemented methods. 10. SquareEnvironment implements only those Environment methods needed to define an environment of square cells whose neighbors are to the N, S, E, and W. These methods are numCellSides, numAdjacentNeighbors, randomDirection, getDirection, getNeighbor, and neighborsOf. It does have 2 constructors. One constructor creates an environment in which there are 4 adjacent neighbors around each cell, each sharing one of the sell's sides. The other constructor provides a way to create an environment with eigher 4 adjacent neighbors around each cell or eight (the 4 that share sides with the cell and the 4 on the diagonals, which share only a single point with the cell.) 11. If the superclass has instance variables, and if the subclass doesn't explicitly call any superclass's constructor, then the default constructor is called automatically. However, it is clearer to have the subclass's constructor call super(); 12. The precondition on the BoundedEnv constructor states that the number of rows must be greater than zero, so theGrid[0] exists and it's length can be returned. 13. A null reference (null location) and a location that is out of bounds 14. Returns false (it's not a valid empty location) 15. Analysis Question Set 3 (page 87) 16. a) The array can be initialized to the correct size because the number of objects in the is known. b) Using nested for loops each row (the output loop), the inner loop steps through all the column positions. 17. Analysis Question Set 4 (page 88) 18. IllegalArgumentException because the location is not a invalid empty location and the precondition isn't met – this is a serious error that the program cannot handle or is not meant to handle. 19. If the object is not in the environment at the correct location, it cannot be removed correctly 20. a) The environment and the object agree on where the object is. b) The object's new location must be a valid location in the environment and there shouldn't be any other object there. The object itself could be in that location already. 21. Analysis Question Set 5 (page 90) AP Java Marine Biology Simulation Answers -Worksheet Ch 5 - Pages 92-102 1. An ArrayList of Locatable objects (a fish is Locatable and keeps track of its own location) 2. objectList.size() gives the number of objects 3. One, default constructor that calls super() to initialize any inherited attributed (from SquareEnvironmnet) and create the empty ArrayList objectList. 4. Analysis Question Set 6 (page 95) 5. The allObjects method needs to return the objects is a one-dimensional array, not in an ArrayList. 6. public Locatable[] allObjects() { Locatable[] theList = new Locatable [objectList.size()]; return (Locatable[]) objectList.toArray(theList); } 7. Analysis Question Set 7 (page 97) 8. objectAt and remove both need to find any object in the ArrayList. It returns the index of the object, or -1 if no such object exists. 9. All recordMove needs to do is check its precondidtion and postcondition (the method's contract). To fulfill the precondition there must be no other object in this object's location. The postcondition requires that if any object has location is the same as oldLoc then it is only this object (that is, it didn't move). Most of the "work" of the BoundedEnv's is unnecessary because all locations are valid and the unbounded environment does not keep track of an object's location in any way - it just makes use of the fact that all the objects in it are Locatable and therefore keep track of their own locations. 10. Analysis Question Set 8 (page 101) 11. No - ArrayList indexOf needs the entire object to match agains, whereas objectAt knows only the location 12. Yes public void remove ( Locatable obj ) { int index = objectList.indexOf ( obj ); // find the index of the object to remove if ( index == -1 ) throw new IllegalArgumentException("Cannot remove " + obj + "; not there"); objectList.remove( index ); } 13. No - it would have the same problems as Pat's first draft - doesn't verify that there aren't two objects at the new location, which could happen if an object moved to a location without first checking that it was empty. 14. The display defaults to the minimum cell size and shows whatever portion of the environment fits in the window with location (0, 0) in the upper left-hand corner. 15. A bounded environment is specified by putting the word "bounded" in the first line with the enviornment's dimensions, as in previous data files. An unbounded environmnet is specified by putting the word "unbounded" in the first line, without any dimensions. The program driver (which reads the initial configuration file and constucts the environment) needs to be changed to fit these new specifications. 16. Location (0, 0) has N, S, E, and W neighboring locations like all other locations since there are no invalid locations - they just might not be shown on the display. 17. Analysis Question Set 9 (page 103) 17. Exercise Set 1 (page 104)