Java TOPIC 10 – MATH FUNCTIONS LESSON NOTE MATH FUNCTIONALITY Most computer
languages have a form of built-in commands that allow for advanced
mathematical calculations (such as taking the square root of a number or
calculating the sine of an angle). In Java,
there is a Math class that contains many different mathematical functions. LIST OF FUNCTIONS Here is a
partial list of functions available in the Math class:
FUNCTION CALL We refer to
the use of a function as a function
call. Because these
functions are inside another class, we must specify the class name in front
of them. So, all function call will
start with Math.functionName(…). EXPLANATION BY EXAMPLE Here is an
example of a function call for the square root function.
Java will
send the value 434.23 to the code inside the square root function. There, an answer will be calculated. That answer will then replace the entire
function call. EXAMPLES OF USING
FUNCTIONS Example 1 – Write the statement that will
calculate the square root of 434.23 and store that answer in a variable
called n. double n = Math.sqrt(434.23); Here is an
explanation of each part of the function call: Example 2 – Write the statement that will
calculate the value of 2.34.1.
double x = Math.pow(2.3,
4.1); Example 3 – Write the statement that will round
the value 4.62. long v = Math.round(4.62); FUNCTION PROTOTYPES
static
double sqrt(double x) static
double pow(double x, double y) static
double tan(double x) static
double random() RANDOM NUMBERS
int randomNumber
= (int)(range * Math.random()) + lowerBound; where range is upper bound - lower bound + 1, lowerBound is the lowest possible random number.
int random = (int)(100 * Math.random()) + 1; API SPECIFICATIONS
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|