# CMPU 101 - Lab 2

\$Revision: 1.4 \$

In this lab you will write methods that compute the value of some mathematical functions.

## Getting Started

Open a web browser.  Go to the class web page http://www.cs.vassar.edu/~cs101/hovemeyer/.  Scroll to the bottom of the page.  Download the files PlottingCalculator.java and Functions.java into the Home directory (right click on the links and choose "Save Link As...").  Start DrJava and load both of these files.  In the DrJava window, click on "Functions.java" in the list of filenames on the left-hand side of the window.  Your screen should look something like the following:

The Functions class defines a number of static methods that take a single double value as a parameter and return another double value as a result.  Your task is to modify each of these static methods so that they compute a particular mathematical function, as specified below:

The sin static method should return the sine of the value passed as a parameter.  You can compute the sine of a value by calling the standard Java method Math.sin, passing a double value.  For example,

```Math.sin(x)
```

would compute the sine of the value of the variable x.

The fancySin static method computes a slightly more complicated function involving sines.  Specifically, for the input value x, it should return the value

4(sin x) + 2(sin 4x)

The step static method should discard the fractional part of the input value and return the integer part.  For example, if passed the value 4.3, it should return 4.0.  There are two ways to discard the fractional part of a floating point value in Java.  One is to cast the value to an integer type such as int.  The other is to call the standard Math.floor method, which takes a single double value and returns the whole integer part of that value as a double.

The normal static method computes the standard normal distribution.  This function is computed according to the formula

(1 / sqrt(2 * pi)) * (e-(x2) / 2)

The named constants Math.PI and Math.E define the values pi and e, respectively.  The square root function (sqrt) may be computed using the standard Java method Math.sqrt, which takes a double value and returns the square root of that value.  Java does not have an exponentiation operator: however, it does have an exponentiation method, Math.pow, which takes two double values.  The first value is a number, and the second value is the power you want the first value raised to.  For example, if x is a variable and you want to compute the value x2, you could call Math.pow as follows:

```Math.pow(x, 2.0)
```

The poly static method should return the result of evaluating the following polynomial:

2x3 + 5x2 + 8x + 1

The onlyEven static method should discard the fractional part of the input value (just like the step method), and then decide whether the value is even or odd.  If the resulting value is even, then the method should return the value 1.0: otherwise, it should return 0.0.

## Viewing the Functions

The PlottingCalculator class is a program that allows you to visualize the functions you have defined.  To run this program, save "Functions.java", click "Compile All", then run the command

```java PlottingCalculator
```

in the interactions window.

 If you press the up arrow key in the interactions window, it will bring up the last command you executed.  Then you can hit return to execute this command.  This will save you some typing.

To use the PlottingCalculator program, type the name of the static method you want to visualize in the input box labeled "Function to plot".  Then enter the minimum and maximum of the range of x value you want to plot and click the "Plot!" button.  Here is an example plot of the fancySin method over the range -10 to 10:

Here is the onlyEven function over the range 0 to 20:

Experiment with plotting the other functions as you complete them.