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.

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.

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.

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.

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!

f3 + f2
- 0.4 f - 0.7

0.7 sine(6 f) + 0.2 sine(20 f)

absolute value of f, that is,
-f, if f is negative,
and f, if f is positive

f * sine(1/f), if f is not zero,
and 0, if f is zero

sqrt(1 - f2)

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