BAEL-6226 Calculate Pi Java Program (#13693)

* BAEL-6226 Calculate Pi Java Program

* BAEL-6226 Calculate Pi Java Program
This commit is contained in:
Michael Olayemi 2023-04-04 03:06:12 +01:00 committed by GitHub
parent 5c520fc768
commit 4cebc2aaf1
4 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,2 @@
### Relevant Articles:

View File

@ -0,0 +1,29 @@
<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>
<artifactId>core-java-numbers-6</artifactId>
<name>core-java-numbers-6</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
</dependencies>
<build>
<finalName>core-java-numbers-6</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1,31 @@
package com.baeldung.pi;
import static org.junit.Assert.*;
import java.util.Random;
import org.junit.Test;
public class PiProgramUnitTest {
@Test
public void givenPiCalculator_whenCalculatePiWithTenThousandPoints_thenEstimatedPiIsWithinTolerance() {
int totalPoints = 10000;
int insideCircle = 0;
Random random = new Random();
for (long i = 0; i < totalPoints; i++) {
double x = random.nextDouble() * 2 - 1;
double y = random.nextDouble() * 2 - 1;
if (x * x + y * y <= 1) {
insideCircle++;
}
}
double pi = 4.0 * insideCircle / totalPoints;
assertEquals(Math.PI, pi, 0.01);
}
}

View File

@ -115,6 +115,7 @@
<module>core-java-numbers-3</module>
<module>core-java-numbers-4</module>
<module>core-java-numbers-5</module>
<module>core-java-numbers-6</module>
<module>core-java-optional</module>
<module>core-java-perf</module>
<module>core-java-reflection</module>