Robot is #4 from the from the 2004 AP Computer Science A Free Response problems.

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

Part (a) forwardMoveBlocked method

private boolean forwardMoveBlocked()
{
    if(facingRight)
        return pos == hall.length - 1;
    else
        return pos == 0;
}

Part (b) move method

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) clearHall method

public int clearHall()
{
    int moves = 0;
    
    while( ! hallIsClear() )
    {
        move();
        moves++;
    }
    
    return moves;
}

2004 AP CS A Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on Robot