GridWorld Case Study Part 2 Bug Variations A Summary by Jim Mims
Contents Introduction Part 1: Observing and Experimenting with GridWorld Part 2: Bug Variations Part 3: GridWorld Classes and Interfaces Part 4: Classes that Extend the Critter Class Part 5: Grid Data Structures
Classes and Interfaces: Relationships
Introduction The case study provides a graphical environment where visual objects inhabit and interact in a two-dimensional grid. Students design and creator actor objects, add them to a grid, and determine whether they behave according to specifications.
Bug Class Methods public boolean canMove() tests whether the bug can move forward into a location that is empty or contains a flower public void move() moves the bug forward, putting a flower into the location it previously occupied public void turn() turns the bug 45 degrees to the right without changing its position
Extending Bug Class – BoxBug Class Moves in a square pattern No new methods need to be added Act method uses the 3 methods listed previously BoxBug class has 2 instance variables, sideLength and steps
Runner Classes A runner class is added to observe the behavior of one or more actors Runner class constructs an ActorWorld object places actors in it shows the world Act method uses the 3 methods listed previously For the bug, this class is BugRunner When you write your own classes that extend bug, you need to create a similar runner class
BoxBug Runner