Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Math Class Methods

Note that the methods are static

 

Radians Review

Math Methods

  Examples

 

 

   

 
Math Methods

The standard java library provides a class in which many of the most commonly used mathematical functions and constants are defined.  It is in the java.lang package; lang inherits directly from Object.
 
The functions and constants in the Math class are defined as static.  Thus you do not construct an object of the class Math but refer to the function by beginning the name with the class resolution of Math.  For example to use the trigonometric function, sine, your code might look like:

    double x, y;
    y = 17.5;                             // y is measured in radians
    x = Math.sin(y);                  // Note use of Math (because method is static - no object requiredNote that you do not have to write java.lang.Math
                                               // because java.lang is imported  automatically.

Math Class Methods (partial)

 

Method

What it Returns

static int abs(int x)

Absolute value of an integer x

static double abs (double x)

Absolute value of a double x

static double pow (double base, double exponent)

Base raised to the exponent

static long round (double x)

X rounded to the nearest whole number. Returned value must be cast to be an int before assignment to an int variable

static int max (int a, int b)

The greater of a and b

static int min (int a, int b)

The lesser of a and b

static double sqrt (double x)

Square root of x