# Yorkville High School Computer Science

ASSIGNMENTS: Hidden Figure - May 24, 2019

## AP Computer Science :: Lessons :: Math Class

Chapter 4:
Pages 234 - 236

Fundamentals of Java
Chapter 4
Pages 107 - 109

### Math Methods

The Math class in Java contains a number of useful methods as well as constants such as pi. Many of these methods are static methods. Remember that static methods are not invoked through an instance of a class. You invoke a static method using the dot operator on the class itself. For example, invoking the absolute value method (abs) would be done by typing

`Math.abs(64)`

.

There are four versions of the absolute value method, but we will only mention the versions for integers and doubles since the other two are not covered by the AP Java Subset.

Method Definition from Java Docs
`public static double abs(double a)`
Parameters:
`a`
- the argument whose absolute value is to be determined.
Returns:
the absolute value of the argument.
Method Definition from Java Docs
`public static int abs(int a)`
Parameters:
`a`
- the argument whose absolute value is to be determined.
Returns:
the absolute value of the argument.

Example:
`d = Math.abs(point1 - point2);`

The above example subtracts point2 from point1 and finds the absolute value of the result. This is the formula used to find the distance between two points.

Method Definition from Java Docs
`public static double pow(double a, double b)`
Parameters:
`a`
- the base.
`b`
- the exponent.
Returns:
the value ab.

The power method takes a given base to a specified exponent.

The formula below takes 1.05 to the 10th power and multiplies it by the variable p. This determines the amount of money earned in an account after 10 years at 5% interest compounded annually.

Example:
`a = p * Math.pow(1.05, 10);`
Method Definition from Java Docs
`public static double sqrt(double a)`
Parameters:
`a`
- a value.
Returns:
the positive square root of
`a`
. If the argument is NaN or
less than zero, the result is NaN.

Finally, the square root method calculates the square root of a given double.

The formula below calculates the radius of a circle given its area. Notice that the pi constant is also used in the formula.

Example:
`radius = Math.sqrt(area/Math.PI);`

### Random Numbers

Method Definition from Java Docs
`public static double random()`
Returns:
a psuedorandom
`double`
greater than or equal to
`0.0`
and less than
`1.0`
.

The random method calculates a random number between 0 and 1. It does not include 1 in the number it generates.

The following examples calculate random numbers in broader ranges by transforming the formula.

Examples:
```double x = 6 * Math.random();     //0.0<=x<6.0
double x = Math.random() + 2;     //2.0<=x<3.0
double x = 2 * Math.random() + 4; //4.0<=x<6.0```

You may notice the pattern that forms to determine the high and low range of the random number. The formula for determining a random double is below.

`double x = (high - low) * Math.random() + low //low<=x<high`

To calculate a random integer you would do the following:

`int n = (int) (Math.random() * (high-low+1)) + low`