JAVATM- The Beginnings

Math Operations

Section 1 - The Math Class

The Math class provides several methods which perform mathematical operations, and several useful constants.. All methods and constants are declared static, so an object of class Math never needs to be created.

Section 2 - Math Class Constants

The Math class defines two constants: PI (for a circle’s circumference) and E (for natural logarithms).

Listing 1 - CalculateCircumference.java

import java.text.DecimalFormat;

public class CalculateCircumference

{

    public static void main(String[] args)

    {

        DecimalFormat output = new DecimalFormat("##.00");

        double diameter = 4.0;

        double circumference = diameter * Math.PI;

        System.out.println("circumference = " +

                           output.format(circumference));

    }

}

PROGRAM OUTPUT

circumference = 12.57

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.



Back to ralphlecessi.com