Mini-Lab: A Whirl of Color

Using Conditional Statements


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, 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 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 for this set of exercises.



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 a Simple Conditional statement to determine when a fish should turn around and start swimming the other direction.

Stop and Think

  • Which statements in Example 1 will be executed when there are no lemon squares? In what order will they be executed? Which statements will be executed, and in what order, when there are lemon squares?
     

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. (Stop and Think: where is the statement that sets the dimensions of the aquarium? In what class, and what method?) "Comment out" the original, and change the dimensions in the copy to be 100 x 200. 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 AquaFish specification to find out how to determine whether a fish is at a wall and how to make it reverse direction. Modify the main method to have each fish check whether to change direction whenever it moves forward. (Stop and Think: You could check whether to reverse direction or not and then move forward, or you could move forward first and then check whether to reverse direction. Does the order matter? Consider three cases: a) for a fish that was constructed along the left wall, b) for a fish that was constructed in the middle of the aquarium, and c) for a fish that was constructed along the right wall. Now ask yourself again: Does the order matter?)
  • 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 standard Java Random class to do this.

Exercise: Random Red and Blue Fish

  • Edit your main method to construct a random number generator right before you construct your three fish. Give your new variable a name that conveys its purpose. (Note: The AquaSimApplication class has an import java.util.Random; statement at the top of the file; this allows you to use the standard Java Random class even though it isn't one of the classes you defined.)
  • After you construct your random number generator and above the code that constructs your three fish, create a new integer variable that can store the a random number. Don't actually generate the random numbers yet, though. Give your new variable a name that conveys its purpose. For example, your variable declaration might look like the following line.
      int randNum;
  • Just above the statement that constructs each fish, set your random number variable to a different random number (0 or 1). Then use the variable in an if statement to randomly construct each fish as either Color.RED or Color.BLUE.
  • Identify in advance what behavior you expect from your program when you test it. Do you know which fish will be which color? Do you know how many fish you should get of each color? Test your program to make sure that your results are what you expect.



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.

Exercise

  • Modify your program to create fish with the colors of the rainbow. Each fish should have an equal probability of having one of the following colors: Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, or Color.MAGENTA. (There are no Color constants for indigo and violet.)
  • 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 Example 1, 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 Example 2, 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?
  • Overall, what behavior does Example 2 in the table above simulate? Express your answer in terms of percentages.
  • How would the behavior of the code in Example 1 be different if the first two else keywords were left out; in other words, if it were a sequence of separate, independent conditional statements? Could you write code that would have the same behavior as Example 1 without any else keywords?
  • How would the behavior of the code in Example 2 be different if both else keywords were left out; in other words, if it were a sequence of separate, independent conditional statements? Could you write code that would have the same behavior as Example 2 without any else keywords?
  • How would the behavior of the code in Example 1 be different if it repeatedly called the nextInt method and checked its return value against different values instead of calling nextInt once, saving the random number in a variable, and checking the variable against different values? In other words, if there were no randNum variable, and each conditional expression called the nextInt method instead?


Exercise

  • Just for fun (and only if 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.