Other 2012 FR Questions FR other years Be Prepared Home
A-2
public class RetroBug extends Bug 1
{
  private Location prevLocation = null; 2
  private int prevDirection;

  public RetroBug() 3
  {
    setColor(Color.RED); 4
  }

  public RetroBug(Color bugColor)
  {
    setColor(bugColor); 5
  }

  public void act()
  {
    prevLocation = getLocation();
    prevDirection = getDirection();
    super.act();
  }

  public void restore()
  {
    if (prevLocation == null) 6
      return;

    Actor neighbor = getGrid().get(prevLocation);
    if (neighbor == null || neighbor instanceof Flower)
      moveTo(prevLocation);

    setDirection(prevDirection);
  }
}
Notes:
  1. import statements are needed for the code to run but not required for full credit.
  2. = null is optional: it is the default, but the rules for default initialization are not in the AP subset.  It won't hurt if you add a redundant statement
        prevLocation = null;
    to each constructor (after a call to super, if present).
  3. The question of whether any or what constructors are needed to receive full credit will be answered at the AP Exam Reading in June.  Good style calls for two constructors for this class, similar to Bug's constructors.
  4. Or:
        super();
    or simply empty braces (super() is called by default).
  5. Or:
        super(bugColor);
  6. That is, if act has not been called before.

Other 2012 FR Questions | Back to Contents

Copyright © 2012 by Skylight Publishing
support@skylit.com