Mini-Lab: Keep On Moving

Using For Loops


This set of Mini-Lab Exercises is the third in a series in which students build a small program with several fish moving around in an aquarium. The set includes the following exercises:

Each section contains an Introduction to a problem or task, (usually) abridged versions of one or more Patterns that will be useful in solving the problem or completing the task, and an Exercise.

In the exercises that precede this one, students will have created three fish, moved them forward one space (changing the fish's direction when it is about to hit a wall), and displayed them graphically. Students should be familiar with constructing objects, invoking methods, and using conditional statements and logical expressions.

Students should read over the patterns that appear in this document before the lab.



Keep on Moving

Introduction

Our program would be more interesting if we had the fish move more than just the one time. We can use the Counted Repetition pattern to allow the fish to move for several time slices.

Exercise: Simulate Fish Moving Forward

Modify your main program to become a simulation of three fish moving in the aquarium over time. Initially, set the number of time steps in the simulation to 10. Choose one of the loop control idioms above and use it correctly. Don't forget to use pause to slow down the animation enough for you to see it.

You do not need to print your statistics about how many fish are facing each direction and whether one of your fish has moved left, right, or not at all after each move. Instead, calculate these once after the simulation is complete.



Put the User in the Driver's Seat

Introduction

Our program would be more flexible if we allowed the user to specify how many times they want the simulation to run. We can use a Prompted Input to ask the user to provide the desired number. The Keyboard class provides a number of methods for getting Prompted Input from the user.

Exercise: Allow User to Control Simulation Steps

Research the Keyboard specification to discover how to prompt the user for an integer value. Notice that the Keyboard methods are static methods (like the main function in FishAnimation.java). This means that rather than construct a Keyboard object, you will invoke the method on the class itself. For example, you could prompt the user for a name with the following:
    String name = Keyboard.readString("Please type your name.  ", "J. Doe");
    
The second string passed to the readString method is a default value. Passing a default value prevents a certain kind of Java error that we don't want to deal with just now.

Modify your program to ask the user how many times they want the simulation to run, and then use that number to control your loop. Provide a default value (such as 10) when you prompt the user. (Question: Should your prompt appear before or after your message welcoming the user to the aquarium program?)



Random Behavior

Introduction

Our three fish are moving in lock-step with one another, more like a marching band than fish. A fish only changes direction when it comes to a wall. It would be more interesting if, in each time step, a fish randomly decides whether to change direction before moving forward. (Of course, if it is at a wall, it will always change direction). We can use the Random class to simulate a coin toss (or other random selection between two values) by specifying that we want a random integer in the range 0 to 1.

Exercise

Research the TempRandom specification to discover how to construct a random number generator and how to use it to randomly generate one of two values (0 or 1). Modify your simulation function to have each fish randomly decide whether to change direction before moving forward. The fish, of course, should also change direction if moving forward means swimming into a wall or out of the aquarium.
    Notes:
    • TempRandom is a temporary version of the Java Random class that provides a useful variation of the nextInt method that is not yet available in the built-in class.
    • Construct a single TempRandom object before your simulation loop to use throughout your main function.
    • If you would like to see an example of the use of TempRandom, you can look at the internals of the AquaFish constructor. There is no need for your TempRandom variable to be static, however.

Make sure that you have updated the program documentation at the top of the file to reflect your modifications.



Copyright Alyce Faulstich Brady, 2000.