Mini-Lab: More Color, More Freedom, More Interesting!

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.



Rainbow Fish

Introduction

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, and LIGHT_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.



Playing Favorites

Introduction

We don't have to make every color equally likely; after all, fish may have a higher likelihood of being some colors than others.

Stop and Think

  • In the Multiple Alternatives example above the Rainbow Fish exercise, what is the probability that the student takes a lemon square? An apple? A slice of apple pie? Express your answer in terms of percentages.
  • In the Uneven Probabilities example above, for what values of randNum would the student take a lemon square? An apple? A slice of apple pie? What does this code do when randNum has the value 9?
  • Now describe the behavior of the Uneven Probabilities example in terms of percentages.
  • How would the behavior of the Multiple Alternatives code be different if the first two else keywords were left out; in other words, if it were a sequence of separate, independent conditional statements?
  • How would the behavior of the Uneven Probabilities code be different if both else keywords were left out; in other words, if it were a sequence of separate, independent conditional statements?


Optional Exercise

  • Just for fun (and only if you feel you have time): Modify your program to give your fish different probabilities of being constructed different colors. Construct additional fish to test this modification. Make sure that you have updated the program documentation at the top of the file.



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).

Exercise

  • Review your use of the Random class to determine how to calculate a one in four chance of changing direction for any given fish. Modify the Aquarium Simulation program so that each fish changes direction if it is up against a wall or if it "chooses" to do so (it has a one in four chance of "choosing" to change direction).

  • Stop and Think

    • Where should you add your complex expression?
    • Do you need to construct a new Random generator object for this exercise, or is there a generator object that is accessible to you already?
    • Should you use an AND (&&) or an OR (||) operator in your complex condition?
    • Does it matter which expression comes first in your complex condition?

  • Test your modified program.
  • Update the appropriate internal and external documentation to reflect your changes.