Loop Options

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.

Answer:

An answer is given below.

Loop Options

The loop is a counting loop. Each iteration of the loop will draw one red circle.

import javax.swing.JApplet;
import java.awt.*;

// assume that the drawing area is 300 by 150
public class TenCircles extends JApplet
{
  final int width = 300, height = 150;

  public void paint ( Graphics gr )
  { 
    int count = 0;

    while ( count < 10 )
    {


      count = count + 1;
    }

  }
}

There are many ways to fill in the blanks so the loop iterates 10 times, but usually it is best to have a counter that goes from 1 to 10, or from 0 to 9. Often the arithmetic in the loop body is easier when the loop counts from 0 to 9.

Now let us focus on drawing the circles. The idea is to draw 10 circles in a row, nicely spaced out across the screen.

QUESTION 11:

The applet's drawing area is 300 pixels wide. If there are to be 10 circles, how wide is the area which will be used for each circle?