CSCI 201 -- Iteration

Repetition is something at which computers excel. In this lab, you will learn how to use Java's repetition structure -- the loop

Repetition in Java

What do you think of when you hear the word Repetition? Doing the same thing over and over? While that might be the literal definition, doing the exact same thing over and over isn't very useful:

    1 + 1 = 2
    1 + 1 = 2
    1 + 1 = 2
    1 + 1 = 2

Now, that wasn't useful. Howeever, if you allow a little feedback, repetition does become quite useful. Rather then adding 1 to itself again and again, this time add the result of the last addition to itself.

    1 + 1 = 2
    2 + 2 = 4
    4 + 4 = 8
    8 + 8 = 16
    16 + 16 = 32

Now we have some useful work being done.

Iteration in Java

In Java we have two forms of iteration, or repitition. One is the while loop. The while loop is done over and over again, until a desired result it accomplished. For example, suppose we need to generate a paycheck for every employee and that employee records are contained in a file called payroll.dat. The while loop for this task would look something like the following pseudo-code.

    while (MoreEmployees("payroll.dat")) {
        EmployRec ER = ReadEmployeeData("payroll.dat") ;
	GeneratePayCheck(ER) ;

A boring example for elementary school

Let's look at an example. Perhaps some of you were given the task of writing the numbers from 1 to 1000 when you were a student at Penrose Elementary School. (Or maybe not...)

This task can easily be accomplished with a single loop in Java.

   int i=1;
   while (i<=1000) {
     System.out.println(i) ;
     i = i+1 ;

Suppose a few years later, you are asked to write all the squares up to 10000 -- 1, 4, 9, 16, ...., 9604, 9801, 10000. The good news is that there are only 100 of them. The bad news is that you have to multiply 100 different numbers. Fortunately, you know Java.

   int i=1;
   while (i*i<=10000) {
     System.out.println(i*i) ;
     i = i+1 ;

Java for loop

Java also has another form of iteration called the for loop. The for is very good for writing repetitive mathematical computations. It puts the loop initialization (i = 1), test (i<=1000), and update (i = i+1) into a single statement, as shown in the following examples:

   for (i=1; i<=1000; i=i+1) {
     System.out.println(i) ;
   for (i=1; i*i<=10000; i=i+1) {
     System.out.println(i*i) ;

Being even more terse

Experienced Java programmers might express this even more succintly by using the ++ operator and by taking advantage of the fact that a for loop with a one-statement body doesn't need the curly braces.

   for (i=1; i<=1000; ++i)
     System.out.println(i) ;
   for (i=1; i*i<=10000; ++i)
     System.out.println(i*i) ;

The null statement pitfall

Programmers become accustomed to adding semi-colons to the end of lines and may accidently produce the following:

   for (i=1; i<=1000; ++i) ;
     System.out.println(i) ;

This is legal Java. The semi-colon at the end of the for is actually a null statement. It means that nothing is done in the body of the for. It is just like writing the following piece of code which only outputs the number 1001. See if you can figure out why.

   for (i=1; i<=1000; ++i) {
   System.out.println(i) ;

More on for

Loops are very simple, yet powerful. There are three steps required to set up a loop. First, you initialize the loop variable. Next, you provide the loop test. Finally, you put in the update statement. This whole sequence of events is illustrated by the following Stack N' Heap:

int i = 1;
while (i < 32)
i = i + i;

Downloading the project framework

Download, a ZIP file containing a NetBeans project named LoopPaint and unZIP this project into your csci/201 directory. Try to make your Projects panel look something like the following picture before continuing.
LoopPaint Projects panel

Modifying the project

The framework for the project

Again, the "look-and-feel" of this project is very similar to that of the Plot2D lab. You select a series of graphical displays within a frame. This time the frame displays a set of tiles or cells. You have to write a loop that "turns on" certain tiles.

Go ahead and compile and run the project and use the radio buttons to select and view the displays created by the program.

Notice that the program begins with statements to import Switcher, SpecLoopPaint, and FrameLoopPaint from the edu.unca.cs.csci201.LabAids package. FrameLoopPaint controls the GUI. You don't have to worry about it.

Your mission will be to program a few implementations of the SpecLoopPaint interface. Each of these will use an instance of the Switcher class.

Implementing the SpecLoopPaint Interface

Scroll through until you find the code for an internal class called MyClass1.

public static class MyClass1 implements SpecLoopPaint
  public int  size() { return 100 ; }
  public void TestFunc(Switcher P)
    P.TurnOn(15) ;
    P.TurnOn(23) ;
    P.TurnOn(87) ;
    P.TurnOn(93) ;

You'll notice that the head of MyClass1 says that it implements the SpecLoopPaint interface. When a class implements an interface, it provides the methods that are specified in the interface. Notice that MyClass1 contains both a size and a TestFunc method. These are the two methods specified in the SpecLoopPaint interface shown below. If you need a bit more information about interfaces, you might want to pull out your textbook.

package edu.unca.cs.csci201.LabAids;

public interface SpecLoopPaint {
  public int  size() ;
  public void TestFunc(Switcher P) ;

The first method, size is easy. It returns the size of the grid. The grid consists of tiles that can be painted by your Java code. These patches are numbered from 0 to n-1, where n is the integer returned by the size method.

The second method, TestFunc is used to paint the cells. TestFunc has single parameter of type Switcher. Whenever we need to display the grid, a Switcher object is created and passed to your TestFunc method. By calling the TurnOn() method of the Switcher object, cells of the grid may be painted red. If a patched isn't TurnOn'ed, it remains white.

The patches are numbered from 0 starting in the upper-left corner. Patch numbers increase sequentially from left-to-right. When one row is completed, we jump to the next.

Your Assignment

You are to modify the program you were given to produce the six displays described in the table below. To produce the first five displays modify the internal classes MyClass1 to MyClass5. We're going to let you figure out how to add the sixth class MyClass6 to LoopPaint.

  1. In a grid with 100 cells, turn on every third integer from 0 to 99.
  2. In a grid with 150 cells, turn on all squares (0, 1, 4, 9, ...).
  3. In a grid with 144 cells, turn on all powers of two (1, 2, 4, 8, ...)
  4. In a grid with 100 cells, turn on the first thirty numbers that are divisible by either three or seven.
  5. In a grid with 10000 cells, turn on every number from 0 to 9999, that can be expressed as (i2 + 5 i) for an integer i.
  6. In a grid with 20736 cells, turn on every number from 0 to 20735 which divides into 8393022 without leaving a remainder. By the way, if you resize the window, you might get a more interesting display.

Show your lab instructor your loopy results.