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

### Concept: Counted Repetition

When writing a program, we sometimes want to repeat a given action or set of actions a known number of times. The number of times to repeat the action, which we call N, might be a constant number or a value in a variable.

The Java `for` statement (also known as a `for` loop) provides an easy way to do this. The structure of a `for` statement is shown below. To repeat a set of actions, enclose the set in curly braces, just as you would do for multiple actions in a conditional statement.

``` ```for ( <initialization> ; <condition> ; <step> )
```        `<repeated action>````

There are several ways to use a `for` statement, each of which is useful for different purposes. For a counted repetition, we create a variable to count the number of times the action has been repeated so far. The counting variable is created and initialized and then, so long as the action has not been repeated too many times (the "condition"), the action is done and the count is incremented (the "step"). There are two common ways to do this: counting from 1 to N, or counting from 0 to N-1. The second approach may not seem as intuitively obvious as the first, but it is very useful in other contexts (such as stepping through all of the items in a list).

For example, consider a code fragment that deals a single card to each player in a card game. Both examples below repeat the `dealCard()` action `nbrPlayer` times, so `nbrPlayer` is the N in this example.

Counting from 1 to N
(Repeats N times)
Counting from 0 to N-1
(Repeats N times)
``````for ( int i = 1; i <= nbrPlayers; i++ )
dealCard();``````
``````for ( int i = 0; i < nbrPlayers; i++ )
dealCard();``````

Notice that both styles express the condition in terms of N (`nbrPlayers`), but one uses the less than or equal to (`<=`) operator, while the other uses the less than (`<`) operator.
WARNING: It is very important not to mix these two approaches, or you will execute the actions in your loop the wrong number of times!

Error Counting from 1
(Repeats N-1 times)
Error Counting from 0
(Repeats N+1 times)
``````for ( int i = 1; i < nbrPlayers; i++ )
dealCard();``````
``````for ( int i = 0; i <= nbrPlayers; i++ )
dealCard();``````

These are known as off-by-one boundary errors, and are very easy to make. To avoid these kinds of mistakes, many people choose to memorize and consistently use the approach counting from 0 to N-1, since that is the approach needed to step through lists of items.

`for ( int i = 0; i < N; i++ )`

The conditional expression in the `for` statement can be written as either a positive or negative expression. Positive Conditions are generally easier to read, though.

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

### Concept: Prompted, Validated Input

When we want to get some input interactively from the user, we need to prompt the user for the input, read the input, and verify that the input meets the expectations of the program. For example, if a program needs a student's name and student ID, then the prompts should make it clear when the program is asking for a name and when it is asking for a student ID. If the student id is always six numeric digits, then the program should check that the ID provided by the user is six numeric digits, and not a alphabetic string, floating point number, or other type. If the input does not meet the expectations of the program, the user should be provided with another opportunity to enter the data, possibly with a new prompt that provides clearer instructions about what the expectations are.

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