logo资料库

实训参考答案.pdf

第1页 / 共59页
第2页 / 共59页
第3页 / 共59页
第4页 / 共59页
第5页 / 共59页
第6页 / 共59页
第7页 / 共59页
第8页 / 共59页
资料共59页,剩余部分请下载后查看
GridWorld AP® Computer Science Case Study Solutions Manual __________________________________________________________________ The AP® Program wishes to acknowledge and to thank Judith Hromcik of Arlington High School in Arlington, Texas. © 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).
Part 1 Part 1 Answers: Do You Know? Set 1 1. Does the bug always move to a new location? Explain. 2 No. A bug will only move to the location in front of it if the cell exists and is empty or if there is a flower in the cell. 2. In which direction does the bug move? A bug attempts to move forward. 3. What does the bug do if it does not move? When a bug cannot move, it turns 45 degrees to the right. 4. What does a bug leave behind when it moves? A bug leaves a flower in its old cell when it moves to a new cell. The flower is the same color as the bug. 5. What happens when the bug is at an edge of the grid? (Consider whether the bug is facing the edge as well as whether the bug is facing some other direction when answering this question.) If a bug is facing the grid edge and it is told to act, it will turn 45 degrees to the right. When told to act again, it will turn another 45 degrees to the right. If a bug is facing the grid edge and it is told to move, it will remove itself from the grid and a flower will replace the bug in that location. 6. What happens when a bug has a rock in the location immediately in front of it? The bug turns 45 degrees to the right. 7. Does a flower move? No. 8. What behavior does a flower have? A flower “wilts” over time. The color of a flower gets darker until it turns a dark gray. 9. Does a rock move or have any other behavior? No. A rock stays in its location and does not appear to have any other behaviors when the step or run button is used. 10. Can more than one actor (bug, flower, rock) be in the same location in the grid at the same time? No. A location in the grid can contain only one actor at a time. © 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).
Exercises (Page 8) 1. Test the setDirection method with the following inputs and complete the table, giving the compass Part 1 3 direction each input represents. Degrees 0 45 90 135 180 225 270 315 360 Compass Direction North NorthEast East SouthEast South SouthWest West NorthWest North 2. Move a bug to a different location using the moveTo method. In which directions can you move it? How far can you move it? What happens if you try to move the bug outside the grid? A bug can be moved to any valid location in the grid using the moveTo method. When moving the bug using the moveTo method, the bug will not change its original direction. The setDirection method or turn method must be used to change a bug’s direction. Attempting to move a bug to a location outside of the grid (an invalid location for the grid) will cause an IllegalArgumentException to be thrown. 3. Change the color of a bug, a flower, and a rock. Which method did you use? The setColor method. 4. Move a rock on top of a bug and then move the rock again. What happened to the bug? When a rock is moved “on top” of a bug, the bug disappears. When the rock is moved to another location, the bug is no longer there, or anywhere else in the grid. When a new actor moves into a grid location occupied by another actor, the old actor is removed from the grid. © 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).
Part 2 Do You Know? Set 2 1. What is the role of the instance variable sideLength? 4 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. © 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).
Part 2 Part 2 Exercises 1. Write a class CircleBug that is identical to BoxBug, except that in the act method the turn method is called once instead of twice. How is its behavior different from a BoxBug? 5 import info.gridworld.actor.Bug; public class CircleBug extends Bug { private int steps; private int sideLength; public CircleBug(int n) { sideLength = n; } public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); steps = 0; } } } The path of a CircleBug is an octagon, instead of a square. © 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).
2. Write a class SpiralBug that drops flowers in a spiral pattern. Hint: Imitate BoxBug, but adjust the side length when the bug turns. You may want to change the world to an UnboundedGrid to see the spiral pattern more clearly. 6 Part 2 import info.gridworld.actor.Bug; public class SpiralBug extends Bug { private int sideLength; private int steps; public SpiralBug(int n) { sideLength = n; steps = 0; } public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); turn(); steps = 0; //Each time a SpiralBug turns, increase the sideLength by one sideLength++; } } } © 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).
7 Part 2 The following SpiralBugRunner class can be used with the suggested UnboundedGrid. import info.gridworld.actor.Actor; import info.gridworld.grid.UnboundedGrid; import info.gridworld.actor.ActorWorld; import info.gridworld.grid.Location; public class SpiralBugRunner { public static void main(String[] args) { UnboundedGrid grid = new UnboundedGrid(); ActorWorld world = new ActorWorld(grid); SpiralBug sp = new SpiralBug(3); world.add(new Location(3,5),sp); world.show(); } } © 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).
Part 2 3. Write a class ZBug to implement bugs that move in a “Z” pattern, starting in the top left corner. After completing one “Z” pattern, a ZBug should stop moving. In any step, if a ZBug can’t move and is still attempting to complete its “Z” pattern, the ZBug does not move and should not turn to start a new side. Supply the length of the “Z” as a parameter in the constructor. The following image shows a “Z” pattern of length 4. Hint: Notice that a ZBug needs to be facing east before beginning its “Z” pattern. 8 import info.gridworld.actor.Bug; import info.gridworld.grid.Location; /** * A ZBug traces out a Z pattern of a given size. */ public class ZBug extends Bug { private int segmentLength; // the number of flowers in each segment private int steps; // the number of steps in the current side private int segment; // which segment of the Z the ZBug is on © 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).
分享到:
收藏