CSCI 201 Introduction to Algorithm Design home | homework index | labs index SPRING 2007

# 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. However, 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 several forms of iteration, or repetition. 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")) {
GeneratePayCheck(ER) ;
}
```

### A boring example from 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 succinctly 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:

 Code int i = 1; while (i < 32) { i = i + i; } Stack Heap Temporary

Download LoopPaint.zip, 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.

## Modifying the project

### The framework for the project

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 Main.java of the `looppaint` project 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

Start by looking in the file MyClass1.java at the class called `MyClass1`.

```package looppaint;

public class MyClass1 implements edu.unca.cs.csci201.labaids.SpecLoopPaint {

public int getSize() { return 12 ; }

public void testFunc(edu.unca.cs.csci201.labaids.Switcher P) {
P.turnOn(1) ;
}
}
```

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; however, that really isn't at all necessary for this lab.

```package edu.unca.cs.csci201.labaids;

public interface SpecLoopPaint {
public int  getSize() ;
public void testFunc(Switcher P) ;
}
```

The first method, `getSize` 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 a 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 patch 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.