From 279f35ca603a18fbd5d603f267dd1fe769a3cc93 Mon Sep 17 00:00:00 2001 From: Dee Date: Sun, 11 Jul 2021 17:22:16 -0400 Subject: [PATCH 1/3] results printed, astericks need to be fixed --- pom.xml | 20 +++++++++++ src/main/java/Bins.java | 22 ++++++++++++ src/main/java/Dice.java | 18 ++++++++++ src/main/java/Simulation.java | 43 ++++++++++++++++++++++++ src/test/java/BinTest.java | 63 +++++++++++++++++++++++++++++++++++ src/test/java/DiceTest.java | 19 +++++++++++ 6 files changed, 185 insertions(+) create mode 100644 src/test/java/BinTest.java create mode 100644 src/test/java/DiceTest.java diff --git a/pom.xml b/pom.xml index 7219542..0621c44 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,26 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 14 + 14 + + + + + + + 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..81a2b11 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,26 @@ public class Bins { + private int min; + private int max; + Integer[] sumOfRolls; + + public Bins(int min, int max){ + this.min = min; + this.max = max; + this.sumOfRolls = new Integer[(max - min) + 1]; + for (int i = 0; i < sumOfRolls.length; i++) { + sumOfRolls[i] = 0; + } + } + + public Integer getBin(int oneSum){ + int index = oneSum-min; + return sumOfRolls[index]; + } + + public void incrementBin(int sameSum){ + int index = sameSum-min; + sumOfRolls[index]++; + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..b6337c8 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,22 @@ public class Dice { + public static void main(String[] args) { + Dice dice = new Dice(2); + Integer testInt = dice.tossAndSum(); + System.out.println(testInt); + } + private int numberOfDie; + public Dice(int numOfDie){ + this.numberOfDie = numOfDie; + } + + public int tossAndSum(){ + int sum = 0; + for (int i = 0; i < numberOfDie; i++) { + sum += ((Math.random()*6)+1); + } // 0.0000001 - 0.9999999 + + return sum; + } } diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..a94094c 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,48 @@ +import java.util.Arrays; + public class Simulation { + public static void main(String[] args) { + Simulation sim = new Simulation(2, 1000000); + sim.runSimulation(); + sim.printResults(); + + } + int numberOfDie; + int numberOfRolls; + Integer[] results; + + public Simulation(int numberOfDie, int rolls){ + this.numberOfDie = numberOfDie; + this.numberOfRolls = rolls; + } + + public Integer[] runSimulation(){ + + Dice simDice = new Dice(this.numberOfDie); + Bins testBin = new Bins(this.numberOfDie,(this.numberOfDie*6)); + + for (int i = 0; i < this.numberOfRolls; i++) { + int simSum = simDice.tossAndSum(); + testBin.incrementBin(simSum); + } + return testBin.sumOfRolls; + } + + public void printResults() { + Integer[] results = runSimulation(); + + for (int i = 0; i < results.length; i++) { + int index = i+2; + int freq = results[i]; + float percent = (float) results[i] / this.numberOfRolls; + int asterickValue = (int) percent * 100; + System.out.printf("%2d : %8d : %.2f " + "* \n", index, freq, percent); + + } + + } + } diff --git a/src/test/java/BinTest.java b/src/test/java/BinTest.java new file mode 100644 index 0000000..ab65407 --- /dev/null +++ b/src/test/java/BinTest.java @@ -0,0 +1,63 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + +public class BinTest { + + @Test + public void testBinConstructor(){ + //given + int testMin = 2; + int testMax = 12; + int expected = (testMax - testMin) + 1; + + //when + Bins test = new Bins(testMin, testMax); + int actualLength = test.sumOfRolls.length; + int actualValue = test.sumOfRolls[1]; + + //then + Assert.assertEquals(expected, actualLength); + Assert.assertEquals(0, actualValue); + + } + + @Test + public void testBinIncrement(){ + //given + Bins test = new Bins(2, 12); + int testSum = 7; + + //when + + test.incrementBin(testSum); + + //then + int actual = test.getBin(testSum); + String testString = Arrays.toString(test.sumOfRolls); + //Do not use String.valueOf for Integer ARRAYS! + System.out.println(testString); + Assert.assertEquals(1, actual); + + } + + @Test + public void testBinIncrement2() { + //given + Bins test = new Bins(2, 12); + int testSum = 7; + + //when + + test.incrementBin(testSum); + test.incrementBin(testSum); + + //then + int actual = test.getBin(testSum); + String testString = Arrays.toString(test.sumOfRolls); + //Do not use String.valueOf for Integer ARRAYS! + System.out.println(testString); + Assert.assertEquals(2, actual); + } +} diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..5d3ecae --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,19 @@ +import org.junit.Assert; +import org.junit.Test; + +public class DiceTest { + + @Test + public void testDiceConstructor(){ + //given + int numOfDie = 2; + Dice dice = new Dice(numOfDie); + + //when + Integer testInt = dice.tossAndSum(); + + //then + System.out.println(testInt); //should be a number between 2 and 12 + Assert.assertTrue(testInt < 13 && testInt > 1); + } +} From a7c2022dc0b5bf710d444324bb3e5861e0148c47 Mon Sep 17 00:00:00 2001 From: Dee Date: Sun, 11 Jul 2021 21:00:28 -0400 Subject: [PATCH 2/3] printed results formatted --- src/main/java/Simulation.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index a94094c..11e1db3 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -36,9 +36,12 @@ public void printResults() { int index = i+2; int freq = results[i]; float percent = (float) results[i] / this.numberOfRolls; - int asterickValue = (int) percent * 100; - System.out.printf("%2d : %8d : %.2f " + "* \n", index, freq, percent); - + int asterickValue = (int) (percent * 100); //casting takes precedence over order of operations + System.out.printf("%2d : %8d : %.2f ", index, freq, percent); + for (int j = 0; j < asterickValue; j++) { + System.out.print("*"); + } + System.out.println(); } } From 18dbe8ce736f8f5df25ff23f7c42203364ea4e66 Mon Sep 17 00:00:00 2001 From: Dee Date: Sun, 11 Jul 2021 21:12:32 -0400 Subject: [PATCH 3/3] results file added --- DeesResults.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 DeesResults.md diff --git a/DeesResults.md b/DeesResults.md new file mode 100644 index 0000000..0391308 --- /dev/null +++ b/DeesResults.md @@ -0,0 +1,13 @@ + Simulation of 2 Dice tossed for 1000000 times + + 2 : 27922 : 0.03 ** + 3 : 56240 : 0.06 ***** + 4 : 83366 : 0.08 ******** + 5 : 111484 : 0.11 *********** + 6 : 139353 : 0.14 ************* + 7 : 166432 : 0.17 **************** + 8 : 138708 : 0.14 ************* + 9 : 110307 : 0.11 *********** +10 : 82890 : 0.08 ******** +11 : 55627 : 0.06 ***** +12 : 27671 : 0.03 ** \ No newline at end of file