#28 Curtis Cook

Open
CurtisMCook wants to merge 1 commits from CurtisMCook/ZCW-DiceyLab:master into master

+ 11
- 11
README.md View File

@@ -1,23 +1,23 @@
1
-# Kris-Tof Dice Toss
1
+# Kris-Tof main.Dice Toss
2 2
 
3
-Create a `Dice` class that acts like a set of N random-tossed dies.
3
+Create a `main.Dice` class that acts like a set of N random-tossed dies.
4 4
 ```
5
-Dice dice = new Dice(2); // for craps
6
-Dice dice = new Dice(5); // for yatzee
5
+main.Dice dice = new main.Dice(2); // for craps
6
+main.Dice dice = new main.Dice(5); // for yatzee
7 7
 
8 8
 Integer toss = dice.tossAndSum();
9 9
 ```
10
-make a couple unit tests for the Dice class. 
10
+make a couple unit tests for the main.Dice class. 
11 11
 
12
-Create a tracking class `Bins` that can be used to track the results of dice tosses.
12
+Create a tracking class `main.Bins` that can be used to track the results of dice tosses.
13 13
 
14 14
 ```
15
-Bins results = new Bins(2, 12); // for bins from 2..12
15
+main.Bins results = new main.Bins(2, 12); // for bins from 2..12
16 16
 Integer numberOfTens = results.getBin(10); // returns the number of tens in the 10 bin
17 17
 results.incrementBin(10); // should increment bin # 10
18 18
 
19 19
 ```
20
-make a couple unit tests for the Bins class. Your test methods should follow this template:
20
+make a couple unit tests for the main.Bins class. Your test methods should follow this template:
21 21
 
22 22
     in the tests
23 23
     Create a new Bin
@@ -27,7 +27,7 @@ make a couple unit tests for the Bins class. Your test methods should follow thi
27 27
     
28 28
     
29 29
 
30
-Create a `Simulation` class whose Constructor takes arguments:
30
+Create a `main.Simulation` class whose Constructor takes arguments:
31 31
     numberOfDies to throw
32 32
     numberOfTosses to run
33 33
 
@@ -35,7 +35,7 @@ Create a simulation where two dies are thrown *one million times*.
35 35
 Keep track of each roll by incrementing the correct Bin.
36 36
 
37 37
 ```
38
-Simulation sim = new Simulation(2, 10000);
38
+main.Simulation sim = new main.Simulation(2, 10000);
39 39
 
40 40
 sim.runSimulation();
41 41
 
@@ -53,7 +53,7 @@ the results of the sim.printResults() should be an output string that looks like
53 53
 
54 54
 ```
55 55
 ***
56
-Simulation of 2 dice tossed for 1000000 times.
56
+main.Simulation of 2 dice tossed for 1000000 times.
57 57
 ***
58 58
 
59 59
  2 :    27917: 0.03 **

+ 16
- 0
pom.xml View File

@@ -8,5 +8,21 @@
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
12
+        <dependency>
13
+            <groupId>org.junit.jupiter</groupId>
14
+            <artifactId>junit-jupiter-api</artifactId>
15
+            <version>RELEASE</version>
16
+            <scope>test</scope>
17
+        </dependency>
18
+
19
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
20
+        <dependency>
21
+            <groupId>junit</groupId>
22
+            <artifactId>junit</artifactId>
23
+            <version>4.12</version>
24
+            <scope>test</scope>
25
+        </dependency>
26
+    </dependencies>
11 27
 
12 28
 </project>

+ 0
- 4
src/main/java/Bins.java View File

@@ -1,4 +0,0 @@
1
-
2
-public class Bins {
3
-
4
-}

+ 0
- 4
src/main/java/Dice.java View File

@@ -1,4 +0,0 @@
1
-public class Dice {
2
-
3
-
4
-}

+ 0
- 5
src/main/java/Simulation.java View File

@@ -1,5 +0,0 @@
1
-public class Simulation {
2
-
3
-
4
-
5
-}

+ 29
- 0
src/main/java/main/Bins.java View File

@@ -0,0 +1,29 @@
1
+package main;
2
+
3
+import java.util.TreeMap;
4
+
5
+public class Bins {
6
+    private int min;
7
+    private int max;
8
+    private TreeMap<Integer, Integer> results;
9
+
10
+    public Bins(int min, int max) {
11
+        this.min = min;
12
+        this.max = max;
13
+        results = new TreeMap<Integer, Integer>();
14
+        for(int i = min; i <= max; i++) {
15
+            results.put(i, 0);
16
+        }
17
+    }
18
+
19
+    public int getBin(int number) {
20
+        return results.get(number);
21
+    }
22
+
23
+    public void incrementBin(int number) {
24
+        int value = results.get(number);
25
+        value++;
26
+        results.put(number, value);
27
+
28
+    }
29
+}

+ 17
- 0
src/main/java/main/Dice.java View File

@@ -0,0 +1,17 @@
1
+package main;
2
+
3
+public class Dice {
4
+    private int numberOfDie;
5
+
6
+    public Dice(int numberOfDie) {
7
+        this.numberOfDie = numberOfDie;
8
+    }
9
+
10
+    public int tossAndSum() {
11
+        int sum = 0;
12
+        for(int i = numberOfDie; i > 0; i--) {
13
+            sum += (int) (Math.random() * 6 + 1);
14
+        }
15
+        return sum;
16
+    }
17
+}

+ 12
- 0
src/main/java/main/Main.java View File

@@ -0,0 +1,12 @@
1
+package main;
2
+
3
+public class Main {
4
+
5
+    public static void main(String[] args) {
6
+
7
+        Simulation sim = new Simulation(2, 1000000);
8
+        sim.runSimulation();
9
+        sim.printResults();
10
+
11
+    }
12
+}

+ 65
- 0
src/main/java/main/Simulation.java View File

@@ -0,0 +1,65 @@
1
+package main;
2
+
3
+public class Simulation {
4
+    private int numberOfDies;
5
+    private int numberOfTosses;
6
+    private Bins result;
7
+    private Dice dice;
8
+
9
+    public Simulation(int numberOfDies, int numberOfTosses) {
10
+        this.numberOfDies = numberOfDies;
11
+        this.numberOfTosses = numberOfTosses;
12
+        this.result = new Bins(numberOfDies, numberOfDies * 6);
13
+        this.dice = new Dice(numberOfDies);
14
+    }
15
+
16
+    public void runSimulation() {
17
+        for(int i = 0; i < numberOfTosses; i++) {
18
+            int sum = dice.tossAndSum();
19
+            result.incrementBin(sum);
20
+        }
21
+    }
22
+
23
+    public void printResults() {
24
+        System.out.println("***");
25
+        System.out.println("Simulation " + numberOfDies + " dice tossed for " + numberOfTosses + " times.");
26
+        System.out.println("***");
27
+        System.out.println();
28
+
29
+        for(int i = numberOfDies; i <= numberOfDies * 6; i++) {
30
+
31
+            double average = (double) result.getBin(i)/numberOfTosses;
32
+            String stars = "";
33
+            if (average >= 0 && average < 0.025) {
34
+                stars = "*";
35
+            }
36
+
37
+            if(average  > 0.025 && average < 0.05) {
38
+                stars = "**";
39
+            }
40
+
41
+            if(average > 0.050 && average < 0.075) {
42
+                stars = "*****";
43
+            }
44
+
45
+            if(average > 0.075 && average < 0.10) {
46
+                stars = "********";
47
+            }
48
+
49
+            if(average > 0.10 && average < 0.125) {
50
+                stars = "***********";
51
+            }
52
+
53
+            if(average > 0.125 && average < 0.15) {
54
+                stars = "*************";
55
+            }
56
+
57
+            if(average > 0.15 && average < 0.175) {
58
+                stars = "****************";
59
+            }
60
+
61
+            System.out.println(String.format("%2d :",i) + String.format("%10d:  ", result.getBin(i)) +
62
+                    String.format("%.2f  ",(double) result.getBin(i)/numberOfTosses) + stars);
63
+        }
64
+    }
65
+}