CS 132: Intro to Computer Science II

Spring 1999

Assignment 4

Ant Races: Modifying and Using Classes

Due Monday February 22

In this assignment, you will work with the Ant class, which simulates an ant (represented as a colored dot) running in a particular direction. The ant can be graphically shown on a drawing frame starting from the bottom and moving towards the top. Here's a short fragment of code that simulates the movement of one ant:

   AntFrame frame = new AntFrame();
   Ant ant1 = new Ant(Color.red, 1, frame);
   ant1.move();
   ant1.move();
   if (ant1.won()) {
      System.out.println("This ant reached its goal!");
   }

As you can see, the constructor takes arguments that allows you to specify the color of the ant and its placement on the drawing frame (ie. the track number). To see the movement, the constructor needs to have the reference to the drawing frame.

The Ant class also has the move method, which advances the ant a random distance, the maximum of which is set by the constant DEFAULT_MAX_JUMP (defined in Ant.java). Once the ant has reached its goal at the top of the screen, the won method returns true whenever it is called with an ant.

Copy the files Ant.java, AntRace.java, and AntFrame.java from ~miller/cs132/lab4. As it now stands, the program in the AntRace.java file has the code that demonstrates the above example. You can compile and run it to see it work.

Part 1

Modify the AntRace.java program so that it simulates a race between two ants. Place one of the ants in track 1 and the other in track 2. Repeatedly have them move until one of them has won the race.

Part 2

Add a new method to the ant class that allows the client (ie. the code int the AntRace program) to specify the maximum jump distance for the ant. Call this method setMaxJump. Then modify the AntRace program so that the two racing ants have different maximum jump distances. Naturally, the ant with the larger maximum jump should win most of the time.

Part 3

So far, the ants only move straight towards the goal. Modify the Ant class to incorporate a "stagger" element. That is, the ant may move horizontally a randomly determined distance. Create a new constant called DEFAULT_MAX_STAGGER that specifies how far an ant may wander horizontally with each move. Then modify the move method so that with each move, the ant staggers horizontally a randomly determined distance but limited by the value of DEFAULT_MAX_STAGGER. You should randomly determine if the ant should stagger to the left or the right. The change in the X location should be determined by the amount of the stagger. The change in the Y location can be determined with the following formula:
   sqrt(jump * jump - stagger * stagger)

Part 4

Create a new method called setMaxStagger that allows the client to specify the maximum stagger distance for a particular ant. If you haven't already, you will need to declare an instance variable that stores the maximum stagger distance. Experimentally determine two pairs of significantly different values that give the pair of ants an even race.

Submission

Turn in printouts of AntRace.java and Ant.java.