From 28b49a3b5ab64af20e2921daf426afcd8b730c88 Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 8 Jul 2021 14:46:01 -0400 Subject: [PATCH 1/5] made dice and diceTest --- pom.xml | 20 ++++++++++++++++++++ src/main/java/Dice.java | 20 ++++++++++++++++++++ src/test/java/DiceTest.java | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/test/java/DiceTest.java 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/Dice.java b/src/main/java/Dice.java index 2283c96..4f2c3d3 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,24 @@ +import java.util.ArrayList; +import java.util.List; + public class Dice { + private ArrayList diceList = new ArrayList(); + public Dice(Integer countOfDice) { + for (int i = 0; i < countOfDice; i++) { + this.diceList.add((int) ((Math.random() * (6 - 1)) + 1)); + } + } + public ArrayList getDiceList() { + return diceList; + } + public Integer tossAndSum() { + Integer sum = 0; + for (int i = 0; i < diceList.size(); i++) { + diceList.set(i, (int) ((Math.random() * (6 - 1)) + 1)); + sum += diceList.get(i); + } + return sum; + } } diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..112bc16 --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,35 @@ +import org.junit.Test; + +import java.lang.reflect.Array; +import java.util.ArrayList; +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); + + } + +} From 599163c226dedb41de2018063e5abba9ff62fe3a Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 9 Jul 2021 23:42:29 -0400 Subject: [PATCH 2/5] Finished all sections and printed --- src/main/java/Bins.java | 41 ++++++++++++++ src/main/java/Dice.java | 21 ++++--- src/main/java/Main.java | 8 +++ src/main/java/Simulation.java | 65 ++++++++++++++++++++++ src/test/java/BinsTest.java | 92 +++++++++++++++++++++++++++++++ src/test/java/DiceTest.java | 4 +- src/test/java/SimulationTest.java | 22 ++++++++ 7 files changed, 244 insertions(+), 9 deletions(-) create mode 100644 src/main/java/Main.java create mode 100644 src/test/java/BinsTest.java create mode 100644 src/test/java/SimulationTest.java 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 4f2c3d3..7e072db 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,23 +1,30 @@ import java.util.ArrayList; -import java.util.List; public class Dice { - private ArrayList diceList = new ArrayList(); + private ArrayList rollResults; public Dice(Integer countOfDice) { + this.rollResults = new ArrayList<>(); for (int i = 0; i < countOfDice; i++) { - this.diceList.add((int) ((Math.random() * (6 - 1)) + 1)); + this.rollResults.add((int) ((Math.random() * (7 - 1)) + 1)); } } + public ArrayList getDiceList() { - return diceList; + 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 < diceList.size(); i++) { - diceList.set(i, (int) ((Math.random() * (6 - 1)) + 1)); - sum += diceList.get(i); + 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..4c315a8 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,8 @@ +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..3b7b094 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; + private Integer max; + + 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(){ + String result = "***\n" + + "Simulation of " + numOfDice + " dice tossed for " + numOfToss+ " times.\n" + + "***\n"; + Integer[] thisBin = bin.getBoxes(); + for(int i = 0; i < thisBin.length; i++) { + int boxId = i + min; + String addToResult = getBoxNumber(i) + " :" + getBoxValue(bin.getBin(i + min)) + " : " + bin.getPercent(i + min) + + " " + getStars(bin.getPercent(i + min)) + "\n"; + result += addToResult; + } + System.out.println(result); + } + + private String getBoxNumber(Integer i){ + String result = ""; //3 spaces + Integer numberSpaces = i + min; + Integer whiteSpaces = 4 - numberSpaces.toString().length(); + for (int j = 0; j < whiteSpaces; j++) { + result += " "; + } + result += numberSpaces.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 index 112bc16..9302d1b 100644 --- a/src/test/java/DiceTest.java +++ b/src/test/java/DiceTest.java @@ -1,7 +1,5 @@ import org.junit.Test; -import java.lang.reflect.Array; -import java.util.ArrayList; import java.util.Arrays; public class DiceTest { @@ -20,9 +18,11 @@ 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)); 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); + + + + } + +} From 43fc49100318609cb5d90f610dfe9e288e712758 Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 9 Jul 2021 23:47:27 -0400 Subject: [PATCH 3/5] Final touch ups --- src/main/java/Simulation.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 3b7b094..959d7b4 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -7,7 +7,6 @@ public class Simulation { private Integer numOfToss; private Integer numOfDice; private Integer min; - private Integer max; public Simulation(Integer numOfDice, Integer numOfToss){ this.numOfToss = numOfToss; @@ -24,13 +23,15 @@ public void runSim(){ } 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(i) + " :" + getBoxValue(bin.getBin(i + min)) + " : " + bin.getPercent(i + min) + + String addToResult = getBoxNumber(i) + " :" + getBoxValue(bin.getBin(i + min)) + ": " + bin.getPercent(i + min) + " " + getStars(bin.getPercent(i + min)) + "\n"; result += addToResult; } @@ -40,7 +41,7 @@ public void printResult(){ private String getBoxNumber(Integer i){ String result = ""; //3 spaces Integer numberSpaces = i + min; - Integer whiteSpaces = 4 - numberSpaces.toString().length(); + Integer whiteSpaces = 3 - numberSpaces.toString().length(); for (int j = 0; j < whiteSpaces; j++) { result += " "; } From 81da34495279899f03b6684571137fcece2b0b3d Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 10 Jul 2021 00:03:40 -0400 Subject: [PATCH 4/5] Added Nathan md file --- NathanResults.md | 19 +++++++++++++++++++ src/main/java/Main.java | 1 + 2 files changed, 20 insertions(+) create mode 100644 NathanResults.md 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/src/main/java/Main.java b/src/main/java/Main.java index 4c315a8..99902e2 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,5 +4,6 @@ public static void main(String[] args) { Simulation newSim = new Simulation(2, 1000000); newSim.runSim(); newSim.printResult(); + } } From 353fc32e884ee10c72da5f763719438686b35838 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 10 Jul 2021 10:23:18 -0400 Subject: [PATCH 5/5] Final edits --- src/main/java/Simulation.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 959d7b4..4c99d25 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -31,21 +31,20 @@ public void printResult(){ Integer[] thisBin = bin.getBoxes(); for(int i = 0; i < thisBin.length; i++) { int boxId = i + min; - String addToResult = getBoxNumber(i) + " :" + getBoxValue(bin.getBin(i + min)) + ": " + bin.getPercent(i + min) + - " " + getStars(bin.getPercent(i + min)) + "\n"; + 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 i){ + private String getBoxNumber(Integer boxID){ String result = ""; //3 spaces - Integer numberSpaces = i + min; - Integer whiteSpaces = 3 - numberSpaces.toString().length(); + Integer whiteSpaces = 3 - boxID.toString().length(); for (int j = 0; j < whiteSpaces; j++) { result += " "; } - result += numberSpaces.toString(); + result += boxID.toString(); return result; }