This lab will introduce you to the basics of the Math class and relational operators. The Math class provides a number of commonly used functions for your convenience. Relational operators allow you to compare numbers. You can use relational operators inside of conditional statements to have your program made decisions while it is running.
Download the jar file: Plot2D.jar to your csci/201 directory, unjar the file (Help), create a new Netbeans project called Plot2D, and mount the Plot2D directory created when you unjarred the archive file (Help).
As we said before, the Math class provides many useful mathematical functions. To use a method in the Math class, you type Math. or its full name, java.lang.Math. followed by the name of the method you want to use. If you can't remember the name of the method, you can still type that in, and then wait a moment and Netbeans will bring up a list of all of the methods in the Math class.
Look at the code in 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 (?)Interfaces are explained in more detail in the Interfaces lab. 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, using the keyword "return," 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 MyClassX to produce the X'th plot specified below. Do not modify the code for any other classes!
You will need to use the sine function in the Math class to write two of the functions above (?). The Math class is explained in more detail in the Numbers and Expressions lab. A list of the methods in the Math class 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)) in order to compile.
Once you've got the five plots, show your work to the lab instructor.
One of the simplest form of conditional statement is the IF statement. This statement directly translates into: IF a test is true, THEN do this, ELSE do this - decisions that you make each and every day of your life. Some examples:
The last statement above is very close to what computers do, here is that statement translated into Java:
if ( X > 5 ) { (IF X is greater than 5) System.out.println( X ); (THEN Output X) } else { (ELSE) X = X + 1; (Add 1 to X) }
Notice, first comes the word if, then a "test" in parenthesis, then the "true" block of code enclosed in curley brackets, then the "false" (or "else") block of code enclosed in curley brackets. The "false" (or else) portion of the statement can be omitted, this translates into doing nothing on a false case. The following Stack N' Heap illustrates some IF statements:
Code
int test = 15;
if (test > 30) { System.out.println("one"); } if (test == 15) { System.out.println("two"); } else { System.out.println("Not two"); } |
Stack
|
Heap
|
Temporary
|
What makes an IF statement, or any conditional statement, so powerful is that almost any code can go in the "true" and "false" blocks. But to use an IF statement, you must know how to correctly specify a "test." The "test," which is always placed in parenthesis, must be an expression that evaluates to either true or false; in other words, it must be an expression that contains a relational operator. Relational operators are binary operators, they require two operands to compare. You may have noticed the use of the relational operators greater than (>) and equivalent to ( == ) in the above Stack N' Heap.
Both relational and logical operators are covered in more detail in your textbook as well as on the Sun website: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/relational.html
In a nutshell, relational operators are used to produce boolean expressions (i.e., expressions that evaluate to true or false), and logical operators are used to combine boolean expressions according to the rules of logic.
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 of the display area 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 usual 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.