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.
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.
Look at the code in the Main.java file
within the plot2d
package.
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 will 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) ;
Now relax.
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 lab will be those, now incomplete,
MyClassN
classes in the plot2d
package.
Here's the simplest, MyClass1
, which contains
a simple linear function to plot a diagonal
straight line.
class MyClass1 implements edu.unca.cs.csci201.labaids.SpecPlot2D { public double TestFunc(double x) { // ENTERING WORK ZONE return x; // LEAVING WORK ZONE } }
Each of these nine files contains a
single method TestFunc
,
the function to be plotted.
The values returned by TestFunc
will be plotted to produce graphs.
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.
Presently the plots are boring. You are going to spice them up a bit.
But before you start that task, go to MyClass2
and
change the x/2
to 2/x
.
When you run the program, a vertical red line will appear.
This happens because, when x
is zero, your divides by zero.
Avoid the red lines!
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 is to modify the
MyClassN.java files to
implement the nine plots listed on the left 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
!
|
Once you've have your nine plots, show your work to the lab instructor.