2.9 Using the Math Class
2.9 Using the Math Class
rectangle.getArea();
Instance method:
Static method:
}
Creating static methods
Object
Double Integer
Static Methods Don’t Need an Instance!
int x = Math.abs(-5);
int n = m.abs(-5);
System.out.println(n);
Static Methods Don’t Need an Instance!
int n = m.abs(-5);
System.out.println(n);
Math class Methods
Method Use
Method Use
double y = Math.abs(-10.3);
Method Use
Method Use
System.out.println(squareRoot); 15.9843673631
Math class Methods
Method Use
0.2396070187920506 2.396070187920506 2
0.0076898059369292 0.076898059369292 0
0.1138854030030303 1.138854030030303 1
0.7881929515623757 7.881929515623757 7
0.9995677392929999 9.995677392929999 9
Creating Random Range
We have additional
documentation for the
Math class in the
CodeHS code editor!
Now It’s Your Turn!
Concepts Learned this Lesson
Term Definition
Static Methods Static methods are the methods in Java that can be called
without creating an object of class. Static methods are called
using the dot operator along with the class name unless they
are defined in the enclosing class.
Math Class The Math class is part of the java.lang package and contains
only static methods.
double pow(double base, double exponent) Returns the value of the first parameter raised to the power of
the second parameter
double random() Returns a double value greater than or equal to 0.0 and less
than 1.0
● (EK) MOD-1.H.1 Static methods are called using the dot operator along with the class name unless
they are defined in the enclosing class.
● (LO) CON-1.D Evaluate expressions that use the Math class methods.
● (EK) CON-1.D.3 The following static Math methods—including what they do and when they are
used—are part of the Java Quick Reference:
○ int abs(int x) — Returns the absolute value of an int value
○ double abs(double x) — Returns the absolute value of a double value
○ double pow(double base, double exponent) — Returns the value of the first parameter
raised to the power of the second parameter
○ double sqrt(double x) — Returns the positive square root of a double value
○ double random() — Returns a double value greater than or equal to 0.0 and less than 1.0
● (EK) CON-1.D.4 The values returned from Math.random can be manipulated to produce a random
int or double in a defined range.