# Notes on Session J5: Control Structures for Repetition

Experiment J5.5, Step 5. The applet now draws the given message twenty times. However, since we don't change the value of any of the variables, the message appears twenty times in the same place. Hence, the output of the applet will be the same.

Experiment J5.5, Step 8. To support more variation in the applet, use something like the following when updating the colors

```      // Update the colors.
redPart = redPart + changeRed;
greenPart = greenPart + changeGreen;
bluePart = bluePart + changeBlue;
```

These lines belong within the `for` loop.

Experiment J5.5, Step 9. Eventually, one or more of the components (`redPart`, `greenPart`, or `bluePart`) will exceed 255 (or fall below 0). The `java.awt.Color` class does not permit this, and will report an error to the browser or applet viewer. Think about how you might deal with this problem.

Experiment J5.5, Step 12. The seemingly complicated math is not that complicated. Basically, we're drawing part of a parabola. As you may recall, a parabola is given by a quadratic equation. In this case, we make the vertical component depend on the square of the step number (`tmp*tmp`) and the horizontal component linear in the step number. We make the peak of the parabola at the middle by subtracting `steps/2` from the `step` before squaring. (Hence, that value will be zero when we are at the middle of our steps.) We divide by `steps*steps` and multiply by 4 to scale the value (the largest value should be approximately `(steps/2)*(steps/2)`, so we divide by that value). We multiply by 100 so that we end up with a value between 0 and 100 (more or less).

You may wish to try variants of these values to see what effects they have.

Experiment J5.6, Step 2. A typical strategy will involve nested `for` loops. We step through the rows (using a `for` loop). Within each row, we step through the columns (again, using a `for` loop). For example,

```    // For each row ...
for (int row = 1; row <= rows; row = row + 1) {
// Draw all the grid points on this row.
// For each column ...
for (int col = 1; col <= cols; col = col + 1) {
// Draw the grid point for the given row/column.
...
} // for each column
} // for each row
```

Experiment J5.6, Step 5. Your new code should resemble the following.

```    // Read the parameters from the HTML page.
String rowString = getParameter("rows");
String colString = getParameter("columns");
String spacingString = getParameter("spacing");
try { rows = Integer.parseInt(rowString); }
catch (Exception e) { rows = 1; }
try { cols = Integer.parseInt(colString); }
catch (Exception e) { cols = 1; }
try { spacing = Integer.parseInt(spacingString); }
catch (Exception e) { spacing = 10; }
```

Experiment J5.6, Step 6. Although we are reading the number of rows and columns, we are not using them within our code. More precisely, the `for` loops use specified ending values, rather than the variables `rows` and `cols`.

Experiment J5.6, Step 7. As you may have noted if you looked closely, we have seven rows and seven columns. What might be wrong? It could be that the first row and the first column fall outside the applet's window. It could be that we're counting wrong. You might verify that the first is not the problem by using a drawing command that you are sure will place the points within the screen (e.g., by adding 10 to horizontal and vertical positions). In fact, because our `for` loops use `<` rather than `<=`, they stop one step too soon.

Experiment J5.6, Step 9. It appears that we've drawn five rows and ten columns, rather than ten rows and five columns. Obviously, we've gotten something backwards.

Experiment J5.7, Step 8. Because our simulation currently allows the ball to go below the bottom of the area, it is possible that it remains below the bottom, even after it starts upward. We want to make sure that the velocity is always negative (upwards) when the ball is below the bottom. In a post-laboratory problem, you will have the opportunity to consider how to prevent the ball from going below the bottom.

Experiment J5.7, Step 9. When the ball reaches the right edge, it should start moving towards the left (a negative horizontal velocity). Hence, we use

```      if (horiz > right) {
horizVelocity = -Math.abs(horizVelocity);
}
```

When the ball reaches the left edge, it should start moving toward the right (a positive horizontal velocity). Hence, we use

```      if (horiz < left) {
horizVelocity = Math.abs(horizVelocity);
}
```