forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-6226 Calculate Pi Java Program (eugenp#13693)
* BAEL-6226 Calculate Pi Java Program * BAEL-6226 Calculate Pi Java Program
- Loading branch information
1 parent
d186dc6
commit 3cdc10d
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### Relevant Articles: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
31 changes: 31 additions & 0 deletions
31
core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/pi/PiProgramUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters