diff --git a/NathanResults.md b/NathanResults.md new file mode 100644 index 0000000..2d41088 --- /dev/null +++ b/NathanResults.md @@ -0,0 +1,19 @@ +##Nathan's Result +Code compiled and gave expected output +```java +*** +Simulation of 2 dice tossed for 1000000 times. +*** + 2 : 27470: 0.03 *** + 3 : 55425: 0.06 ****** + 4 : 83023: 0.08 ******** + 5 : 110886: 0.11 *********** + 6 : 139311: 0.14 *************** + 7 : 166757: 0.17 ***************** + 8 : 139440: 0.14 *************** + 9 : 110721: 0.11 *********** + 10 : 83396: 0.08 ******** + 11 : 55470: 0.06 ****** + 12 : 28101: 0.03 *** +``` +~yay~ \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7219542..d8403e1 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 + + 11 + 11 + + + + + + + junit + junit + RELEASE + test + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..16a119f 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,45 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Arrays; public class Bins { + private Integer[] boxes; + private final Integer min; + private final Integer max; + + + public Bins(Integer min, Integer max){ + this.min = min; + this.max = max; + this.boxes = new Integer[(max - min) + 1]; + Arrays.fill(boxes, 0); + } + public Integer[] getBoxes() { + return boxes; + } + + public void fillBins(Integer number){ + int index = number - min; + boxes[index]++; + } + + public Integer getBin(int binIndex) { + return boxes[binIndex - min]; + } + + public void incrementBin(int binIndex) { + boxes[binIndex - min]++; + } + + public double getPercent(Integer boxOfResult){ + Integer sum = 0; + for(Integer number: boxes){ + sum += number; + } + double raw = (double) this.getBin(boxOfResult)/sum; + BigDecimal rounded = new BigDecimal(raw).setScale(2, RoundingMode.HALF_EVEN); + double result = rounded.doubleValue(); + return result; + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..7e072db 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,31 @@ +import java.util.ArrayList; + public class Dice { + private ArrayList rollResults; + + public Dice(Integer countOfDice) { + this.rollResults = new ArrayList<>(); + for (int i = 0; i < countOfDice; i++) { + this.rollResults.add((int) ((Math.random() * (7 - 1)) + 1)); + } + } + + public ArrayList getDiceList() { + return rollResults; + } + public void toss(){ + for (int i = 0; i < rollResults.size(); i++) { + rollResults.set(i, (int) ((Math.random() * (7 - 1)) + 1)); + } + } + public Integer tossAndSum() { + Integer sum = 0; + for (int i = 0; i < rollResults.size(); i++) { + rollResults.set(i, (int) ((Math.random() * (7 - 1)) + 1)); + sum += rollResults.get(i); + } + return sum; + } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..99902e2 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,9 @@ +public class Main { + + public static void main(String[] args) { + Simulation newSim = new Simulation(2, 1000000); + newSim.runSim(); + newSim.printResult(); + + } +} diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..4c99d25 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,70 @@ +import java.math.BigDecimal; + public class Simulation { + private Dice dice; + private Bins bin; + private Integer numOfToss; + private Integer numOfDice; + private Integer min; + + public Simulation(Integer numOfDice, Integer numOfToss){ + this.numOfToss = numOfToss; + this.numOfDice = numOfDice; + this.min = numOfDice; + this.dice = new Dice(numOfDice); + this.bin = new Bins(numOfDice, numOfDice * 6); + } + + public void runSim(){ + for (int i = 0; i < numOfToss; i++) { + bin.fillBins(dice.tossAndSum()); + } + } + + public void printResult(){ + //Heading + String result = "***\n" + + "Simulation of " + numOfDice + " dice tossed for " + numOfToss+ " times.\n" + + "***\n"; + //Iterating through each box and constructing string of boxNumber, values, percentage, stars. + Integer[] thisBin = bin.getBoxes(); + for(int i = 0; i < thisBin.length; i++) { + int boxId = i + min; + String addToResult = getBoxNumber(boxId) + " :" + getBoxValue(bin.getBin(boxId)) + ": " + bin.getPercent(boxId) + + " " + getStars(bin.getPercent(boxId)) + "\n"; + result += addToResult; + } + System.out.println(result); + } + + private String getBoxNumber(Integer boxID){ + String result = ""; //3 spaces + Integer whiteSpaces = 3 - boxID.toString().length(); + for (int j = 0; j < whiteSpaces; j++) { + result += " "; + } + result += boxID.toString(); + return result; + } + + private String getBoxValue(Integer value){ + String result = ""; //9 spaces + Integer spaces = 9 - value.toString().length(); + for (int i = 0; i < spaces; i++) { + result += " "; + } + result += value.toString(); + return result; + } + public String getStars(double number){ + StringBuilder result = new StringBuilder(); + double numOfStars = number * 100; + for (int i = 0; i < numOfStars; i++) { + result.append("*"); + } + return result.toString(); + } } diff --git a/src/test/java/BinsTest.java b/src/test/java/BinsTest.java new file mode 100644 index 0000000..e14f3e1 --- /dev/null +++ b/src/test/java/BinsTest.java @@ -0,0 +1,92 @@ +import org.junit.Test; + +import java.math.BigDecimal; +import java.util.Arrays; + +public class BinsTest { + @Test + public void binsTest(){ + //given + Integer min = 2; + Integer max = 12; + //when + Bins bin = new Bins(min, max); + //then + System.out.println(Arrays.toString(bin.getBoxes())); + } + + @Test + public void fillBinsTest(){ + //given + Bins bin = new Bins(2, 12); + Dice dice = new Dice(2); + //when + for (int i = 0; i < 1000000; i++) { + Integer input = dice.tossAndSum(); + bin.fillBins(input); + } + String result = Arrays.toString(bin.getBoxes()); + //then -check by looking at + System.out.println(result); + } + + @Test + public void getBinTest(){ + //given + Bins bin = new Bins(2, 12); + Dice dice = new Dice(2); + //when + for (int i = 0; i < 1000; i++) { + Integer input = dice.tossAndSum(); + bin.fillBins(input); + } + String wholeBin = Arrays.toString(bin.getBoxes()); + Integer bin5 = bin.getBin(5); + Integer bin12 = bin.getBin(12); + //Then + System.out.println(wholeBin); + System.out.println(bin5); + System.out.println(bin12); + + } + + @Test + public void incrementBinTest(){ + //given + Bins bin = new Bins(2, 12); + Dice dice = new Dice(2); + //when + for (int i = 0; i < 1000; i++) { + Integer input = dice.tossAndSum(); + bin.fillBins(input); + } + String wholeBin = Arrays.toString(bin.getBoxes()); + Integer bin12 = bin.getBin(12); + bin.incrementBin(12); + Integer bin12AfterIncrement = bin.getBin(12); + + //Then + System.out.println(wholeBin); + System.out.println(bin12); + System.out.println(bin12AfterIncrement); + + } + + @Test + public void getPercentTest(){ + //given + Bins bin = new Bins(2, 12); + Dice dice = new Dice(2); + for (int i = 0; i < 1000; i++) { + Integer input = dice.tossAndSum(); + bin.fillBins(input); + } + String wholeBin = Arrays.toString(bin.getBoxes()); + //when + double result = bin.getPercent(12); + //then + System.out.println(wholeBin); + System.out.println(result); + } + +} diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..9302d1b --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,35 @@ +import org.junit.Test; + +import java.util.Arrays; + +public class DiceTest { + @Test + public void diceTest(){ + //given + Dice newDices = new Dice(10); + //when + Integer[] dices = newDices.getDiceList().toArray(new Integer[0]); + //then + System.out.println(Arrays.toString(dices)); + } + + @Test + public void tossAndSumTest(){ + //given + Dice newDices = new Dice(10); + Integer[] preToss = newDices.getDiceList().toArray(new Integer[0]); + + //when + Integer toss = newDices.tossAndSum(); + Integer[] postToss = newDices.getDiceList().toArray(new Integer[0]); + + //then + //Check with output + System.out.println(Arrays.toString(preToss)); + System.out.println("================After Tossing================"); + System.out.println(Arrays.toString(postToss)); + System.out.println(toss); + + } + +} diff --git a/src/test/java/SimulationTest.java b/src/test/java/SimulationTest.java new file mode 100644 index 0000000..d48b5b2 --- /dev/null +++ b/src/test/java/SimulationTest.java @@ -0,0 +1,22 @@ +import org.junit.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class SimulationTest { + + @Test + public void getStarsTest(){ + //given + Simulation newSim = new Simulation(2, 10); + newSim.runSim(); + double number = .05; + BigDecimal num = new BigDecimal(number).setScale(2, RoundingMode.HALF_DOWN); + String result = newSim.getStars(number); + System.out.println(result); + + + + } + +}