Solution to Grid interface practice test

Complete the Grid interface practice test before reviewing the solution.

Review the Grid interface practice test with AP CS Tutor Brandon Horn.

Question 1

(B) III only

The only precondition of isValid is that the parameter is not null. If the parameter is not valid in the grid, isValid returns false. This is not a violation of its precondition.

Question 2

ArrayList<Actor> actors = new ArrayList<Actor>();
ArrayList<Location> locs = g.getOccupiedLocations();
for(Location loc : locs)
 actors.add(g.get(loc));
return actors;

Grid provides a method to get a list of all occupied locations; however, there is no method to get a list of all actors. You need to know how convert from one to the other.

Question 3

(a) [(0, 1), (0, 3), (1, 1), (1, 2), (1, 3)]
(b) [(2, 0), (2, 1), (3, 1)]
(c) Violates preconditon of getAdjacentLocations
(d) Violates preconditon of getAdjacentLocations

You must know what is returned by getAdjacentLocations for locations with different characteristics. For example, (3,0) is at the edge of the grid and getAdjacentLocations returns only valid locations.

The precondition of getAdjacentLocations is that its explicit parameter is a valid location in the grid. (4,5) is not a valid location in a 4 x 5 bounded grid.

You should also know that the Location class has a compareTo method that orders Location objects in row major order. You are not required to memorize this. Instead, you should know where to look it up in your Quick Reference and understand what is presented there.

Get AP CS Help

GridWorld Practice Problems

GridWorld Case Study Explanations

Leave a Reply