Mini-Lab: Keep On Moving

Introducing Loops and Complex Conditions


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, 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 forward one step (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, using conditional statements and logical expressions (including the && and || operators), and the java.util.Random class.



Keep on Moving

Introduction

Our program would be more interesting if we had the fish move more than just once or twice. We can use Counted Repetition to allow the simulation to continue for several timesteps.

Exercise

  • Modify the main method in the AquaSimApplication class to make your program become a simulation of three fish moving in the aquarium over time. For this exercise, make your fish move 10 times. Choose one of the loop control styles above and use it correctly. Make sure that your loop body includes moving and displaying the fish.
  • Test your modified program.
  • Update the internal and external documentation for the AquaSimApplication class to reflect your changes.



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, Validated Input to ask the user to provide the desired number.

Exercise

  • It is possible to create an AquaSimGUI object that prompts the user for the number of times the fish should move (the number of steps for our aquarium simulation). Read the class documentation for the AquaSimGUI class, to learn how to use its two-parameter constructor. (You may need to read the Constructor Details to discover how to use the second parameter correctly.) Modify your program to use this constructor, updating the internal documentation as appropriate, and test your program.

  • Stop and Think

    Before you test your modified program, think clearly about what you think the modified behavior will be. Then test it. Does your program work as you expected? Why or why not? If it does not, does the difference reflect an error in your program or an error in your expectations?


    Stop and Experiment

    How "well-written and robust" is the Prompted, Validated Input provided by the AquaSimGUI graphical user interface? How useful is the prompt? How thorough is the validation? Try a number of different valid and invalid inputs to test this.
  • Now research the AquaSimGUI class to discover how you can find out what number of steps the user entered. Save the number of steps in a well-named variable, and then use it in the counted repetition for loop you created above.

  • Stop and Think

    Where's the earliest point in your program that it would make sense to ask for the number of simulation steps? Where's the latest point?
  • Test your modified program. As always, identify your expected results before testing.
  • Update the internal and external documentation to reflect your changes.



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.