Start with the Loop

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:

With ten calls to drawOval(). Or, better yet, with a call to drawOval() inside of a loop body that is iterated 10 times.

Start with the Loop

Here is a start on an applet that uses a loop to draw ten red circles.

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 = ;

    while (  )
    {


      
    }

  }
}

The above program skeleton has most of what is needed. It has:

  • The import statements.
  • The header for the class definition.
  • The important constants declared.
  • The header for the paint() method.
  • The nearly completed loop.

It is often a good idea to think about what you are doing one piece at a time. Often it is wise to first examine the main loop of the program.

QUESTION 10:

Fill in the blanks so the loop is correct for a program that is to draw ten circles.