CSCI 201 -- Using the Math class

This lab will give you some practice with 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 starter program

Download Plot2D.java and save it in your csci/201 directory. Start up DrJava and load up your Plot2D.java file.

Modifying the project

When you run the program it will display nine pictures that plot nine different and boring functions. You need to modify the program to plot more interesting functions.

What to ignore

The Java program you've loaded 157 lines long. However, you don't do anything to the first 110 lines of it. Go down to around line 110, where you will see a comment line like the following:

  // ***   START YOUR WORK HERE   ***

Don't change anything before this comment line. After this comment there are nine methods named plot1 to plot9. You'll be changing each of these methods.

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 plotN methods 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.

  1. x3 + x2 - 0.4 x - 0.7
  2. absolute value of x, that is,
    -x, if x is negative,
    and x, if x is positive
  3. x * sine(1/x), if x is not zero,
    and 0, if x is zero
  4. sqrt(1 - x2)
  5. 0.3 sine(50 x)
    + 0.3 sine((4/3)*50 x)
    + 0.3 sine((3/2)*50 x)
    [a natural major]
  6. staircase with 0.1 steps
    [see the plot]
  7. 2x-1
  8. almost x/2
    [Use Math.random()]
  9. 0.4, when x ≤ -0.7
    0.35, when -0.7 < x ≤ 0.1
    -0.1, when 0.1 < x ≤ 0.3
    -0.25, when 0.3 < x

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