CSCI 201 -- Using the Math class

This lab will introduce you to the basics of Java's Math class. The Math class provides a number of commonly used functions, including trigonometric and exponential function, for your convenience. In this lab we're going to use this function to plot some functions.

Downloading the project framework

Download Plot2D.zip, a ZIP file containing a NetBeans project named Plot2D and unZIP this project into your csci/201 directory. Try to make your Projects panel look something like the following picture before continuing.
Initial projects panel

Modifying the project

The framework for the project

Look at the code in the Plot2D.java file. Notice that this file begins by importing a class SpecPlot2D from the edu.unca.cs.csci201.LabAids package. This class specifies an interface your program must use to pass a mathematical function to the plotter. A bit further down the program you'll see a rather long Java statement that creates a Java frame to control the plotter.

    edu.unca.cs.csci201.LabAids.FramePlot2D testWindow = 
            new edu.unca.cs.csci201.LabAids.FramePlot2D(labName, 750, 500) ;

You really don't need to worry about this statement, though you are welcome and encouraged to look at the FramePlot2D implementation. For you the interesting parts of the Plot2D class follow the main method. There you see a number of similar internalclasses. Here's the first one, a simple linear function, which plots a diagonal straight line.

private static class MyClass1 implements SpecPlot2D {
    public double TestFunc(double f) {
        return f;
    }
}

Each of these internal classes has single method TestFunc, the function to be plotted. When we say that MyClass1 implements SpecPlot2D in Java, we mean that MyClass1 has methods corresponding to all of those listed in the interface SpecPlot2D. By the way, here is a copy of the Java code, striped of comments, that defines the Plot2D interface within the edu.unca.cs.csci201.LabAids package.

package edu.unca.cs.csci201.LabAids ;

public interface SpecPlot2D {
    public double TestFunc(double x) ;	
}

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 addPainter of FramePlot2D. You can see five calls to addPainter in the main method of Plot2D.

Running the project

Build and run the project. There is a panel of radio buttons at the top of the frame that select the function to the plotted. In some instances, the plots are incomplete: They end in a vertical red line. This occurs when there is some problem with the function being plotted. For example, it might divide by 0 and product the result Double.POSITIVE_INFINITY or Double.NaN (not-a-number). In these cases, the plotting stops.

More on Math

As we said before, the Math class provides many useful mathematical functions. To use a method in the Math class, you type 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 the Math. and wait a few moments for NetBeans to bring up a list of all of the methods in the Math class. However, you'll be much more protective in this lab if you first take a moment to study the Java documentation for the Math class.

Your assignment

Your assignment is to modify the five internal classes within the Plot2D class to implement the five plots. Modify class MyClassX to produce the X'th plot specified below. The Applet on the right should give you some idea of how your finished program should work. Do not modify the code for any classes other than MyClassX!

  1. f3 + f2 - 0.4 f - 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,
    and 0, if f is zero
  5. sqrt(1 - f2)

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