From 164bcfd7e65bf0ecae9577807512bf20fc74b050 Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Fri, 19 Nov 2021 16:30:14 -0500 Subject: [PATCH] completed lab --- pom.xml | 32 ++++++++++++++++++++++++++ src/main/java/Bins.java | 25 ++++++++++++++++++++ src/main/java/BinsTest.java | 32 ++++++++++++++++++++++++++ src/main/java/Dice.java | 18 +++++++++++++++ src/main/java/DiceTest.java | 16 +++++++++++++ src/main/java/Simulation.java | 43 +++++++++++++++++++++++++++++++++++ 6 files changed, 166 insertions(+) create mode 100644 src/main/java/BinsTest.java create mode 100644 src/main/java/DiceTest.java diff --git a/pom.xml b/pom.xml index 7219542..8db8284 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,38 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + junit + junit + 4.13.1 + compile + + + org.junit.jupiter + junit-jupiter-api + 5.8.1 + compile + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..40b7158 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,29 @@ +import java.util.HashMap; +import java.util.Map; public class Bins { + Map mapBins = new HashMap<>(); + private Integer minimumNumber; + private Integer maximumNumber; + + public Bins(int minimumNumber, int maximumNumber){ + this.minimumNumber = minimumNumber; + this.maximumNumber= maximumNumber; + for (int i = minimumNumber; i <= maximumNumber ; i++) { + mapBins.put(i,0); + + } + } + + public Integer getBin(int binNumber){ + return mapBins.get(binNumber); + } + + public void incrementBin(Integer binNumber){ + if(binNumber >=this.minimumNumber && binNumber <= this.maximumNumber){ + Integer currentResult = this.getBin(binNumber); + this.mapBins.put(binNumber , currentResult + 1); + } + } } diff --git a/src/main/java/BinsTest.java b/src/main/java/BinsTest.java new file mode 100644 index 0000000..4261da6 --- /dev/null +++ b/src/main/java/BinsTest.java @@ -0,0 +1,32 @@ + + +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import static org.junit.jupiter.api.Assertions.*; + +public class BinsTest { + +@Test + public void getBins(){ + //given + Bins bins = new Bins(2,12); + + //when + int actual = bins.getBin(4); + + //Then + Assertions.assertEquals(0,actual); +} + +@Test + public void incrementTest(){ + //given + Bins bins = new Bins(2,12); + int expected = 2; + bins.incrementBin(10); + bins.incrementBin(10); + assertEquals(expected, bins.getBin(10)); +} + +} \ No newline at end of file diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..4d767ae 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,22 @@ + + public class Dice { + private Integer numberOnDice; + + public Dice(Integer numberOfDice){ + this.numberOnDice = numberOfDice; + } + + public int tossAndAdd(){ + int sum = 0; + for (int i = 0; i < numberOnDice ; i++) { + sum+= (int) (Math.random() * 6 ) + 1; + } + return sum; + } + public Integer getNumberOfDice() { + return numberOnDice; + } } diff --git a/src/main/java/DiceTest.java b/src/main/java/DiceTest.java new file mode 100644 index 0000000..1baa411 --- /dev/null +++ b/src/main/java/DiceTest.java @@ -0,0 +1,16 @@ +import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.*; + + + +public class DiceTest { + @Test + public void testDiceTossAndAdd(){ + int numberOfDice = 2; + Dice dice = new Dice(numberOfDice); + int actual = dice.tossAndAdd(); + assertTrue(actual >= 1 && actual <= 13); + } + +} \ No newline at end of file diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..18d1bdf 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,48 @@ + + + public class Simulation { + private Integer numberOfDice; + private Integer numberOfTosses; + + public Simulation(Integer numberOfDice , Integer numberOfTosses){ + this.numberOfDice = numberOfDice; + this.numberOfTosses = numberOfTosses; + } + + public static void main(String[] args){ + Simulation simulation = new Simulation(2,10000); + simulation.printResults(simulation.runSimulation()); + } + + private Bins runSimulation(){ + Dice dice = new Dice(numberOfDice); + Integer maxValue = numberOfDice * 6; + Bins bins = new Bins(numberOfDice, maxValue); + for (int i = 0; i < numberOfTosses ; i++) { + bins.incrementBin(dice.tossAndAdd()); + + } + return bins; + } + private void printResults(Bins bins){ + Integer maxValue = numberOfDice * 6; + for (int i = numberOfDice; i < maxValue ; i++) { + System.out.println(String.format("%2d : %4d : %1.2f ",i, bins.getBin(i),binPercent(bins.getBin(i)))); + printHash(binPercent(bins.getBin(i))); + System.out.println(); + } + } + public void printHash(Double Value){ + int numberOfHash = (int)(Value * 100); + for (int i = 1; i <= numberOfHash ; i++) { + System.out.print(" # "); + } + } + public Double binPercent(Integer Value){ + return Double.valueOf(Value) /numberOfTosses; + } }