Home » Core Java » Pi in Java Example

About Mary Zheng

Mary Zheng
Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.

Pi in Java Example

In this post, we feature a comprehensive article about Pi in Java.

1. Introduction

PI is a mathematics number defined as the circumference of any circle divided by its diameter. Java has provided the java.lang.Math class since version 1. It includes a static constant PI with value of 3.141592653589793.

/*the double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.*/
static double PI = 3.141592653589793;

In this example, I will print out the Math.PI value and use it to calculate a circle’s circumference and area.

2. Technologies Used

The example code in this article was built and run using:

  • Java 11
  • Maven 3.3.9
  • Eclipse Oxygen
  • Junit 4.12

3. Maven Project

3.1 Dependencies

I will include Junit in the pom.xml.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>jcg.zheng.demo</groupId>
	<artifactId>java-pi-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.0</version>
				<configuration>
					<release>11</release>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
	</dependencies>
</project>

3.2 Circle

In this step, I will create a Circle class which has a diameter data member and two methods:

  • calculateCircumference() – calculate a circle’s circumference by multiplying PI by its diameter.
  • calculateArea() – calculate a circle’s area by multiplying PI by the square of its radius.

Circle.java

package jcg.zheng.demo;

public class Circle {

	public Circle(double diameter) {
		super();
		this.diameter = diameter;
	}

	private double diameter;

	public double calculateCircumference() {
		return Math.PI * diameter;
	}

	public double calculateArea() {
		return Math.PI * Math.pow(diameter / 2, 2);
	}
}

3.3. Circle Test

In this step, I will create a CircleTest class to print out Math.PI value and calculate a circle’s circumference and area.

  • print_Pi_value() – prints out Math.PI value.
  • calculateCircumference() – calculates the circumference of a circle whose diameter is 10 units.
  • calculateArea() – calculates the area of a circle whose diameter is 20 units.

CircleTest.java

package jcg.zheng.demo;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class CircleTest {

	@Test
	public void print_Pi_value() {
		System.out.println(Math.PI);
		System.out.printf("%.16f", Math.PI);
	 
		assertEquals(3.14, Math.PI, 2);
	}

	@Test
	public void calculateCircumference() {
		double diameter = 10;
		Circle c = new Circle(diameter);

		assertEquals(31.42, c.calculateCircumference(), 2);
	}
	
	@Test
	public void calculateArea() {
		double diameter = 20;
		Circle c = new Circle(diameter);

		assertEquals(314.23, c.calculateArea(), 2);
	}
}

Execute mvn test -Dtest=CircleTest and capture output here.

Output

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running jcg.zheng.demo.CircleTest
3.141592653589793
3.1415926535897930
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.126 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

Note: Math.PI value is defined as 3.141592653589793.

4. Pi in Java – Summary

In this example, I demonstrate the Math.PI value and how to use it to calculate a circle’s circumference and area.

5. Download the Source Code

This example consists of a Maven project which demonstrates the Math.PI constant.

Download
You can download the full source code of this example here: Pi in Java Example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
JoeHx
6 months ago

Oh, I thought you were going to calculate PI, not just use it. Useful post though, nevertheless.