We've worked with a basic set of arithmetic operators (i.e+, -,*, /, %). However, when it comes to more complex calculations, we need a little more help.
The Math class is a built-in library of common math methods (functions) and constants that we can access to compute beyond the basic set of operators. The documentation and full list of methods can be found here. Below is a list of common methods that we access:
| Operation | Command | Description |
|---|---|---|
| Power | Math.pow(double base, double exponent) | Compute the powers operation given a base and exponent |
| Squareroot | Math.sqrt(double number) | computes the squareroot of a given number |
| Round | Math.round(double number) | round as a number to its' closest whole number |
| Convert Degrees to Radians | Math.toRadians(double degrees) | converts a given angle in degrees to radians |
| Convert Radians to Degrees | Math.toDegrees(double radians) | given an angle in radians, convert it to degrees. |
| Cosine | Math.cos(double radians) | given an angle in radians, compute the cos of that angle |
| Sine | Math.sin(double radians) | given an angle in radians, compute the sin of that angle |
| Tangent | Math.tan(double radians) | given an angle in radians, compute the tan of that angle |
| PI | Math.PI | pi constant |
Write a program Hypotenuse.java that lets you enter the two sides of a right angled triangle, and then prints the hypotenuse.
Solution:
/*
* Write a program Hypotenuse.java that lets you enter the two sides of a right angled triangle,
* and then prints the hypotenuse.
* @author: E. Fabroa
*
*/
public class Hypotenuse extends ConsoleProgram{
public void run() {
// Create variables for sideA, sideB, hypotenuse
double sideA;
double sideB;
double hypSqrd;
double hypotenuse;
// Get values for sideA, sideB
sideA = readDouble("Enter the length of side A: ");
sideB = readDouble("Enter the length of side B: ");
// Compute hypotenuse**2 = sideA**2 + sideB**2
hypSqrd = Math.pow(sideA, 2) + Math.pow(sideB, 2);
hypotenuse = Math.sqrt(hypSqrd);
// Output hypotenuse
System.out.println("The hypotenuse of the right triangle is " + hypotenuse);
}
}
When using trigonometric functions like Math.cos or Math.Sin, you must supply the angle in radians. You can use the Math.toRadians() method to convert from degrees to radians.
//a radians example
double angle_deg = 45.0;
double cos_angle = Math.cos(Math.toRadians(angle_deg));