Bug extends Actor but differs from Actor in




Bug extends Actor but differs from Actor in that a Bug actually moves from cell to cell. A Bug moves to the cell immediately in front if possible. If a move is not possible, the bug turns in 45 degree increments until it finds a spot to which it can move.

Bug extends Actor frequently used constructors Name Use Bug() make a new red bug going north Bug(color) make a new bug set to color import info. gridworld. actor. Bug;

Bug extends Actor frequently used methods Name Use get. Color() gets the bug's color get. Direction() gets the bug's direction get. Location() gets the bug's location set. Color(col) sets the bug's color to col set. Direction(dir) sets the bug's direction to dir import info. gridworld. actor. Bug;

Actor. World world = new Actor. World(); Bug dude = new Bug(); world. add(new Location(3, 3), dude); Bug sally = new Bug(Color. GREEN); sally. set. Direction(Location. SOUTHEAST); world. add(new Location(2, 2), sally); Bug ali = new Bug(Color. ORANGE); ali. set. Direction(Location. NORTHEAST); world. add(new Location(1, 1), ali); world. show();




1. What is the role of the instance variable side. Length? The side. Length instance variable defines the number of steps a Box. Bug 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 Box. Bug has moved on the current side of its box. 3. Why is the turn method called twice when steps becomes equal to side. Length? When a Box. Bug travels side. Length 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 Box. Bug class when there is no move method in the Box. Bug code? The Box. Bug class extends the Bug class, and the Bug class has a public move method. Since the Box. Bug class is a subclass of the Bug class, it inherits the move() method from the Bug class.

5. After a Box. Bug is constructed, will the size of its square pattern always be the same? Why or why not? Yes. When a Box. Bug is constructed, the side length is determined and cannot be changed by client code. 6. Can the path a Box. Bug travels ever change? Why or why not? Yes. If another Actor, like a Rock or Bug, is in front of a Box. Bug when it tries to move, the Box. Bug 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 Box. Bug is constructed. After that, the value of steps will be set to zero when steps is equal to side. Length—meaning the Box. Bug has completed one side of its box path, or when the Box. Bug cannot move and turns instead to start a new box path.


What does a Bug do when its act() method is called ? What methods does the act() method appear to call?

public void act() { if (can. Move()) move(); else turn(); } Let’s look at the Bug class

Bug extends Actor frequently used methods – Bug specific Name Use act() move if possible or otherwise turn can. Move() check to see if a move is possible move() move forward and leave a flower turn() turn 45 degrees without moving import info. gridworld. actor. Bug;

The bug act method looks to see if a move is possible by calling can. Move looks at the location in front of this bug to see if it is empty or if it contains a flower. can. Move returns true or false.

{ } Grid <Actor> gr = get. Grid(); if (gr == null) return false; Location loc = get. Location(); Location next = loc. get. Adjacent. Location(get. Direction()); if (!gr. is. Valid(next)) return false; Actor neighbor = gr. get(next); return (neighbor == null) || (neighbor instanceof Flower); // ok to move into empty location or onto flower // not ok to move onto any other actor instanceof keyword checks to see if object is a certain type

The bug act method calls move if can. Move returns true. move calls move. To to move the bug to the location in front of this bug. move leaves a flower in the old location.

{ } Grid <Actor> gr = get. Grid(); if (gr == null) return; Location loc = get. Location(); Location next = loc. get. Adjacent. Location(get. Direction()); if (gr. is. Valid(next)) move. To(next); else remove. Self. From. Grid(); Flower flower = new Flower(get. Color()); flower. put. Self. In. Grid(gr, loc);

The bug act method calls turn if can. Move returns false. turn changes the direction of the bug by 45 degrees to the right.

{ } set. Direction(get. Direction() + Location. HALF_RIGHT);

Actor. World world = new Actor. World(); Bug dude = new Bug(Color. GREEN); dude. set. Direction(Location. EAST); Location loc = new Location(5, 5); world. add(loc , new Rock()); loc = new Location(2, 5); world. add(loc, new Flower()); loc = new Location(2, 7); world. add(loc, dude); world. show();



1. How will the new bug differ from the original bug? 2. Can the new behavior be created using existing methods? 3. Which of the methods will be overridden? 4. Will new methods need to be added?

• Override Bug's act method • Each call to the move() method should be guarded by a call to the can. Move() method • Add additional instance fields if needed • Add new methods to the subclass if needed • Constructors for the subclass call super() or super(some. Color)

What has to change if you want the bug to go backwards instead of forwards? Use the GW quick reference!


public class Backward. Bug extends Bug { //constructor public void act() { } //other methods } Is this the only way to write this class? What methods could be changed?


• Bug class: - constructor method act() can. Move() move() turn() • Grid class: - is. Valid() get. Num. Rows() get. Num. Cols() • Location class: - get. Row() - get. Col() - get. Adjacent. Location() • Actor class: - get. Grid() put. Self. In. Grid() remove. Self. From. Grid() move. To() • Box. Bug class: - overriding act() - using instance variables to control movement of a bug

- Slides: 33