Part 2
© 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals)
and www.collegeboard.com/apstudents (for AP students and parents).
4
Do You Know?
Set 2
1. What is the role of the instance variable sideLength?
The sideLength instance variable defines the number of steps a BoxBug moves on each side of its box.
2. What is the role of the instance variable steps?
The steps instance variable keeps track of how many steps a BoxBug has moved on the current side of its
box.
3. Why is the turn method called twice when steps becomes equal to sideLength?
When a BoxBug travels sideLength steps, it has to turn 90 degrees to travel along the next side of its
box. The turn method only executes a 45 degree turn; therefore it takes two turn method calls to turn 90
degrees.
4. Why can the move method be called in the BoxBug class when there is no move method in the
BoxBug code?
The BoxBug class extends the Bug class, and the Bug class has a public move method. Since the
BoxBug class is a subclass of the Bug class, it inherits the move method from the Bug class.
5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?
Yes. When a BoxBug is constructed, the side length is determined and cannot be changed by client code.
6. Can the path a BoxBug travels ever change? Why or why not?
Yes. If another Actor, like a Rock or Bug, is in front of a
BoxBug when it tries to move, the BoxBug
will turn and start a new box path.
7. When will the value of steps be zero?
Initially, the value of steps is set to zero when a BoxBug is constructed. After that, the value of steps
will be set to zero when steps is equal to sideLength—meaning the BoxBug has completed one side
of its box path, or when the BoxBug cannot move and turns instead to start a new box path.
评论1
最新资源