Aquarium Lab Series    

Mini-Lab: Staying in Bounds

Using Conditional Statements

Alyce Brady
Kalamazoo College


This set of Mini-Lab Exercises is the second 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 created three fish, moved them forward one step, and displayed them graphically. Therefore, students should be familiar with constructing objects, using variables, and invoking methods. Some familiarity with logical expressions is also required, since the Selection Patterns use conditions.

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



About Face!

Introduction

In our current program, fish move to the right and then get stuck at the right wall. This does not make sense. We can use the Whether or Not selection pattern to change direction when a fish gets to the wall.

Exercise

  • In your previous testing of the program you may or may not have noticed that fish swim only to the right and get stuck at the right wall. To verify the problem, make a copy of the statement that sets the dimensions of the aquarium. "Comment out" the original, and change the dimensions in the copy to be 100 x 480. Copy the code that moves and redisplays the fish to let them move a second time. Now run the program several times and make sure you see the problem.
  • Research the abbreviated AquaFish specification to find out how to determine whether a fish is at a wall and how to make it turn around. Using the Whether or Not Selection pattern, modify your main method to have each fish reverse direction if it is at a wall. Then move the fish forward, whether it reversed itself or not. Test your program in the narrower aquarium you created above. When you are satisfied that your program is behaving correctly, restore the aquarium to its original size.



One Fish, Two Fish, Red Fish, Blue Fish

Introduction

Our aquarium is a little boring, since the fish are all the same color. We could specify the color of each fish as we construct it, giving each fish a different color. Or, to make things more interesting, we could decide on the color of each fish based on a random number. We can use the Alternative Action selection pattern and a new class, the RandNumGenerator class, to do this.

Exercise

  • Research the RandNumGenerator specification to discover how to construct a random number generator. A RandNumGenerator object can be used to simulate a coin toss or other random selection. Edit your main function to construct a random number generator right before you construct your three fish. Give it an Intention Revealing Name.
  • Create a new variable that will be a reference to an object of the Color class, but don't actually construct or initialize a Color object. Give your variable an Intention Revealing Name. For example, your variable declaration might look like the following line.
      Color fishColor;
  • Research the RandNumGenerator specification to discover how to randomly generate one of two values (0 or 1). Before you construct each fish, generate a random 0 or 1 first. Using the Alternative Action Selection pattern, set your Color variable to Color.RED if the random number is 0; set it to Color.BLUE otherwise. (Color.RED and Color.BLUE are constant Color values defined in the Java Color class.)
  • Research the abbreviated AquaFish specification to discover how to specify the color of a fish as you create it. Modify your code to specify the color of each fish using your Color variable.
  • Test your program to make sure that your results are what you expect.

  • Stop and Think

    What if all three fish are the same color? Does that indicate a programming error? Why or why not? What if all three fish are the same color (sometimes all red, sometimes all blue) every time you run the program? What programming error would cause that error?



Rainbow Fish

Introduction

Why should there only be two colors of fish in our aquarium? We can use the Sequential Choice selection pattern to add diversity of color when constructing and displaying fish.

Exercise

  • Modify your program to create fish with the colors of the rainbow. This time, before you construct each fish, generate a random integer less than 6 (in the range of 0 to 5) first. Using the Sequential Choice selection pattern, set your Color variable according to the following table:
    Random Integer Color
    0 Color.RED
    1 Color.ORANGE
    2 Color.YELLOW
    3 Color.GREEN
    4 Color.BLUE
    5 Color.MAGENTA
    Do not generate a new random number for every test; just generate one random number for each fish and test it against the various integer values. You may want to use cut and paste to reinitialize the Color variable for the second and third fish! (This table uses Color.MAGENTA because Color.INDIGO and Color.VIOLET are not defined colors.)
  • Test your program to make sure that your results are what you expect. (What results were you expecting? What tests are necessary to make sure the results are what you expect?)
  • Make sure that you have updated the program documentation at the top of the file to reflect your modifications.



Copyright Alyce Faulstich Brady, 2001-2002.
The Selection Pattern was written by Joe Bergin, of Pace University.