GridWorld Solutions Document

2y ago
6 Views
2 Downloads
373.18 KB
59 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Grant Gall
Transcription

GridWorld AP Computer ScienceCase StudySolutions ManualThe AP Program wishes to acknowledge and to thankJudith 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 12Part 1 Answers:Do You Know?Set 11. Does the bug always move to a new location? Explain.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 thecell.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 aswhether 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, itwill 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 willreplace 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 isused.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).

Part 13Exercises (Page 8)1. Test the setDirection method with the following inputs and complete the table, giving the compassdirection each input represents.Degrees04590135180225270315360Compass WestNorthWestNorth2. Move a bug to a different location using the moveTo method. In which directions can you move it? How farcan 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 usingthe moveTo method, the bug will not change its original direction. The setDirection method or turnmethod 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 anIllegalArgumentException 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, thebug is no longer there, or anywhere else in the grid. When a new actor moves into a grid location occupied byanother 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 24Do You Know?Set 21. 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 itsbox.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 itsbox. The turn method only executes a 45 degree turn; therefore it takes two turn method calls to turn 90degrees.4. Why can the move method be called in the BoxBug class when there is no move method in theBoxBug code?The BoxBug class extends the Bug class, and the Bug class has a public move method. Since theBoxBug 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 BoxBugwill 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 stepswill be set to zero when steps is equal to sideLength—meaning the BoxBug has completed one sideof 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 2Part 2 Exercises1. Write a class CircleBug that is identical to BoxBug, except that in the act method the turnmethod is called once instead of twice. How is its behavior different from a BoxBug?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).5

Part 22. Write a class SpiralBug that drops flowers in a spiral pattern. Hint: Imitate BoxBug, but adjust the sidelength when the bug turns. You may want to change the world to an UnboundedGrid to see the spiralpattern more clearly.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 onesideLength ;}}} 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals)and www.collegeboard.com/apstudents (for AP students and parents).6

Part 2The following SpiralBugRunner class can be used with the suggested .Location;public class SpiralBugRunner{public static void main(String[] args){UnboundedGrid grid new UnboundedGrid Actor ();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).7

Part 23. Write a class ZBug to implement bugs that move in a “Z” pattern, starting in the top left corner. Aftercompleting one “Z” pattern, a ZBug should stop moving. In any step, if a ZBug can’t move and is stillattempting 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 oflength 4. Hint: Notice that a ZBug needs to be facing east before beginning its “Z” pattern.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 segmentprivate int steps; // the number of steps in the current sideprivate 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).8

Part 2/*** Constructs a Zbug that traces a Z pattern in which each segment* of the Z has the given length* When the Z is drawn, the ZBug stops.* @param length the segment length*/public ZBug(int length){setDirection(Location.EAST);steps 0;segment 1;segmentLength length;}public void act(){if (segment 3 && steps segmentLength){if (canMove()){move();steps ;}}else if (segment 1){setDirection(Location.SOUTHWEST);steps 0;segment ;}else if (segment 2){setDirection(Location.EAST);steps 0;segment ;}}} 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals)and www.collegeboard.com/apstudents (for AP students and parents).9

Part 2104. Write a class DancingBug that “dances” by making different turns before each move. The DancingBugconstructor has an integer array as parameter. The integer entries in the array represent how many times thebug turns before it moves. For example, an array entry of 5 represents a turn of 225 degrees (recall one turn is45 degrees). When a dancing bug acts, it should turn the number of times given by the current array entry,then act like a Bug. In the next move, it should use the next entry in the array. After carrying out the lastturn in the array, it should start again with the initial array value so that the dancing bug continually repeatsthe same turning pattern.The DancingBugRunner class should create an array and pass it as a parameter to the DancingBugconstructor.import info.gridworld.actor.Bug;public class DancingBug extends Bug{private int[] turnList;private int currentStep;public DancingBug(int[] turns){turnList turns;currentStep 0;}public void turn(int times){for(int j 1; j times; j ){turn();}}public void act(){if(currentStep turnList.length)currentStep 0;turn (turnList[currentStep]);currentStep ;super.act();}} 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 211import info.gridworld.actor.ActorWorld;import info.gridworld.grid.Location;import java.awt.Color;public class DancingBugRunner{public static void main(String[] args){ActorWorld world new ActorWorld();int[] turns {2,2,1,3};DancingBug ballerina new ;world.add(new Location(9, 9), ballerina);world.show();}}5. Study the code for the BoxBugRunner class. Summarize the steps you would use to add another BoxBugactor to the grid.1. Create a BoxBug object with the desired side lengthBoxBug anotherOne new BoxBug(2);2. Add the new BoxBug to the word at a random or specific locationworld.add(anotherOne);orworld.add(new Location(1,1), anotherOne); 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 312Do You Know?Set 3Assume the following statements to answer the following questions.Location loc1 new Location(4,3);Location loc2 new Location(3,4);1. How would you access the row value for loc1?loc1.getRow()2. What is the value of b after the following statement is executed?boolean b loc1.equals(loc2);false3. What is the value of loc3 after the following statement is exectued?Location loc3 loc2.getAdjacentLocation(Location.SOUTH);(4,4)4. What is the value of dir after the following statement is executed?int dir loc1.getDirectionToward(new Location(6,5));135 (degrees)—Southeast5. How does the getAdjacentLocation method know which adjacent location to return?The parameter in the getAdjacentLocation method indicates the direction of the adjacent neighbor to find. Itreturns the adjacent location in the compass direction that is closest to the direction given in the parameter list.Do You Know?Set 41. How can you obtain a count of the objects in a grid? How can you obtain a count of the empty locations in a boundedgrid?Assume that gr is a reference to a Grid object.gr.getOccupiedLocations().size() will find the number of occupied locations in any type of grid.gr.getNumRows()*gr.getNumCols() - gr.getOccupiedLocations().size() will find thenumber of empty locations in a BoundedGrid object. 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 3132. How can you check if location (10,10) is in the grid?gr.isValid(new Location(10,10))The Grid method isValid returns true if and only if the given location is a valid location in the grid.3. Grid contains method declarations, but no code is supplied in the methods. Why? Where can you find theimplementations of these methods?Grid is an interface. In Java, an interface specifies which methods another class must implement. You can find theimplementations of the methods in the AbstractGrid and the BoundedGrid and UnboundedGridclasses. Since the AbstractGrid only implements some of the required methods of the Grid interface, it isdeclared an abstract class. The BoundedGrid and UnboundedGrid extend the AbstractGrid classand implement the rest of the methods required by the Grid interface.4. All methods that return multiple objects return them in an ArrayList. Do you think it would be a better design toreturn the objects in an array? Explain your answer.As a user of the Grid classes, perhaps. Accessing elements with the [] notation is somewhat easier than usingdifferent method calls. For example:locs[j] versus locs.get(j)In terms of implementing the methods, an ArrayList does not require the user to size the list before filling it. Anarray does. Since the BoundedGrid does not keep track of the number of objects in the grid, you would have tofirst count the number of occupied locations to size the array, and then go back through the grid to find and store eachof the locations. If the Grid kept track of the number of occupied locations, filling an array would be just as easy asfilling an ArrayList.Do You Know?Set 51. Name three properties of every actor.An actor has a color, a direction, and a location. It also has a reference to its grid.2. When an actor is constructed, what is its direction and color?An actor’s initial color is blue and its initial direction is North.3. Why do you think that the Actor class was created as a class instead of an interface?An actor has both state and behavior. An interface does not allow the programmer to declare instance variables orimplement methods. 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 3144. (a) Can an actor put itself into a grid twice without removing itself? (b) Can an actor remove itself from a grid twice?(c) Can an actor be placed into a grid, remove itself, and then put itself back? Try it out. What happens?(a) No—if the actor is already in the grid, it may not put itself in the grid again. This version of BugRunner.javawill compile, but when it runs it will throw an IllegalStateException.public class BugRunner{public static void main(String[] args){ActorWorld world new ActorWorld();Bug b new etLocation());world.add(new Rock());world.show();}}(b) No—if the actor has already been removed from the grid (and is currently out of the grid), it cannot be removedagain. This version of BugRunner.java will compile, but when it runs it will throw anIllegalStateException.public class BugRunner{public static void main(String[] args){ActorWorld world new ActorWorld();Bug b new Bug();world.add(b);world.add(new veSelfFromGrid();}} 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 3(c) Yes—an actor can be placed in the grid, remove itself, and then put itself back in the grid. Try thisActorRunner.java. It will compile and run without error.public class ActorRunner{public static void main(String[] args){ActorWorld world new ActorWorld();Actor a new Actor();world.add(a);Grid g a.getGrid(); //must remember the grid for placement back in//the gridworld.add(new elfInGrid(g,new Location(5,5)); //must specify a location here}}5. How can an actor turn 90 degrees to the right?To turn an actor 90 degrees to the right, use the setDirection method as follows in an Actor class:setDirection(getDirection() Location.RIGHT)orsetDirection(getDirection() 90);To turn our bug in BugRunner.java, try this code:public class BugRunner{public static void main(String[] args){ActorWorld world new ActorWorld();Bug b new Bug();world.add(b);world.add(new Rock());world.show();b.setDirection(getDirection() Location.RIGHT);}} 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals)and www.collegeboard.com/apstudents (for AP students and parents).15

Part 316Do You Know?Set 61. Which statement(s) in the canMove method ensures that a bug does not try to move out of its grid?if(!gr.isValid(next))return false;This statement makes sure that the next location is a valid location in the grid.2. Which statement(s) in the canMove method determines that a bug will not walk into a rock?Actor neighbor gr.get(next);return (neighbor null) (neighbor instanceof Flower);These two statements work together to make sure that a bug will only travel to the next location if it is unoccupied oroccupied by a flower.3. Which methods of the Grid interface are invoked by the canMove method and why?isValid and get. These methods are called to ensure that the next location is a valid location in the grid and tolook at the object in that location to ensure that it is empty or contains an actor that can be replaced by the bug.4. Which method of the Location class is invoked by the canMove method and why?getAdjacentLocation. This method is called by the bug with the bug’s current direction to find its nextpossible location.5. Which methods inherited from the Actor class are invoked by the canMove method?getLocation, getDirection, getGrid6. What happens in the move method when the location immediately in front of the bug is out of the grid?The bug will remove itself from the grid.7. Is the variable loc needed in the move method, or could it be avoided by calling getLocation() mulipletimes?Yes, the variable loc is needed. The variable loc stores the bug’s location before it moves. It is used to insert aflower in the bug’s old location after the bug has moved to its new location.8. Why do you think the flowers that are dropped by a bug have the same color as the bug?(Answers may vary.) Initially (before the flower wilts), it is easier to see which bug dropped what flowers because thecolor of the bug and its flowers are the same. 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 3179. When a bug removes itself from the grid, will it place a flower into its previous location?If you just call the removeSelfFromGrid method, no. This method is inherited from the Actor class. Actorsdo not put a flower in their old location.When the removeSelfFromGrid is called in th

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 anothe

Related Documents:

to real robot navigation. In other words, we only include machine learning approaches for motion planning and con-trol that actually move mobile robots in their (simulated or physical) environments, excluding papers that, for example, use machine learning to navigate (non-robotic) agents in gridworld-like environments (Dennis et al. 2020), address

The College Board The College Board is a not-for-profit membership association whose mission is to connect students to college success and opportunity. Founded in 1900, the College Board is composed of more than 5,700 schools, colleges, universities and other educational organizations. Each year, the College Board serves seven million students .

Document Type System will populate the document type based on the document code. Document Description System will populate the document description based on the document code. User can edit the description. Document Reference Enables the user to capture the document reference. Copies Received Provide the number copies received from the Drawer.

20 DOCUMENT HISTORY 21 22 Document Number: 7.7.1 Document Version: 7.7.1 Document Issue Date May 23, 2005 23 24 Document Summary Document Title EAN UCC - Business Requirements Document For Data Synchronization Data Model for Trade Item - (Data Definition) Owner Align Data BRG Grant Kille - Co-Chair -Julia Holden - Vice Chair

Cisco 819G-S-K9 Integrated Solutions Router 15.2(4)M6A Cisco 819HG-4G-G-K9 Integrated Solutions Router 15.2(4)M6A Cisco 891 Integrated Solutions Router 15.2(4)M6A Cisco 881 Integrated Solutions Router 15.2(4)M6A Cisco 1905 Integrated Solutions Router 15.2(4)M6A Cisco 1921 Integrated Solutions Router 15.2(4)M6A Cisco 1941 Integrated Solutions .

SOLUTION maNUaL n Solutions for Diagnostic Test n Solutions for Language Comprehension n Solutions for mathematical Skills n Solutions for Data analysis and Data Sufficiency n Solutions for Intelligence and Critical Reasoning n Solutions for mock Tests (1 to 5) 01_Solutions 01.indd 1 10/30/2009 11:56:28 AM

This solutions manual contains solutions for all odd numbered problems plus a large number of solutions for even numbered problems. Of the 624 exercises in Statistical Inference, Second Edition, this manual gives solutions for 484 (78%) of them. There is an obtuse pattern as to which solutions were included in this manual.

The SBSS-prepared A02 and A0B MILS transactions carry the expanded length descriptive data, which could contain various types of information for part-numbered requisitions in rp 67-80, and requires mapping to the DLMS transaction. This information is common to the YRZ exception data used by DLA, and so can be mapped to the generic note field as specified above. b. DLMS Field Length .