Repetition is something that computers excel at. In this lab, you will learn how to use Java's repetition structure - a "loop"
To Get Started ...

Download the jar file: LoopPaint.jar to your csci/201 directory, unjar the file (Help), create a new Netbeans project called LoopPaint, and mount the LoopPaint directory created when you unjarred the archive file (Help).

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
		1 + 1 = 2 ...

As you can see, this isn't very useful. So, computers have a form of repetition called loops which allow them to easily do the same action on different things:

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

In Java, the above computation can be accomplished with one line of code. Amazing? Yes it is:

		for (int i = 1; i < 32; i = i + i) { }

Meet the for loop. An advanced version of the while loop. To do the same thing with a while loop, you would write this:

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

These commands are very simple, yet powerful. There are three steps required to set up a loop. First, you initialize the loop test variable, in this case, i. Next, you provide the loop test, in other words, how will the computer know when to stop looping? These loops run while the variable i is less than 32. Finally, the update statement. This statement changes the value of the loop test variable; it is executed each time the body of the loop is executed. In this case, the loop test variable is incremented by doubling its value, i.e., (i + i). The structure of the for loop is such that the code for all three of these steps is in the first line of the loop. This whole sequence of events is illustrated by the following Stack N' Heap:

Stack N' Heap What's This? The Stack N' Heap simulates a trace program. It shows the code being executed, the contents of the stack, and the contents of the heap. To move through the code, use the >> button. This will take you one step through the code. To go back one step, use the << button.

Primative variables are shown in the stack with their names on the left side and values on the right. Reference variables do not have any value on the right, but instead have a yellow line drawn to show which object in the heap it points to. Reference variables with a NULL value don't show anything on the right.
int i = 1;
while (i < 32)
i = i + i;

The LoopPaint project

Now compile and run the Java program LoopPaint.java and use the radio buttons to select and view each of the three displays created by the program.

Notice that the program LoopPaint.java begins with statements to import classes Painter and FrameLoopPaint and the interface (?)Interfaces are explained in more detail in the Interfaces lab. called SpecLoopPaint from the edu.unca.cs.csci201.LabAids package. Your mission will be to program a few implementations of the SpecLoopPaint interface. Each of these will use an instance of the Painter class.

An example of implementing the SpecLoopPaint Interface

Scroll through LoopPaint.java 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(Painter 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(Painter P) ;

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

The second method, TestFunc() is used to paint the patches. TestFunc() has single parameter, which we'll call P, of type Painter. Whenever we need to display the grid, an approprite P will be created and passed to your TestFunc() method. By calling the TurnOn() method of P, patches of the grid may be painted red. If a patched isn't TurnOn'ed, it remains white.

The TurnOn() method has a single parameter, the index of the patch to be painted. 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.

1In a grid with 100 cells, turn on every third integer from 0 to 99.
2In 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.
6In 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.
Instructor Checkoff ...

Be sure to show your lab instructor your loopy results.