More Practice with Conditions and Random Numbers
This set of Mini-Lab Exercises is part of 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 in the Aquarium Lab Series contains an Introduction to a problem or task, descriptions or examples of one or more Concepts to apply 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 several steps (changing the fish's direction when it is about
to hit a wall), and displayed them graphically. Students should be familiar
with using conditional statements and
logical expressions
(including the &&
and ||
operators), and the
java.util.Random
class.
Why should there only be two colors of fish in our aquarium? We can use multiple alternatives to add diversity of color when constructing and displaying fish.
Concept: Multiple Alternatives
Sometimes there are several alternative actions, only one of which should be performed, depending on a set of conditions.
For example, consider modifying our dessert example to represent a student randomly choosing from among 4 dessert choices every day.
Multiple Alternatives Random generator = new Random(); // get a new rand. num. generator int randNum = generator.nextInt(4); // get a random number between 0 and 3 student.takeASandwich(); student.takeCoffee(); if ( randNum == 0 ) // 1 in 4 chance that it is 0 { student.takeALemonSquare(); } else if ( randNum == 1 ) // 1 in 4 chance that it is 1 { student.takeAnApple(); } else if ( randNum == 2 ) // 1 in 4 chance that it is 2 { student.takeApplePie(); } else // randNum must be 3 { student.takeACookie(); } student.sitDownAndEat();
Exercise: Rainbow Fish
Modify your program to create fish with randomly chosen colors. Include at least 5 of the 13 colors built in to the
Color
class:Color.RED
,Color.ORANGE
,Color.YELLOW
,Color.GREEN
,Color.BLUE
,Color.MAGENTA
,Color.CYAN
,Color.PINK
,Color.BLACK
,Color.WHITE
,DARK_GRAY
,GRAY
, andLIGHT_GRAY
.If you choose to use 5 colors, you would want to randomly get one of the 5 numbers from 0 to 4. If you use all 13 colors, get one of the 13 numbers from 0 to 12.
NOTE: Doing this for 3 fish will lead to "unsightly code buildup"! We'll see ways to clean this up in the next set of exercises.
- 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.
We don't have to make every color equally likely; after all, fish may have a higher likelihood of being some colors than others.
Consider modifying the cafeteria lunch example, giving different probabilities to different desserts. What does the code in this example do?
Uneven Probabilities |
---|
|
Stop and Think
Optional Exercise
|
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 write a complex boolean expression using the AND (&&) or OR (||) operators to check for two conditions, whether the fish is at a wall and so must change direction, or has randomly chosen to change direction even though it is not at a wall.
Exercise
|