Chapter 6-Math Class
Chapter 6-Math Class
1
MATH Class
Contains methods for performing
basic numeric operations such as
the elementary exponential,
logarithm, square root and
trigonometric functions.
2
Math Class Methods
Math Class Return Value
Methods
1. abs(num) returns absolute value of
a numerical value
2. acos(double angle) returns the arc cosine of
a value; the returned
angle is in the range 0.0
through pi
3. asin(double angle) returns the arc sine of a
value; the returned angle
is in the range –pi/2
through pi/2.
3
Math Class Return Value
Methods
4. atan(double angle) returns the arc tangent of
a value; the returned
angle is in the range 0.0
through pi
5. cbrt(double num) returns the cube root of a
double value
6. ceil(double angle) returns the smallest
double value that is
greater than or equal to
the argument and is
equal to a mathematical
integer
4
Math Class Return Value
Methods
7. cos(double angle) returns the trigonometric
cosine of an angle
8. cosh(double num) returns the hyperbolic
cosine of a double value
9. exp(double num) returns euler’s number e
raised to the power of a
double value
10. floor(double num) returns the largest
(closest to positive
infinity) double value that
is less than or equal to
the argument and is
equal to a mathematical
integer
5
Math Class Return Value
Methods
11. hypot(double a, return square root of a2 +
double b) b2 without intermediate
overflow or underflow
12. log(double num) returns the natural
logarithm (base e) of a
double value.
13. log10(double returns the base 10
num) logarithm of a double
value.
14. max(num1, num2) returns the greater of two
numerical values
15. min(num1, num2) returns the smaller of two
numerical values
6
Math Class Return Value
Methods
16. pow(double base, returns the value of the
double exponent) first argument raised to
the second argument
17. random() returns a double value
with a positive sign,
greater than or equal to
0.0 and less than 1.0
18. round(double returns the closest long to
num) the argument
19. round(float num) returns the closest int to
the argument
20. sin(double angle) returns the trigonometric
sine of an angle
7
Math Class Return Value
Methods
21. sinh(double num) returns the hyperbolic sine
of a double value
22. sqrt(double num) returns the rounded
positive square root of a
value
23. tan(double a) returns the trigonometric
tangent of an angle
24. tanh(double x) returns the hyperbolic
tangent of a value
25. toDegrees(double converts an angle
angrad) measured in radians to an
approx. equivalent angle
measured in degrees
8
Math Class Return Value
Methods
26. toRadians(double converts an angle
angdeg) measured in degrees to
an approx. equivalent
angle measured in radians
9
Line 1, import java.util.* to tell the compiler
to add the class Scanner to be able to use
the methods in Scanner class
Line 2, import java.text.DecimalFormat to
add the class DecimalFormat to be able to
use the methods in DecimalFormat class
10
Line 6, instantiate the Scanner class and
store the object in variable scan;
Line 7, instantiate DecimalFormat class
and store the object to twoDecPt
Line 8, declare n1, n2, high, low and pos
as integer
Line 9, declare sqroot, round as double
integer
11
Line 16, max method and store the return
value in high variable
Line 17, min method and store the return
value in low variable
Line 18, abs method and store the return
value in pos variable
Line 19, sqrt method and store the return
value in sqroot variable
12
Line 21, display the value of high
Line 22, display the value of low
Line 23, display the value of pos
Line 24, display the value of sqroot
13
Line 25, display the value of sqroot
formatted in two decimal places
Line 27, use ceil method and display the
value
Line 28, use floor method and display the
value
Line 29, use round method and display the
value
14