diff --git a/ZachResults.md b/ZachResults.md new file mode 100644 index 0000000..1886f6c --- /dev/null +++ b/ZachResults.md @@ -0,0 +1,15 @@ +*** +Simulation of 2 dice tossed for 1000000 times. +*** + +2 : 27389: 0.03 +3 : 55543: 0.06 +4 : 83925: 0.08 +5 : 110847: 0.11 +6 : 139083: 0.14 +7 : 166554: 0.17 +8 : 139119: 0.14 +9 : 110969: 0.11 +10 : 83486: 0.08 +11 : 55431: 0.06 +12 : 27654: 0.03 diff --git a/pom.xml b/pom.xml index 7219542..1b5e145 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,14 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + junit + junit + 4.13.1 + test + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..c6d0b21 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,39 @@ +import java.util.*; public class Bins { + private int startingBin; + private int endingBin; + Integer[] diceRolls; + + public Bins(int startingBin, int endingBin) { + this.startingBin = startingBin; + this.endingBin = endingBin; + diceRolls = new Integer[endingBin - (startingBin - 1)]; + Arrays.fill(diceRolls, 0); + } + + public int getBin(int binNumber) { + return diceRolls[binNumber - startingBin]; + } + + public void incrementBin(int binNumber) { + diceRolls[binNumber - startingBin]++; + } + + public int getStartingBin() { + return startingBin; + } + + public void setStartingBin(int startingBin) { + this.startingBin = startingBin; + } + + public int getEndingBin() { + return endingBin; + } + + public void setEndingBin(int endingBin) { + this.endingBin = endingBin; + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..0d1c667 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,27 @@ +import java.util.Random; + public class Dice { + private int numberOfDice; + + public Dice(Integer numberOfDice) { + this.numberOfDice = numberOfDice; + } + + public Integer tossAndSum() { + int sum = 0; + for (int i = 0; i < numberOfDice; i++) { + sum += (int)(Math.random() * 6 + 1); + } + return sum; + + } + + public Integer getNumberOfDice() { + return numberOfDice; + } + public void setNumberOfDice(Integer numberOfDice) { + this.numberOfDice = numberOfDice; + } } diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..512c26d 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,48 @@ +import java.util.*; + public class Simulation { + private int numberOfDies; + private int numberOfTosses; + + public Simulation(Integer numberOfDies, Integer numberOfTosses) { + this.numberOfDies = numberOfDies; + this.numberOfTosses = numberOfTosses; + } + + public Integer[] runSimulation() { + Dice die = new Dice(numberOfDies); + Bins results = new Bins(numberOfDies, numberOfDies * 6); + for (int i = 0; i < numberOfTosses; i++) { + int diceRoll = die.tossAndSum(); + results.incrementBin(diceRoll); + } + return results.diceRolls; + } + + public void printResults() { + Integer[] diceRolls = runSimulation(); + StringBuilder builder = new StringBuilder(); + System.out.printf("***\nSimulation of %s dice tossed for %s times.\n***\n\n", numberOfDies, numberOfTosses); + for (int i = 0; i < diceRolls.length; i++) { + for (int j = 0; j < (diceRolls[i] / numberOfTosses) * 100; i++) { + builder.append("*"); + } + System.out.printf("%s : %s: %.2f %s\n", i + numberOfDies, diceRolls[i], (double)diceRolls[i] / numberOfTosses, builder.toString()); + builder.delete(0, builder.length()); + } + } + + public Integer getNumberOfDies() { + return numberOfDies; + } + public Integer getNumberOfTosses() { + return numberOfTosses; + } + public static void main(String[] args) { + Simulation simulation = new Simulation(2, 1000000); + simulation.printResults(); + } } diff --git a/src/test/java/BinsTest.java b/src/test/java/BinsTest.java new file mode 100644 index 0000000..3c4dad5 --- /dev/null +++ b/src/test/java/BinsTest.java @@ -0,0 +1,22 @@ +import org.junit.Assert; +import org.junit.Test; + +public class BinsTest { + + @Test + public void constructorTest() { + // Given + Integer expectedStartingBin = 2; + Integer expectedEndingBin = 12; + Bins results = new Bins(expectedStartingBin, expectedEndingBin); + + + // When + Integer actualStartingBin = results.getStartingBin(); + Integer actualEndingBin = results.getEndingBin(); + + // Then + Assert.assertEquals(expectedStartingBin, actualStartingBin); + Assert.assertEquals(expectedEndingBin, actualEndingBin); + } +} diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..a5dee12 --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,46 @@ +import org.junit.Assert; +import org.junit.Test; + +public class DiceTest { + + @Test + public void tossAndSumTest() { + // Given + Integer numberOfDice = 2; + + // When + Dice dice = new Dice(numberOfDice); + Integer sumOfDice = dice.tossAndSum(); + + // Then + Assert.assertTrue(sumOfDice >= numberOfDice && sumOfDice <= numberOfDice * 6); + } + + @Test + public void tossAndSumTest1() { + // Given + Integer numberOfDice = 4; + + // When + Dice dice = new Dice(numberOfDice); + Integer sumOfDice = dice.tossAndSum(); + + // Then + Assert.assertTrue(sumOfDice >= numberOfDice && sumOfDice <= numberOfDice * 6); + } + + @Test + public void constructorTest() { + // Given + Integer expectedNumberOfDice = 4; + Dice dice = new Dice(expectedNumberOfDice); + + + // When + Integer actualNumberOfDice = dice.getNumberOfDice(); + + // Then + Assert.assertEquals(expectedNumberOfDice, actualNumberOfDice); + } + +} diff --git a/src/test/java/SimulationTest.java b/src/test/java/SimulationTest.java new file mode 100644 index 0000000..c2388bb --- /dev/null +++ b/src/test/java/SimulationTest.java @@ -0,0 +1,22 @@ +import org.junit.Assert; +import org.junit.Test; + +public class SimulationTest { + + @Test + public void constructorTest() { + // Given + Integer expectedNumberOfDies = 2; + Integer expectedNumberOfTosses = 1000000; + + + // When + Simulation simulation = new Simulation(expectedNumberOfDies, expectedNumberOfTosses); + Integer actualNumberOfDies = simulation.getNumberOfDies(); + Integer actualNumberOfTosses = simulation.getNumberOfTosses(); + + // Then + Assert.assertEquals(expectedNumberOfDies, actualNumberOfDies); + Assert.assertEquals(expectedNumberOfTosses, actualNumberOfTosses); + } +}