#11 Java Math Class
The Math Class
In order to perform certain mathematical operations like square root (sqrt), or power (pow); Java has a built in class containing a number of methods as well as static constants, e.g. Pi = 3.141592653589793 and E = 2.718281828459045. All the methods involving angles use radians and return a double (excluding the Math.round()).
class Example15
{
public static void main(String args[])
{
double x, y, z;
x = 3;
y = 4; z = Math.sqrt(x*x + y*y);
System.out.println("Hypotenuse is " +z);
}
}
predicted output:
Please note that whenever a method is called, a particular nomenclature is used where we first specify the class that the particular method belongs to, e.g. Math.round( ); where Math is the class name and round is the method name. If a particular method accepts parameters, these are placed in brackets, e.g. Math.max(2.8, 12.9) – in this case it would return 12.9 as being the larger number.A useful method is the Math.random( ) which would return a random number ranging between 0.0 and 1.0.
Comments
Post a Comment