CSCI 201 Lab 7 -- Loops

Getting ready

If a directory called C:\files\LoopPaint presently exists on your computer, delete it.

Download a self-extracting archive of the Lab 7 project and store it in the C:\files directory. In Windows Explorer, double-click on the file and extract the project folder to the c:\files directory. This should create the new directory:
C:\files\LoopPaint.

The LoopPaint workspace

Open, compile, and run the workspace c:\files\LoopPaint\LoopPaint.jcw. Use the radio buttons to select and view each of the three displays created by the program.

Now, let's look at the code for the class MyClass1 (the class used in creating the first display) because you will have to modify this class as well as use it as a template for creating other similar classes.


package edu.unca.cs.csci201.LoopPaint;

public class MyClass1 implements SpecLoopPaint {
	public int height() {
		return 10 ;
	}
	public int width() {
		return 10 ;
	}
	public void TestFunc(Painter P) {
		P.TurnOn(15) ;
		P.TurnOn(23) ;
		P.TurnOn(87) ;
		P.TurnOn(93) ;
	}
}
The first line specifies that this file belongs to the package "edu.unca.cs.csci201.LoopPaint" which contains all of the code defined as part of this project. Next,

   public class MyClass1 implements SpecLoopPaint {
tells us that MyClass1 implements the interface SpecLoopPaint (See lab05, or page 236 of your text, for more about implementing an interface.) Looking further down, we see that MyClass1's method TestFunc() has one parameter named "P", and "P" is an object of type "Painter". Painter is a class that is defined in this project (in the file Painter.java).

Notice that, in addition to TestFunc(), there are two other methods defined in MyClass1: width() and height(), and each of these two methods returns an integer. The value returned by the method width() determines how many "x" values the display area will be partitioned into, and, similarly, the value returned by height() determines the number of "y" coordinates. In effect, the class PaintLoopPaint partitions the display area into a grid with height() rows and width() columns. The cells in this grid are numbered starting with "0" in the upper-left corner, and increasing across the rows until the max. number is assigned to the cell in the lower-right corner. Each cell in this grid, can be "turned-on" causing it to display its number and appear red, or it can be left "off" in which case it appears "invisible" (i.e., white). The display below is created by specifying a 10x10 grid in which all cells are "turned on".

The TestFunc() method is responsible for "turning on" cells. To turn on a cell, for example, cell number 15, call the Painter object P's method TurnOn() giving it the cell's number as input:


		P.TurnOn(15) ;

Your Task

You are to modify the program you were given to produce the five displays described in the table below. To produce the first display, modify MyClass1 to "turn on" the designated cells. Do not modify the code for any other classes, i.e., do not modify PaintLoopPaint, Painter, SpecLoopPaint, and TestLoopPaint.

Once you have modified MyClass1, MyClass2, and MyClass3 and checked the resulting displays, you will need to create new classes to produce the remaining two displays---one for each display. Here is how to create a new class in your LoopPaint project (and produce a display using that class). Call the first new class MyClass4:

Repeat the procedure stated above for the remaining display.


1 Create a grid with 100 cells and 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 128 cells, turn on all powers of two (1, 2, 4, 8, ...).
4 In a grid of size greater than 50, turn on the first fifteen numbers that are divisible by either three or seven.
5 In a grid of arbitrary size, turn on every number from 0 to the max cell number which divides into 8,393,022 without leaving a remainder.


Lab instructor check-off 1

Once you've got the five displays, show your work to the lab instructor.