Other 2004 FR Questions FR other years Be Prepared Home
A-4
Part (a)
  // postcondition: returns true if this Robot has a wall immediately in
  //                front of it, so that it cannot move forward;
  //                otherwise, returns false
  public boolean forwardMoveBlocked()
  {
    return (facingRight && pos == hall.length - 1) ||
                                    (!facingRight && pos == 0); 1
  }
Notes:
  1. No need to write verbose if statements.

Part (b)
  // postcondition: one move has been made according to the
  //                specifications above and the state of this
  //                Robot has been updated
  private void move()
  {
    if (hall[pos] > 0)
      hall[pos]--;

    if (hall[pos] == 0)
    {
      if (forwardMoveBlocked())
        facingRight = !facingRight;
      else if (facingRight)
        pos++;
      else
        pos--;
    }
  }

Part (c)
  // postcondition: no more items remain in the hallway;
  //                returns the number of moves made
  public int clearHall()
  {
    int count = 0;
    while (!hallIsClear())
    {
      move();
      count++;
    }
    return count;
  }

Other 2004 FR Questions | Back to Contents

Copyright © 2004 by Skylight Publishing
support@skylit.com