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.

https://secure-media.collegeboard.org/apc/ap11_frq_comp_sci_a.pdf

AttractiveCritter class

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

        for (Location loc : getGrid().getOccupiedLocations())
            actors.add(getGrid().get(loc));
        
        actors.remove(this);
        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
                a.moveTo(loc);
        }
    }
}

2011 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on AttractiveCritter