Skip to content

Lab Completed #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>Dicey-Lab</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>


</project>
22 changes: 22 additions & 0 deletions src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
import java.util.HashMap;
import java.util.Map;

public class Bins {
private Integer lower;
private Integer upper;

private Map<Integer, Integer> binsMap = new HashMap<>();

public Bins(Integer lower, Integer upper) {
this.lower = lower;
this.upper = upper;

for (int i = lower; i <= upper; i++) {
binsMap.put(i, 0);
}
}

public Integer getBin(int bin) {
return binsMap.get(bin);
}

public void incrementBin(int bin) {
binsMap.put(bin, getBin(bin)+1);
}
}
19 changes: 19 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import java.util.Random;

public class Dice {
private Integer numberOfDie;

public Dice(int numberOfDie) {
this.numberOfDie = numberOfDie;
}

public Integer tossAndSum() {
Random random = new Random();
Integer sum = 0;

for (int i = 0; i < numberOfDie; i++) {
sum += random.nextInt(6) + 1;
}
return sum;
}

public Integer getNumOfDie() {
return this.numberOfDie;
}
}
8 changes: 8 additions & 0 deletions src/main/java/MainApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class MainApplication {
public static void main(String[] args) {
Simulation sim = new Simulation(2, 1000000);

sim.runSimulation();
sim.printResults();
}
}
56 changes: 56 additions & 0 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
public class Simulation {
private Integer numberOfDie;
private Integer numberOfTosses;
private Bins bins;

public Simulation(Integer numberOfDie, Integer numberOfTosses) {
this.numberOfDie = numberOfDie;
this.numberOfTosses = numberOfTosses;
}

public Integer getNumOfDie() {
return this.numberOfDie;
}

public Integer getNumOfTosses() {
return this.numberOfTosses;
}

public Bins runSimulation() {
Dice dice = new Dice(numberOfDie);
bins = new Bins(numberOfDie, numberOfDie*6);
Integer result;

for (int i = 0; i < numberOfTosses; i++) {
result = dice.tossAndSum();
bins.incrementBin(result);
}
return bins;
}

public void printResults() {

System.out.println(new StringBuilder()
.append("\n***")
.append("\nSimulation of " + numberOfDie + " dice tossed " + numberOfTosses + " times.")
.append("\n***")
+ printBins(bins));

}


private String printBins(Bins bins) {
String binLine = "";
Double binPercent;
Integer stars = 0;

for(int i = numberOfDie; i <= (numberOfDie*6); i++) {
binPercent = ((double) bins.getBin(i)) / numberOfTosses;
binLine += String.format("\n%2d : %8d : %.2f ", i, bins.getBin(i), binPercent);

stars = (100 * bins.getBin(i)) / numberOfTosses;

for (int j = 0; j < stars; j++) {
binLine += "*";
}
}
return binLine;
}


}
34 changes: 34 additions & 0 deletions src/test/java/BinsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import org.junit.Assert;
import org.junit.Test;

public class BinsTest {

@Test
public void incrementBinTest() {
//Given
Bins bins = new Bins(2, 12);
Integer expected = 1;

//When
bins.incrementBin(10);
Integer actual = bins.getBin(10);

//Then
Assert.assertEquals(expected, actual);
}

@Test
public void binsConstructorTest() {
//Given
Integer lower = 2;
Integer upper = 12;
Integer expected = 0;

//When
Bins bins = new Bins(lower, upper);
Integer actual = bins.getBin(2);

//Then
Assert.assertEquals(expected, actual);
}
}
42 changes: 42 additions & 0 deletions src/test/java/DiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import org.junit.Assert;
import org.junit.Test;

public class DiceTest{

@Test
public void diceConstructorTest() {
//Given
Integer expectedNumOfDie = 8;

//When
Dice dice = new Dice(expectedNumOfDie);
Integer actual = dice.getNumOfDie();

//Then
Assert.assertEquals(expectedNumOfDie, actual);
}

@Test
public void tossAndSumTwoDiceTest() {
//Given
Dice dice = new Dice(2);

//When
Integer toss = dice.tossAndSum();

//Then
Assert.assertTrue((toss > 1) && (toss < 13));
}

@Test
public void tossAndSumFiveDiceTest() {
//Given
Dice dice = new Dice(5);

//When
Integer toss = dice.tossAndSum();

//Then
Assert.assertTrue((toss > 4) && (toss < 31));
}
}
22 changes: 22 additions & 0 deletions src/test/java/SimulationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.junit.Assert;
import org.junit.Test;

public class SimulationTest {

@Test
public void simulationConstructorTest() {
//Given
Integer numberOfDie = 2;
Integer numberOfTosses = 10000;

//When
Simulation sim = new Simulation(numberOfDie, numberOfTosses);
Integer actualNumOfDie = sim.getNumOfDie();
Integer actualNumOfTosses = sim.getNumOfTosses();

//Then
Assert.assertEquals(numberOfDie, actualNumOfDie);
Assert.assertEquals(numberOfTosses, actualNumOfTosses);
}

}