Skip to content

Commit

Permalink
BAEL-6226 Calculate Pi Java Program (eugenp#13693)
Browse files Browse the repository at this point in the history
* BAEL-6226 Calculate Pi Java Program

* BAEL-6226 Calculate Pi Java Program
  • Loading branch information
Michaelin007 authored Apr 4, 2023
1 parent d186dc6 commit 3cdc10d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core-java-modules/core-java-numbers-6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Relevant Articles:

29 changes: 29 additions & 0 deletions core-java-modules/core-java-numbers-6/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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);
}

}
1 change: 1 addition & 0 deletions core-java-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 3cdc10d

Please sign in to comment.