JAVA MATH CLASS
What is it called when a method gives a result after performing a task?
"Returning a value"
(random) to set boundaries other than 0<=pi<1, use _
(b-a)+a, when a and b are represented by a<=r<b
Formula to generate a random int within a specific range.
(int) ((maxValue - minValue) * Math.random() + minValue) OR (int) (numValues * Math.random() + minValue)
What does System.out.println (Math.ceil (-123.45)); display?
-123.0
What does System.out.println (Math.pow (10, -2)); display?
0.01
What does System.out.println (Math.abs (-1.23)); display?
1.23
What does System.out.println (Math.round (Math.PI)); display?
3
What is a method?
A chunk of code that performs a task
Math.random()
A method that returns a random floating point value between 0 (inclusive) and 1 (exclusive).
Math.abs()
A method that returns the absolute value of the parameter passed to it.
Math.pow()
A method that returns the power of a value when passed a base and exponent.
Math.sqrt()
A method that returns the square root value of the parameter passed to it.
Math.PI
A static field that represents the double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.
What type of method is a math method?
A static method. To use a math method, you need to put the name of the class in front of the method name with a dot or period. Ex. Math.pow()
Math.abs (value)
Absolute value
Math.pow (base, exp)
Base to the exp power
Math.toRadians (value)
Convert from degrees to radians
Math.toDegrees (value)
Convert from radians angle to degrees
Math.cos (value)
Cosine of an angle in radians
Math.abs(x);
Finds the absolute value
How do you store the result of the Math.pow() method?
In a variable. Ex. double power = Math.pow(7,2);
Math.max (value1, value 2)
Larger of two values
Math.log10 (value)
Logarithm, base 10
What is the constant for Pi?
Math.PI
creates pi
Math.PI;
absolute value of x
Math.abs(x);
arccosine of x, in range 0 to PI
Math.acos(x);
arcsine of x, in range -PI/2 to PI/2
Math.asin(x);
arctangent of x, in range -PI/2 to PI/2
Math.atan(x);
next highest whole number form of x
Math.ceil(x);
cosine of angle a in radians
Math.cos(a);
next lowest whole number form of x
Math.floor(x);
log base e of x
Math.log(x);
larger of a and b
Math.max(a,b);
smaller of a and b
Math.min(a,b);
b raised to the e power
Math.pow(b,e);
random
Math.random();
round to nearest whole number of x
Math.round(x);
sine of angle a in radians
Math.sin(a);
square root of x
Math.sqrt(x);
tangent of angle a in radians
Math.tan(a);
converts radians to degrees
Math.toDegrees(angle);
converts degrees to radians
Math.toRadians(angle);
Math.round (value)
Nearest while number
Math.random ( )
Random double between 0 and 1
Math.sqrt(x);
Returns the square root of an argument
Math.floor (value)
Rounds down
Math.ceil (value)
Rounds up
What do parameters do in a math method?
Send information in from the caller to the method
What do return values do in a math method?
Send information out from a method to its caller
Math.sin (value)
Sine of an angle in radians
Math.min (value1, value2)
Smaller of two values
Do methods require data in order to perform a task?
Some do
Math.sqrt (value)
Square root
16.0
System.out.print( Math.pow( 4, 2) );
How can you output the result of a math method without creating a variable?
System.out.println (Math.pow(7,2)); *Note: if you need to "remember" the result for later, use a variable instead.
213
System.out.println( Math.abs(-213) );
213
System.out.println( Math.abs(213) );
6.0
System.out.println( Math.ceil( 5.1 ) );
5.0
System.out.println( Math.ceil( Math.sqrt(17) ) );
8.0
System.out.println( Math.floor( 8.9 ) );
5.6
System.out.println( Math.max( 2.3, 5.6 ));
7
System.out.println( Math.max( 7,3) );
2.3
System.out.println( Math.min( 2.3, 5.6 ));
3
System.out.println( Math.min( 7,3) );
12
System.out.println( Math.round( 12.34 ) );
13
System.out.println( Math.round( 12.56 ) );
7.0
System.out.println( Math.sqrt( 49 ) );
Math.tan (value)
Tangent of an angle in radians
import java.awt.*;
This is the abstract window toolkit
Math.pow(x,y);
Uesd to raise a number to a cerain power, with x being the base and y being the power form
random integers int num = (int) (Math.random() * 100)
between 0 to 99 because
How can you calculate the area of a circle using Math.PI?
double area = Math.PI * radius * radius;
What package is the Math class a part of?
java.lang
Math.random();
random number generator
[0.0, 1.0)
range of Math.random( )
[10.0, 30.0)
range of Math.random( ) * 20 + 10
[0.0, 10.0)
range of Math.random( )*10
random double random()
returns a random number r, where 0.0 <= r <= 1.0
abs int abs(int x) double abs(double x)
returns absolute value
pow double pow(double b, double e)
returns b raised to the e power
sqrt double sqrt(double x)
returns square root of x
Math.max(x,y);
returns the higher of the two arguments
Math.min(x,y);
returns the lower of the two arguments
Math.round(x*10)/10.0
rounds a number to the nearest tenth