# How to Use Pi Constant in Java

Computer programs can help us with numerous math functions. Java provides a huge library of tools and functions for mathematical operations. This lesson covers the use of pi in Java.

Pi in Java

Java has very powerful math functions, including pi. If you need to work with pi in Java, there is PI, which is a built-in math function that’s part of the overall Math class. For many Java programs, you’ll notice that you have to import the parent class to get special tools such as importing input or output classes so we can accept user input. The Java Math class, however, is included in the lang (language) class, and that’s imported by default. However, it’s considered good programing practice to import these classes anyway. This shows right away that your program will be using math for its calculations. The import code looks like this:

import java.lang.Math.*;

Even though pi goes on seemingly forever, Java stores the value of pi as a constant, or 3.141592653589793. For programming purposes, this is precise enough. Normally we don’t like the term ‘good enough’ in mathematics, but we can’t have infinite values in our Java variables. For our purposes, and even complex gaming or medical systems, the precision is acceptable.

Since PI is a constant, you cannot set its value, say back to 3.14. It is a final variable, meaning that you cannot change its value. This is a good thing, since we wouldn’t want any code to be able to change a key foundation of mathematics.

Here is the code to invoke Math.PI:

// let’s figure the area of a circle

double radius = 15;

double area;

area = Math.PI * radius * radius;

When we run the program, this output is displayed:

Java pi basic output

An Example

Okay, let’s now consider a little more complicated example. This one calculates the volume of a cylinder, which might come in handy for game programming or other fun programming. Recall that the volume of a cylinder is its area times the length. To get the area, we need to multiply pi times the radius squared. First, we will calculate area, then the volume. To make the code easier to follow, we’ll set the radius and length as constants, like this:

import java.lang.Math.*;

public class Pie {

public static void main(String[] args) {

//radius and length

double radius = 5;

double len = 15;

// calculate the area using PI

double area = radius * radius * Math.PI;

//and now volume

double volume = area * len;

System.out.println(“Volume of cylinder is: ” + volume);

}

}

Here’s another useful application of both PI and Java’s object-oriented nature. We’ll create our own classes for a Circle and a Cylinder. That way, we can send different values to each of those classes and get back the volume or area, without having to re-write a lot of code, like this:

public class Circle

// setup calculation of area

public double calcArea(double r) {

return Math.PI * r * r;

}

}

Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code “Newclient”

The post How to Use Pi Constant in Java appeared first on Superb Professors.