The AttractiveCritter problem from the 2011 AP Computer Science Exam is typical of free response problems that test GridWorld: The AP Computer Science Case Study. The problem requires you to extend the Critter class, determine which methods to override, and implement the entire subclass.

AttractiveCritter is #2 from the from the 2011 AP Computer Science A Free Response problems.

AttractiveCritter class

public class AttractiveCritter extends Critter
    public ArrayList<Actor> getActors()
        ArrayList<Actor> actors = new ArrayList<Actor>();

        for (Location loc : getGrid().getOccupiedLocations())
        return actors;

    public void processActors(ArrayList<Actor> actors)
        for (Actor a : actors)
            int direction = a.getLocation().getDirectionToward(getLocation());
            Location loc = a.getLocation().getAdjacentLocation(direction);
            if (getGrid().isValid(loc) && // validity check is not strictly necessary
                    getGrid().get(loc) == null) // empty check is necessary

2011 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on AttractiveCritter