RetroBug free response answer 11

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.

Review the RetroBug free response solution with AP CS Tutor Brandon Horn.

RetroBug

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);
    }
  }
}

Get AP CS Help

2012 AP CS Exam Free Response Solutions

11 thoughts on “RetroBug free response answer

  1. Reply bob May 11,2012 2:52 pm

    this.setDirection(this.prevDir);
    should be outside the if (location != null) loop. the bug should always go back to the original direction.

    • Reply Brandon Horn May 12,2012 9:04 am

      The problem stated that restore should have no effect if it is run before the first run of act. This would not be true if the call to setDirection was outside the check. Specifically, the direction of the bug would be set to the value of prevDir (0 unless explicitly initialized to something else) if restore is run before act.

  2. Reply Sarah Apr 18,2013 1:28 pm

    Could you please explain in detail how the restore() method is working?

    • Reply Brandon Horn Apr 19,2013 4:17 pm

      restore uses the RetroBug’s instance fields to restore the bug’s direction and (possibly) location to their values before its last move.

      If restore was run prior to the first run of act, prevLoc would be null. The first if statement ensures that the bug does not change its direction or attempt to move if restore is run prior to the first run of act.

      The second if statement ensure that the previous location is empty or is occupied by a flower.

  3. Reply Sarah Apr 20,2013 8:34 am

    In this program, how do I understand where to use the this keyword?

  4. Reply Sarah Apr 20,2013 10:08 am

    Please also explain the function of:
    Actor atPrev = this.getGrid().get(this.prevLoc);

    • Reply Brandon Horn Apr 22,2013 1:27 pm

      The RetroBug is only supposed to return to its previous location if the location is empty or contains a Flower. The line you mentioned obtains the Actor at prevLoc or null if the location is empty.

  5. Reply Jake Apr 21,2013 7:15 pm

    Why is this called before all the variables? http://www.skylit.com/beprepared/x2012a2.html is another solutions guide, and it never calls this

    • Reply Brandon Horn Apr 22,2013 1:24 pm

      The Java keyword this refers to the current object. It can be used for a variety of purposes. The use in RetroBug is optional.

  6. Reply Toan May 4,2014 10:12 pm

    When is the restore method called? The act method restore previous Location and Direction, but when is the method restore called for RetroBug? Or is it unnecessary?

    • Reply Brandon Horn May 4,2014 10:16 pm

      restore is not called within the question. It is a method that could be run by a class that uses RetroBug.

Leave a Reply