Math Class & Common Algorithms
Objective #1: Use the Math class for routine mathematical
tasks.
- The Math class is automatically imported with Java programs. It contains many useful methods and constants.
- You can use a number of Math class methods (i.e. static methods) such as the following methods that you should know for the AP exam or class tests:
- Math.sqrt(x) - which obtains the square root of a value. Note that this method returns a double and not an int.
int num = Math.sqrt(10); and int num = Math.sqrt(9); cause errors since sqrt returns a double which cannot be assigned to an int even if the double is 3.0 for example.
- Math.pow(x, y) - which is used to compute x to the yth power
System.out.println(Math.pow(2, 8)); displays 256 which is 2 raised to the 8th power.
- Math.abs(x) - to
compute the absolute value of x
- Math.max(x, y) -
returns x or y, whichever is greater
System.out.println(Math.max(-2, 3)); displays 3 since 3 is greater than -2
System.out.println(Math.max(Math.max(1, 4), 2)); displays 4
- Math.min(x, y) -
returns x or y, whichever is least
- Math.round(x) - to
round x to the closest integer
System.out.println(Math.round(4.5)); displays 5
System.out.println(Math.round(4.4)); displays 4
- Math.ceil(x) - to
find the smallest integer that is greater than or equal to x. Note that this method returns a double so the statement int num = Math.ceil(2.3); causes a compile error.
System.out.println(Math.ceil(3.1)); displays 4
System.out.println(Math.ceil(-2.9)); displays -2
- Math.floor(x) - to
find the largest integer that is less than or equal to x. Note that this method returns a double so the statement int num = Math.floor(2.3); causes a compile error.
System.out.println(Math.floor(3.9)); displays 3
System.out.println(Math.floor(-2.1)); displays -3
- Math.random() -
returns a pseudorandom floating-point value greater than or equal to
zero but less than 1; that is a value in the range [0, 1)
You are not responsible for memorizing or using the methods below on the AP exam or class tests but they are convenient.
- Math.sin(x) - to compute the sine of x in radians
- Math.cos(x) - to compute the cosine of x in radians
- Math.tan(x) - to compute the tangent of x in radians
- Math.toRadians(x) - to convert x radians to degrees (i.e. returns x * 180/Math.PI)
- Math.toDegrees(x) - to convert x degrees to radians (i.e. returnx x * Math.PI/180)
- Math.exp(x) - to compute e to the xth power
- Math.log(x) - to
compute the natural log of x (i.e. ln(x) where x > 0)
- The methods above are called static methods. You
do not need to instantiate a Math object
in order to use the methods. You simply type the name of the class Math followed
by a dot operator and then the name of the method.
- You can use a reasonably good approximation of pi by referencing the constant Math.PI in a Java program. You can use Euler's constant, e, by referencing the constant Math.E.
For example, the following code could be used to display the area of a circle that has a radius of 5 units
int radius = 5;
System.out.println(Math.PI * Math.pow(radius, 2));
Objective #2: Use handy mathematical algorithms with casting and Math class methods.
- To truncate (or "chop off") the decimal part of a decimal number you can cast the double to an int:
double num = 123.456;
System.out.println((int) num); // 123
- To extract the digits in the decimal part of a decimal number:
double num = 123.456;
System.out.println(num - (int) num); // 0.456
- To find the maximum integer out of 3 numbers without using an if statement:
int x = 3, y = 4, z = 2;
System.out.println(Math.max(Math.max(x, y), z)); // 4
- To convert a number to a String by concatenating an empty string to an int
int numberZip = 19610;
String stringZipCode = numberZip + ""; // "19610"
- To convert a String to an int
String stringZipCode = "19610";
int numberZipCode = Integer.parseInt(stringZipCode); // 19610
- To convert a String to a double
String stringPrice = "19.99";
double numberPrice = Double.parseDouble(stringPrice); // 19.99
- To round a decimal number to the nearest whole number by casting:
double unrounded = 12.56789;
System.out.println( (int) (unrounded + 0.5)); // 13
or, using an available Math class method
double unrounded = 12.56789;
System.out.println( Math.round(unrounded)); // 13
- To round a decimal number to the nearest tenth's place by casting:
double unrounded = 12.56789;
System.out.println( (int) (unrounded * 10 + 0.5) / 10.0); // 12.6
- To round a decimal number to the nearest hundredth's place by casting:
double unrounded = 12.56789;
System.out.println( (int) (unrounded * 100 + 0.5) / 100.0); // 12.57