Mini-Lab: Keep On Moving

Introducing Loops and Complex Conditions


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



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.