If a directory called c:\files\lab05 presently exists on your computer, delete it.
We're going to give you two projects to work on in this lab. The
projects are stored in a zip archive so you will have to use
PowerZip to extract them. First, download a zipped
copy of the lab05 project by right-clicking on this link, and storing the target file in the
C:\files directory.
Now, you should be able to double-click on the lab05.zip icon
in the C:\files directory to run the PowerZip program. On PowerZip's
menu, hit the extract button, set the download directory to C:\files
and then select ok to create:
C:\files\lab05\Plot2D
C:\files\lab05\Flags
Check to make sure that these folders exist before continuing.
Start up JCreator by first opening the lab05 folder followed by the Plot2D folder and then double-clicking on the ".jcw" file (i.e., Plot2D.jcw). Once the project is open in JCreator, you should be able to execute the program by selecting the Execute Project option under the Build Menu of JCreator's toolbar. Run the program and select the MyClass1 radio button in the display of the appleviewer to see the first 2D plot. Press the X in the upper right corner of the display window to terminate the program.
Let's look at the code for the class MyClass1 because you will have to modify this code as well as use it as a template for creating other similar classes.
import edu.unca.cs.csci201.Plot2D.SpecPlot2D ;
public class MyClass1 implements SpecPlot2D {
public float TestFunc(float f) {
return f ;
}
}
The first line simply imports SpecPlot2D from a package
with a very long name, "edu.unca.cs.csci201.Plot2D". Next is the
first line of the class MyClass1. Notice that it says
MyClass1 "implements" SpecPlot2D. We will learn
about this in Chapter 5, but can look at it a little bit right now.
SpecPlot2D is an interface which consists of a
collection of methods that have not been implemented (i.e., the code
for the methods have not been written)---look at the file
SpecPlot2D.java in your project. When we say that
MyClass1 "implements" SpecPlot2D, we are telling
the compiler that MyClass1 will contain an implementation
(i.e., the code) for the methods listed in SpecPlot2D.
TestFunc(), the only method implemented in this class, is a very simple method. It requires one input, a float, to run, and when it runs, it returns the value it received as input. When a method "returns" a value such as a float number, it "evaluates" to that number when it is called. For example, we say that the expression, 3 + 4 evaluates to 7 which means that the statement:
System.out.print(3+4);
would cause the number 7 to be output to the monitor. In
the same sense, when a method such as TestFunc() returns a value, the
statement (where obj is an object of type
MyClass1)
System.out.print(obj.TestFunc(7));
would output the number returned by the method, which would also be
7. This is because TestFunc() (as it is currently written)
returns the same number it receives as input. How do we know that
TestFunc() returns a float number and that it is the same number that
TestFunc() receives as input? Check the hyper-links in the segment of
code below for an answer.
public float TestFunc(float f) {
return f ;
}
There is one more thing that we need to understand about return statements in general. Regardless of where the return statement occurs in a method, as soon as the return statement is executed, the method terminates---any code that follows the return statement is not executed.
In this program, the value returned by TestFunc() is not printed to the monitor as in the example above, but rather is used by the class PaintPlot2D to produce the graph displayed when you run the program. More specifically, PaintPlot2D produces a 2D-plot by inputting an "x" value to TestFunc(), and using the value returned by TestFunc() as the corresponding "y" value.
You are to modify the program you were given to produce 4 plots, where
the "y" values used to produce these plots are specified below. To
produce the first plot, modify MyClass1 so that TestFunc()
returns the value of expression 1 below. Do not modify the code
for any other classes, i.e., do not modify PaintPlot2D,
SpecPlot2D, or TestPlot2D. 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 methods available in java.Math is on page 84 in
your textbook.) Note that most of the methods in the Math
class return a double value. However, TestFunc() returns a
float. This means that you will need to do some Data
Conversions; see pages 70-73 of your text. Below is an example of
the kind of conversion that you will need to do:
In this example, the method TestFunc() returns the cosine of its
input after it has cast that value as a float.
public class MyClass1 implements SpecPlot2D {
public float TestFunc(float f) {
return (float) Math.cos(f) ;
}
}
Once you have modified MyClass1 and checked the resulting plot, it's time to produce the remaining three plots. For each plot, the "y" values will be produced by a separate class. Here is how to create a new class in your Plot2D project (and produce a plot using that class). We will call the first new class MyClass2:
Repeat the procedure stated above for each of the remaining 2 plots.
Once you've got the five plots, show your work to the lab instructor.
Now open the Flags project folder and double-click on the file Flags.jcw to open this project in JCreator. This workspace should contain six files
As with the Plot2D project above, you will need to modify classes MyClass1 and MyClass2 as well as create additional classes to produce the five pictures described below.
Start by compiling and executing the project. Look at the graphics created by MyClass1 and MyClass2. Now, take a look at the code for MyClass1.
...
public class MyClass1 implements SpecFlag {
public int height() {
return 10 ;
}
public int width() {
return 10 ;
}
public Color TestFunc(int x, int y) {
if (x < y)
return Color.green ;
else
return Color.yellow ;
}
}
Notice that there are three methods in this class.
TestFunc() is a method that takes 2 integers as input and
returns a color. The "x" and "y" values that TestFunc()
receives as input are coordinates in the display area, and the color
returned by TestFunc() is the color applied to these
coordinates.
The value returned by the method width() determines how
many "x" values the display area will be partitioned into, and,
similarily, the value returned by height() determines the
number of "y" coordinates. In effect, the class PaintFlag
partitions the display area into a grid where the number of rows is
specified by the return value of height() and the number of
columns is specified by the return value of width(). The
color of each element in the grid is specified by
TestFunc() when it is given the column number, "x", and the
row number, "y", as input.
You are to modify/create classes to produce the pictures described in the table below. In order to do this, you will need to write if-else statements that use relational operators (see page 113 of your text), and, occasionally, logical operators (see page 123 of your text).
MyClass1 | Returns Red , if x >
y; White , if x equals y; and
Blue , if x < y
|
MyClass2 | Returns values that cause the window to resemble the flag of Ireland (three vertical stripes: green, white, and red) or any other European country. |
MyClass3 | Returns values that cause the window to resemble the flag of the state of North Carolina, but without the seal. |
MyClass4 | Returns values that cause the window to resemble the flag of
the United States of America, but without the stars.
(Hint:You can create alternating red and white stripes by assigning one color to even numbers and the other color to odd numbers.) |
Once you've got the four pictures, show your work to the lab instructor.