Aquarium Lab Series    

Mini-Lab: Keep On Moving

Introducing Loops and Complex Conditions

Alyce Brady
Kalamazoo College


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 step (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, using conditional statements and logical expressions (including the && and || operators), and the RandNumGenerator class.

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 once or twice. We can use the Counted Repetition pattern to allow the simulation to continue for several timesteps.

Exercise: Simulate Fish Moving Forward

  • Modify the main method to make your program 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. Make sure that your loop body includes moving and displaying the fish (and pausing the animation long enough for you to see it).



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.

Exercise: Allow User to Control Simulation Steps

  • The ValidatedInputReader class puts up a dialog box displaying an initial prompt, waits for the user's response, and validates it (the response must be a valid integer). Modify your program to prompt the user for the number of simulation steps by inserting a statement like the following into your code:
    int namedVar = ValidatedInputReader.getInteger("initial prompt", number, "clarification prompt");
    where namedVar is a local variable with an Intention Revealing Name, the initial prompt string is the initial prompt to the user, the number is a suggested default value and, the clarification prompt string is a follow-up prompt for input after the user has entered invalid data.

  • Now use the number specified by the user to control your loop.


    Stop and Think

    Where's the earliest point in your program that it would make sense to ask for the number of simulation steps? Where's the latest point?

  • Test your modified program.


    Stop and Experiment

    How "well-written and robust" is the implementation of the Prompted Input pattern in ValidatedInputReader? What happens if the user types in something other than what the program expects? (What kind of input do you think the program should expect?)



Showing an Independent Spirit

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 a complex boolean expression to check for two conditions, whether the fish is at a wall, and so must move, or randomly chooses to move even though it is not at a wall.

Exercise: Fish Doing Their Own Thing

  • Review your use of the RandNumGenerator class to determine how to calculate a one in four chance of changing direction for any given fish. Modify your main method to introduce a more complex check: each fish should change direction if it is up against a wall or if it randomly chooses to change direction.

    Note: Use the same RandNumGenerator object you constructed earlier to influence fish color.

  • Test your modified program.

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



Copyright Alyce Faulstich Brady, 2001-2002.