We continued our discussion of the repetition (loop) structure with some examples. These examples also illustrate the use of counter variables (to count of course) and accumulation variables (to sum).
//File: Score.java
import CSLib.*;
//This program asks the user to enter scores between 0 and 100.
//The program then outputs the number of scores and the average of the scores.
public class Score
{
public static void main (String [] args)
{
//Declare variables
InputBox in = new
InputBox("Average Score");
int score,
sum=0,
count=0;
while (true)
{
//Get input from user
in.setPrompt("Enter score: (OK to quit)");
score = in.readInt();
if
(in.eoi())
{
break; // out of the loop
}
count = count + 1;
//increment the counter
sum = sum + score;
//accumulate the sum of the scores entered
}
//Output the results
OutputBox out = new
OutputBox("Average Scores");
out.setSize(300, 100);
out.println ("The average of the " +
count + " scores is: " + sum/count);
}
}


An alternative version removes the if statement that ends the loop and instead has the while test for continuing the loop as follows:
//File: ScoreAlt.java
import CSLib.*;
//This program asks the user to enter scores between 0 and 100.
//The program then outputs the number of scores and the average of the scores.
public class ScoreAlt
{
public static void main (String [] args)
{
//Declare variables
InputBox in = new
InputBox("Average Score");
int score,
sum=0,
count=0;
//Get input from user
in.setPrompt("Enter score: (OK to
quit)");
score = in.readInt();
while (!in.eoi()) //NOT end of
input
{
count = count + 1;
//increment the counter
sum = sum + score;
//accumulate the sum of the scores entered
//Get input from user
in.setPrompt("Enter score: (OK to quit)");
score = in.readInt();
}
//Output the results
OutputBox out = new
OutputBox("Average Scores");
out.setSize(300, 100);
out.println ("The average of the " +
count + " scores is: " + sum/count);
}
}
The interaction with the user is identical with that of the original program.
Lab Exercise. Write a program that sums the integers from 1 to n, where n is provided by the user. For example, if the user enters 6 (for n), then your program will provide the results of
1 + 2 + 3 + 4 + 5 + 6 = 21
HINT: This program will require both a counter variable and an accumulation variable (that sums the counter). If you get frustrated, you can look at my solution.
Finally, we contemplated the joys of graphics. In particular, the following program was provided for us by the authors
//File: Surprise.java
import CSLib.*;
import java.awt.*;
public class Surprise
{
// Surprise with an exploding balloon
// Author: Rachel McDermott, September 27, 1996
DrawingBox board;
int width,
height,
dmax; // The
maximum diameter circle
public void setup ()
{
board =
new DrawingBox("Surpriser");
width =
board.getDrawableWidth();
height =
board.getDrawableHeight();
dmax =
(int)Math.min(width, height);
}
public void draw ()
{
board.setColor(Color.yellow);
int diameter=0;
while (diameter < dmax)
{
board.fillOval((width-diameter)/2, (height-diameter)/2,
diameter, diameter);
Timer.pause(10);
// sleep for 10 msec
diameter++;
}
board.setColor(Color.red);
board.drawString("Surprise!",
(width-53)/2, (height+14)/2);
}
}
This produces the following after growing from an insignificantly small circle
Try it by running SurpriseClient.java.
Lab Exercise. Modify the Surprise program so that after drawing the balloon, it pauses for 1 second and then erases the balloon from the outside inward. By drawing successively smaller and smaller unfilled circles in white (the color of the background), you can achieve the effect of collapsing the balloon.