Math Class: constants, main methods and calling methods from other classes


Learn the main methods, constants and how to use the Math class in Java. And learn how to make call to methods from other class.Let us study an important class, the Math Class, which comes several mathematical functions that will help us greatly in our complete an free Java course.


Allied to this large and powerful class with our Methods section, we'll learn how to use the methods that are declared in a different class that we're making the call.





Calling methods from other Java classes


So far we call the methods as follows:
method_name(arguments);

We did it because we define these methods within the same class (so far only made ​​Java applications containing one class. Classes will study in the next section).
However, when the method is not present in the class that we're making the call, the syntax is different, it is as follows:
Class_Name.method_name(arguments);

It is noteworthy that the statement is made that way because these methods are static (static). That is, it was not necessary to create an object of class Math (different class Scanner, for example).
In the next section of the course on Classes, you will understand more about static elements.


The Math Class from Java

To illustrate the use of methods of other classes, and to assist in the calculation of constants (such as the number pi, Euler's number), the calculation of trigonometric functions (sine, cosine, tangent, etc.) and other functionalities, we will introduce and use the Math class.

This class is already in the package java.lang. That is, we don't need to care in import, as we do with the Scanner class, for example.

Math class constants:

We will print the value of Pi and Euler's constant, the 'e' of exponential numbers.
These values ​​are stored in the constants PI and E, the Math class. The constants of other classes are accessed in the same manner as the methods, ie:
Math.PI
Math.E

Let's print these values​​:

public class constante {
    
    public static void main(String[] args){
        System.out.println("Pi value: " + Math.PI);
        System.out.println("Exponential value: " + Math.E);
    }

}



Exponential and potentiation in Math Class

To calculate things values ​​such as: e ^ x
use the method: exp(), which takes a double and returns a double.
number = math.exp(argument)

To calculate any powers, of the form: a ^ b
where a and b are of type double, use the method pow () in class Math
number = Math.pow(a, b)

public class Mathtest {
    
    public static void main(String[] args){
        System.out.println("'e' elevado ao quadrado = "+ Math.exp(2));
        System.out.println("2 cubed = " + Math.pow(2, 3));
    }

}


Remember that these methods return double.
If you have declared a float and get the result you want in your float variable, use the cast.
For example, in the case of our methods to calculate the BMI, the "square" returns a float, then do:
(float) Math.pow (height, 2);

Thus, the method Math.pow() will return a float instead of double.
See how would our methods:

    public static float BMI(float weight, float height){
        return weight/(float)Math.pow(height, 2);
    }
    
    public static float square(float num){
        return Math.pow(num,2);
    }


In fact, neither method  square() would need more, because the pow() already does this, but it's important that you learned how square works in programming, not only in Java.


Calculating the Square Root in Java through grade Math

To calculate the square root of a positive number, we use the method sqrt().
That receives and returns a double.

For example, a program that shows the square root of PI:


public class RaizDePi {
    
    public static void main(String[] args){
        System.out.println("Pi square root = "+ Math.sqrt( Math.PI ) );
    }

}



Calculating natural logarithms in Java through grade Math

Calculates the natural logarithms (ie, the base 'e') using the method: log()
that receives and returns doubles.

For example, a program that shows the natural logarithm of the number of 10 and 'e':



public class logarithms {
    
    public static void main(String[] args){
        System.out.println("Natural logarithm of 10 is = "+ Math.log(10) );
        System.out.println("Natural logarithm of 'e' is = "+ Math.log( Math.E ) );
    }

}




Calculating sines, cosines, tangents and other trigonometric functions in Java using the Math class

The main trigonometric functions are sine, cosine and tangent, and are calculated by the methods:
Math.sin()
Math.cos()
Math.tan()

They receive and return values ​​of type double. However, the values ​​that these functions must be given in RADIANS!




public class Trigonometrics {
    
    public static void main(String[] args){
        System.out.println("90 degrees sin = "+ Math.sin( (Math.PI)/2 ) );
        System.out.println("O degrees cosine = "+ Math.cos(0) );
        System.out.println("45 degrees tangent = "+ Math.tan( (Math.PI)/4 ));
    }

}



Module, maximum, minimum and rounding in Java through Math Class

To calculate the modulus of a number 'number' use: Math.abs(number)
To calculate the minimum of two numbers 'num1' and 'num2', use: Math.min(num1, num2)
To compute the maximum of two numbers 'num1' and 'num2', use: Math.max(num1, num2)

To round a number 'number' up, use: Math.ceil(number)
To round a number 'number' down, use: Math.floor(number)

These methods, like all others, receive and return double. If you would like to spend another type, use cast as explained in the method pow().


More mathematical methods in Java

Access the documentation:
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html



No comments: