The RetroBug problem from the 2012 AP Computer Science Exam tests your understanding of inheritance by asking you to extend the Bug class from the GridWorld Case Study.

RetroBug is #2 from the 2012 AP Computer Science A Free Response.

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

RetroBug class

public class RetroBug extends Bug
{
    private Location prevLoc;
    private int prevDir;

    public void act()
    {
        prevLoc = getLocation();
        prevDir = getDirection();

        super.act();
    }

    public void restore()
    {
        if(prevLoc != null)
        {
            setDirection(prevDir);

            Actor atPrev = getGrid().get(prevLoc);
            if(atPrev == null || atPrev instanceof Flower)
                moveTo(prevLoc);
        }
    }
}

2012 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on RetroBug