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:

 

English name

Function name

Square root

sqrt

Cube root

cbrt

Maximum value

max

Minimum value

min

Power

pow

Random Number

random

Round

round

Ceiling

ceil

Floor

floor

Absolute Value

abs

 

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

 

  • These examples give us an idea of how to use some static methods. However, it is not possible to look at examples of every single method. A more general system is needed.

  • A method prototype provides us with the information needed to use a specific method:
    • The name of the method.
    • The arguments (parameters), their types and their order.
    • The type of value that is computed and returned (if any).

 

  • To use static methods, we also need to know that class that the method belongs to.

  • A method prototype has this form:


typeOfMethod returnType methodName(type1 arg1, type2 arg2, …)

 

  • For now, the typeOfMethod is always static.


  • The returnType is the type of the answer returned by method.


  • The methodName is the name of the method. It follows the same naming convention as variables.


  • The argument list is a listing of all the parameters and their types that must be given to the method to do the required calculations.

 

  • Here are examples of some method prototypes from the Math class:


static double sqrt(double x)

static double pow(double x, double y)

static double tan(double x)

static double random()

 

 

RANDOM NUMBERS

 

  • The Math class contains a method called random() that generates a random number that is greater or equal to 0.0 and less than 1.

 

  • We can generate integers in a specific range by using this approach:

int randomNumber = (int)(range * Math.random()) + lowerBound;

 

          where

range is upper bound - lower bound + 1,

           lowerBound is the lowest possible random number.

 

  • For example, to generate a random number between 1 and 100, we would use this:

 

int random = (int)(100 * Math.random()) + 1;


API SPECIFICATIONS

 

  • One can get a full listing of function prototypes in the Java documentation found on the Java Website. Follow these instructions:

    • Google “Java API”