You're going to write a few loops in this lab.
Download the Lab 7 jar file into the directory csci/201, and extract the archived files to create the directory csci/201/Lab07. In case you have forgotten, the command is:
[csciuser@mach dir] cd ~/csci/201 [csciuser@mach dir] jar xfv Lab07.jar
By now, some of you may be wondering about that "xfv" you use with the jar command. It is an option that comes from the ancient Unix tar, tape archive, program. The xfv is for extract from a file (not a tape) verbosely.
Open the jGRASP project file LoopPaint.gpj located inside
the Lab07 directory that you just extracted. Then open and compile
the Java program LoopPaint.java.
Run the LoopPaint
class and use the radio buttons to
select and view each of the three displays created by the program.
Notice that the program LookPaint.java begins
with startments to import classes Painter
and FrameLoopPaint
and interface SpecLoopPaint
from the edu.unca.cs.csci201.LabAids
package.
Your mission will be to program a few implementation of
the SpecLoopPaint
interface. Each of these
will use an instance of the Painter
class.
SpecLoopPaint
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 MyClass
implements the SpecLoopPaint
interface shown below.
By the way, if you need a bit more information about interaces,
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.
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. |
Be sure to show your lab instructor your loopy results.