function:RenderSidePart pageleftbodycaption pageleftbody sidenote Main.GridWorld2-SideNote Main.SideNote Site.SideNote

# Review Exercises

1. Set 2, Nos. 1-7, page 12

The source code for the BoxBug class is in Appendix C.

1. What is the role of the instance variable sideLength?
2. What is the role of the instance variable steps?
3. Why is the turn method called twice when steps becomes equal to sideLength?
4. Why can the move method be called in the BoxBug class when there is no move method in the BoxBug code?
5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?
6. Can the path a BoxBug travels ever change? Why or why not?
7. When will the value of steps be zero?
1. CircleBug, page 12
2. SpiralBug, page 12
3. ZBug, page 13
4. DancingBug, page 13 Write a class DancingBug that “dances” by making different turns before each move. The DancingBug constructor has an integer array as parameter. The integer entries in the array represent how many times the bug turns before it moves. For example, an array entry of 5 represents a turn of 225 degrees (recall one turn is 45 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 last turn in the array, it should start again with the initial array value so that the dancing bug continually repeats the same turning pattern.

The DancingBugRunner class should create an array and pass it as a parameter to the DancingBug constructor.

import 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 DancingBug(turns);
ballerina.setColor(Color.ORANGE);
world.show();
}
}


# Programming Exercises

## JumperRunner.java

import info.gridworld.actor.*;

/**
* This class runs a world that contains a jumper, a bug, a flower, and a
* rock added at random locations.
*/
public class JumperRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
world.show();
}
}


## CircleBugRunner.java

import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;

/**
* This class runs a world that contains box bugs. <br />
* This class is not tested on the AP CS A and AB exams.
*/
public class CircleBugRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
CircleBug a = new CircleBug(2);
a.setColor(Color.BLUE);

a.setDirection(Location.NORTHEAST);
world.show();
}
}


## CritterRunner.java

import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Critter;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Location;

import java.awt.Color;

/**
* This class runs a world that contains critters.

* This class is not tested on the AP CS A and AB exams.
*/
public class CritterRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
world.show();
}
}


## GridTest.java

import info.gridworld.grid.*;
import java.awt.Color;

/**
* This class runs a world that helps test out answers to Set 4.
*
*/
public class GridTest
{
public static void main(String[] args)
{
BoundedGrid gr=new BoundedGrid(10,10);
Location loc1 = new Location(4,3);
Location loc2 = new Location(11,4);

System.out.println("Is 4,3 in gr? " + gr.isValid(loc1) );
System.out.println("Is 11,4 in gr? " + gr.isValid(loc2) );

}
}


## ReverseBoxBugRunner.java

import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;

/**
* This class runs a world that contains box bugs. <br />
* This class is not tested on the AP CS A and AB exams.
*/
public class ReverseBoxBugRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
ReverseBoxBug a = new ReverseBoxBug(2);
a.setColor(Color.BLUE);