From 31b4d0f700a8016a8e2df6091407e3349500ad27 Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 4 Aug 2021 11:04:35 -0400 Subject: [PATCH] sorry i took so long --- pom.xml | 8 ++++++ src/main/java/Bins.java | 27 ++++++++++++++++++++ src/main/java/Dice.java | 30 +++++++++++++++++++++- src/main/java/Simulation.java | 47 ++++++++++++++++++++++++++++++++++- src/test/java/BinsTest.java | 31 +++++++++++++++++++++++ src/test/java/DiceTest.java | 22 ++++++++++++++++ 6 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 src/test/java/BinsTest.java create mode 100644 src/test/java/DiceTest.java diff --git a/pom.xml b/pom.xml index 7219542..ae2a523 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,14 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..644f0a7 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,31 @@ +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; public class Bins { + Integer[] bins; + Integer min; + Integer max; + + public Bins(Integer min, Integer max) { + bins = new Integer[max - min + 1]; + this.min = min; + this.max = max; + Arrays.fill(bins, 0); + } + + // looking for one index array [6] + public Integer getBinValue(Integer diceRoll) { + + return bins[diceRoll - min]; + } + + public Integer[] getBins() { + return bins; + } + + public Integer incrementBins(Integer diceRoll) { + return bins[diceRoll - min] ++; + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..f3b1813 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,32 @@ public class Dice { + private int numberOfDie; -} + public Dice(int numberOfDice) { + numberOfDie = numberOfDice; + } + + public int tossAndSum() { + int sumOfDice = 0; + for (int i = 0; i < numberOfDie; i++) { + sumOfDice += rollDie(); + } + + return sumOfDice; + } + + public int rollDie() { + + return (int) (Math.random() * 6 + 1); + } + + public int getNumberOfDie() { + return numberOfDie; + } + + public void setNumberOfDie(int numberOfDie) { + this.numberOfDie = numberOfDie; + } + + +} \ No newline at end of file diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..f119154 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,50 @@ public class Simulation { + Integer numberOfDice; + Integer numberOfTosses; + Dice dice; + Bins bins; + public Simulation(Integer numberOfDice, Integer numberOfTosses) { + this.numberOfDice = numberOfDice; + this.numberOfTosses = numberOfTosses; + } -} + public void runSimulation() { + this.dice = new Dice(2); + this.bins = new Bins(2, 12); + + for (int i = 0; i < numberOfTosses; i++) { + Integer sumUp = dice.tossAndSum(); + bins.incrementBins(sumUp); + } + System.out.println(""); + } + + public void printResults() { + for(Integer i = 2; i <= 12; i++) { + Double percentage = (double) bins.getBinValue(i) / numberOfTosses; + System.out.printf("%2d : %7d: %.2f ", i, bins.getBinValue(i), percentage); + for(int j = 1; j < percentage * 100; j++) + { + System.out.print("*"); + } + System.out.println(); + } + } + + + public double getPercentage(Integer numberOfTosses, Bins bins) { + double percentage = 0; + for( int element : bins.getBins()) { + percentage = element/ numberOfTosses; + } + return percentage; + } + + public static void main(String[] args) { + Simulation simulation = new Simulation(2, 1000000); + simulation.runSimulation(); + simulation.printResults(); + } +} \ No newline at end of file diff --git a/src/test/java/BinsTest.java b/src/test/java/BinsTest.java new file mode 100644 index 0000000..8770efb --- /dev/null +++ b/src/test/java/BinsTest.java @@ -0,0 +1,31 @@ +import org.junit.Test; + +public class BinsTest { + @Test + public void numberOfEachNumberTest(){ + // given + + // when + + //then + + } + @Test + public void binIncrementTest(){ + // given + + // when + + // then + + } + @Test + public void percentageOfRollTest(){ + // given + + // when + + // then + + } +} diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..20b9d4d --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,22 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class DiceTest { + @Test + public void tossAndSumTest(){ + // given + Dice dice = new Dice(2); + + // when + dice.tossAndSum(); + + // then + + } + + } + +