CSCI 201 Lab 5 -- Conditional Operators

In this lab you use conditional operators to generate some pretty pictures.

Cleaning Windows

If you are in a Windows-based lab, check to see if there is a directory C:\Files\Lab05 on your PC. If one exists, delete it now.

Getting to the starting gate

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. Windows labers can store them in C:\Files. Linux labers can store them in csci/201.

Plot in 2-D

Start jGRASP and then extract the files of Plot2D.jar into the directory C:\Files\Lab05\Plot2D under Windows or the directory csci/201/Lab05/Plot2D under Linux. Then open the Plot2D.gpj jGRASP project file. If you can't get unjarred, consult Downloading a Java archive from Lab 2.

A warning to Windows users

There appears to be a bug in the jGRASP jar extractor that causes it to corrupt two Java class files while processing Plot2D.jar. This problem can result in a rather ugly ArrayIndexOutOfBoundsException error when you try to compile your applet. To avoid this problem, we suggest you start up a Command Prompt window and type the following commands, which use Sun's jar extractor rather the jGRASP one.

mkdir C:\Files\Lab05\Plot2D
cd C:\Files\Lab05\Plot2D
C:\j2sdk1.4.0_01\bin\jar xfv C:\Files\Plot2D.jar

We have reported this problem to the maintainer of jGRASP.

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.

The Plot2D class

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.

Your task

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 MyFuncI to produce the I'th plot. Do not modify the code for any other classes!

  1. x3 + x2 - 0.4 x - 0.7
  2. 0.7 sine(6 f) + 0.2 sine(20 f)
  3. absolute value of f, that is,
    -f, if f is negative,
    and f, if f is positive
  4. f * sine(1/f), if f is not zero,
    0, if f is zero
  5. sqrt(1 - f2)

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.

Lab instructor check-off 1

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

Conditional statement execution

Close the Plot2D project and extract the files of Flag.jar (hopefully stored in C:\Files or in csci/201) into the directory C:\Files\Lab05\Flag under Windows or the directory csci/201/Lab05/Flag under Linux.

Another warning to Windows users

Due to the jGRASP jar extractor bug, you need to execute the following commands in a Command Prompt window.

mkdir C:\Files\Lab05\Flag
cd C:\Files\Lab05\Flag
C:\j2sdk1.4.0_01\bin\jar xfv C:\Files\Flag.jar

A non-warning to Linux users

If you don't want to miss the fun of unjarring at the command prompt (which is really faster anyway), you can do the following.

mkdir -p ~/csci/201/Lab05/Flag
cd ~/csci/201/Lab05/Flag
jar xfv ~/csci/201/Flag.jar

However you managed to extract the Flag.jar files, go ahead a 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 Flag class

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.

Your task

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 use to use the modulus, %, 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.

Lab instructor check-off II

Show your lab instructor the flags.