In this lab you use conditional operators to generate some pretty pictures.
We're going to give you two projects to work on in this lab. Both are stored as jar-files. Retrieve them from the list below and store them in csci/201.
Extract the files of Plot2D.jar to create the directory csci/201/Lab05/Plot2D. Then open the Plot2D.gpj jGRASP project file. If you can't get unjarred, consult Downloading a Java archive from Lab 2.
Go to the Project panel and double-click on
Plot2D.java to bring up a CSD window containing
the Java program for the Plot2D
class.
Compile and then run the program. You'll get a window with a
row of radio buttons for selecting a function to plot.
Look at the code of Plot2D.java.
Notice that this file begins by importing two classes
SpecPlot2D
and FramePlot2D
from the edu.unca.cs.csci201.LabAids
package.
The FramePlot2D
class is the graphics driver
for your application. It loads and plots functions within classes
that satisfy
the SpecPlot2D
interface shown below:
public interface SpecPlot2D { public float TestFunc(float f) ; }
If you scroll a bit down the code of Plot2D.java,
you will see a class MyClass1
that
implements this interface.
private static class MyClass1 implements SpecPlot2D { public float TestFunc(float f) { return f; } }
The single method TestFunc
of class MyClass1
is the function to be plotted.
When we say that
MyClass1
implements SpecPlot2D
,
we mean that MyClass1
has methods corresponding
to all of those listed in the interface SpecPlot2D
.
TestFunc
, the only method of MyClass1
,
is a very simple function. It receives a floating point number
f
and returns that same number as
its output. Your mathematically inclined friends (should
that not be an empty set) would call this an identity function.
In this lab, the values returned by TestFunc
are used by the FramePlot2D
class to produce graphs.
Graphing functions can be "added" to a plotter by calling the
method addPlotter
. You can see five calls to
addPlotter
in the main
method of
Plot2D
. Each adds a new function for graphing.
Notice that, toward the end of the code for the Plot2D
,
there are several internal classes, MyClass1
to MyClass5
. Each of these implements a
SpecPlot2D
interface.
However, the functions they plot are dreadfully boring.
You are to modify these five classes to produce the five plots
shown in the table below.
Modify class MyFuncX
to produce the X'th
plot.
Do not modify the code for any other classes!
You will need to
use the sine function in the Math class to write that
expression. Fortunately, the Math class is part of the
java.lang package and does not need to be imported.
A list of the methods of java.Math
is available in the
Java
documentation for the Math
class.
Note that most of the methods in the Math
class return a double
value. Since TestFunc
must return a float
, you will have to do some
data conversions, such as
(float) Math.cos(f)
to quieten the Java compiler.
Once you've got the five plots, show your work to the lab instructor.
Close the Plot2D project and extract the
files of Flag.jar (hopefully stored in
csci/201)
to form the directory csci/201/Lab05/Flag.
Open the Flag.gpj jGRASP project file.
Then go to the Project panel and double-click
Flag.java.
Compile and then run the Flag
program.
You'll get a window with a row of radio buttons
for selecting a flag.
The file Flag.java starts by importing the
classes SpecFlag
and FrameFlag
from the edu.unca.cs.csci201.LabAids
package.
As with the Plot2D project you just compiled,
here you will modify classes that satisfy an interface
specification.
In this case the interface is:
package edu.unca.cs.csci201.LabAids; import java.awt.Color ; public interface SpecFlag { public int height() ; public int width() ; public Color TestFunc(int x, int y) ; }
Notice that this time you have to complete three methods.
TestFunc
is a method that takes 2 integers as input and
returns a color.
The x
and y
values
that TestFunc
receives are coordinates in the display area
The color returned by TestFunc
is used to paint a rectangle
located at those coordinates.
The number of rectangle within the flag "screen" is determined
by the values returned by the height
and
width
methods.
Scroll through the file Flag.java to the
code for the class Haiti
.
private static class Haiti implements SpecFlag { public int height() { return 20 ; } public int width() { return 30 ; } public Color TestFunc(int x, int y) { if (y<10) return Color.blue ; else return Color.red ; } }
The height
and width
methods tell us that the
flag display area is be partitioned into a grid
20 by 30 rectangles.
This means the that TestFunc
method will be called
with y
values ranging from 0 to 19 an
with x
values ranging from 0 to 29.
For each of these input combinations, TestFunc
returns a Java Color
to be used to paint
a small rectangle of the screen. Our sample TestFunc
paints the top half blue and the bottom half red.
You are to modify the classes checkers
,
Ireland
, North Carolina
,
and USA
to display some nifty flags.
In order to do this, you will need to write
conditional statements that use the relational
and logical operators described in your textbook.
The checkers
class should return the unusual 8 by 8
checkerboard of alternating black and red places. You'll find it
useful to use the remainder, %
, operator here.
The flag of Ireland is three equal vertical strips of green, white, and orange. You can consult the on-line World Flag Database for a bit more information.
For the flag of the state of North Carolina, you do not need to show the letters, star, or banners within the field of blue. For the flag of the United States, you do not need to show the stars, but you better show the thirteen alternating red and white stripes.
If you need more information about Java colors, consult the
Java
documentation for the Color
class.
Good luck.
Show your lab instructor the flags.