From 93e9fa50985a540c0db847093d220ccc1824dd94 Mon Sep 17 00:00:00 2001 From: johnS93 Date: Wed, 4 Aug 2021 10:12:40 -0400 Subject: [PATCH] Done this lab --- pom.xml | 12 +++++++++ src/main/java/Bins.java | 22 ++++++++++++++++ src/main/java/Dice.java | 27 ++++++++++++++++++++ src/main/java/Simulation.java | 47 +++++++++++++++++++++++++++++++++++ src/test/java/DiceTest.java | 2 ++ 5 files changed, 110 insertions(+) create mode 100644 src/test/java/DiceTest.java diff --git a/pom.xml b/pom.xml index 7219542..3ad8b09 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 14 + 14 + + + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..ed18226 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,26 @@ +import java.util.Arrays; 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); + } + + 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..73ceac6 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,31 @@ +import java.util.Random; + public class Dice { + int numberOfDice; + + + public Dice(int numberOfDice) { + this.numberOfDice = numberOfDice; + } + + public int tossAndSum() { + int sumOfDice = 0; + for (int i = 0; i < numberOfDice; i++) { + sumOfDice += rollDie(); + } + return sumOfDice; + } + + private int rollDie() { + return (int) (Math.random() * 6 + 1); + } + + public int getNumberOfDice(){ + return numberOfDice; + } + public void setNumberOfDice(int numberOfDice) { + this.numberOfDice = numberOfDice; + } } diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..fb58116 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,52 @@ 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(); + } } diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..98ff4ec --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,2 @@ +public class DiceTest { +}