Grid World 2

<< GridWorld 1 | HomeworkTrailIndex | GridWorld 3 >>

Bug Variations

Chapter 2 Text

Download a Answer Form

Reference Guide You'll have to use during the exam

The Gridworld code

Complete Text


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.add(new Location(9, 9), ballerina); 
            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.add(new Jumper()); 
    world.add(new Rock()); 
    world.add(new Bug()); 
    world.add(new Flower()); 
    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);

        world.add(new Location(7, 2), a);
        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.add(new Location(7, 8), new Rock());
        world.add(new Location(3, 3), new Rock());
        world.add(new Location(2, 8), new Flower(Color.BLUE));
        world.add(new Location(5, 5), new Flower(Color.PINK));
        world.add(new Location(1, 5), new Flower(Color.RED));
        world.add(new Location(7, 2), new Flower(Color.YELLOW));
        world.add(new Location(4, 4), new Critter());
        world.add(new Location(5, 8), new Critter());
        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);

        world.add(new Location(7, 2), a);
        a.setDirection(Location.NORTHEAST);
        world.show();
    }
}