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.



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

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. (Should this 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. It would be more interesting if, in each time step, a fish randomly decides whether to change direction before moving forward (unless it is at a wall, in which case it must change direction). We can use the RandGen 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 RandGen interface to discover how to randomly generate a 0 or 1. Modify your simulation function to have each fish randomly decide whether to change direction before moving forward (unless it is facing a wall, in which case it should always change direction before moving forward).

In a previous exercise you wrote the selection statement to change direction when a fish is facing a wall. You do not need to add any new selection statements to your code in order to implement the random behavior for this exercise. Just modify your selection statement to test for a more complex condition. Don't forget to include the header file for RandGen at the top of your file.


Stop and Think

Does it matter which expression comes first in your complex condition?



Looking Ahead:

If you run your program enough times you may discover that we have just introduced a logic error into our program. On rare occasions a fish may swim out through the left wall of the aquarium. We will explore this behavior further, and fix the underlying problem, in a later exercise.



Copyright Alyce Faulstich Brady, 1999.