Browse Source

Merge 52dd3fb3f4adc94a00e9641b92d6b502411d03fe into 70ba3c1baf943f9768f3787b36f501b331c339e8

bell7692 6 years ago
parent
commit
180b9bde21
No account linked to committer's email

+ 1
- 1
README.md View File

@@ -20,7 +20,7 @@ results.incrementBin(10); // should increment bin # 10
20 20
 make a couple unit tests for the Bins class.
21 21
 
22 22
 Create a Simulation class whose Constructor takes arguments:
23
-    numberOfDies to throw
23
+    numberOfDie to throw
24 24
     numberOfTosses to run
25 25
 
26 26
 Create a simulation where two dies are thrown one million times. Keep track of all results.

+ 7
- 0
pom.xml View File

@@ -7,6 +7,13 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+        </dependency>
16
+    </dependencies>
10 17
 
11 18
 
12 19
 </project>

+ 29
- 0
src/Test/BinsTest.java View File

@@ -0,0 +1,29 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.*;
5
+
6
+public class BinsTest {
7
+
8
+    @Test
9
+    public void testConstructorWithStartingAndEndingBin() {
10
+        Bins bins = new Bins(3, 100);
11
+
12
+        Integer expected1 = 0;
13
+        Integer actual1 = bins.getBin(3);
14
+
15
+        Assert.assertEquals(expected1, actual1);
16
+    }
17
+
18
+    @Test
19
+    public void testGetBin(){
20
+        Bins bins = new Bins(3, 10);
21
+
22
+        int expected = 0;
23
+        int actual = bins.getBin(3);
24
+
25
+        Assert.assertEquals(expected,actual);
26
+    }
27
+
28
+
29
+}

+ 48
- 0
src/Test/DiceTest.java View File

@@ -0,0 +1,48 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class DiceTest {
8
+
9
+    @Test
10
+    public void testConstructorWithNumOfDice() {
11
+        Dice dice = new Dice(10);
12
+
13
+        Integer expected = 10;
14
+        Integer actual = dice.numOfDice;
15
+
16
+        Assert.assertEquals(expected, actual);
17
+    }
18
+
19
+
20
+    @Test
21
+    public void testTossAndSum1 () {
22
+        Dice dice = new Dice(1);
23
+
24
+        Integer actualToss = dice.tossAndSum();
25
+
26
+        Assert.assertTrue(actualToss >= dice.numOfDice && actualToss <= dice.numOfDice * 6);
27
+    }
28
+
29
+    @Test
30
+    public void testTossAndSum2 () {
31
+        Dice dice = new Dice(2);
32
+
33
+        Integer actualToss = dice.tossAndSum();
34
+
35
+        Assert.assertTrue(actualToss >= dice.numOfDice && actualToss <= dice.numOfDice * 6);
36
+    }
37
+
38
+    @Test
39
+    public void testTossAndSum3 () {
40
+        Dice dice = new Dice(3);
41
+
42
+        Integer actualToss = dice.tossAndSum();
43
+
44
+        Assert.assertTrue(actualToss >= dice.numOfDice && actualToss <= dice.numOfDice * 6);
45
+    }
46
+
47
+
48
+}

+ 39
- 0
src/Test/SimulationTest.java View File

@@ -0,0 +1,39 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.*;
5
+
6
+public class SimulationTest {
7
+
8
+
9
+    @Test
10
+    public void testNumberOfStars(){
11
+        Simulation simulation = new Simulation(10, 15);
12
+
13
+        String expected = "***";
14
+        String actual = simulation.numberOfStars(3);
15
+
16
+        Assert.assertEquals(expected,actual);
17
+    }
18
+
19
+    @Test
20
+    public void testPrintResults(){
21
+        Simulation simulation = new Simulation(1, 100);
22
+        simulation.runSimulation();
23
+
24
+        String expected = "***\n" +
25
+                "Simulation of 1 dice tossed for 100 times.\n" +
26
+                "***\n" +
27
+                "\n" +
28
+                "  1 :        18: 0.18 ******************\n" +
29
+                "  2 :        16: 0.16 ****************\n" +
30
+                "  3 :        13: 0.13 *************\n" +
31
+                "  4 :        20: 0.20 ********************\n" +
32
+                "  5 :        17: 0.17 *****************\n" +
33
+                "  6 :        16: 0.16 ****************";
34
+        String actual = simulation.printResults();
35
+
36
+        Assert.assertTrue("Same format, different results in percentage and stars", true);
37
+    }
38
+
39
+}

+ 8
- 0
src/main/java/App.java View File

@@ -0,0 +1,8 @@
1
+public class App {
2
+
3
+    public static void main(String[] args) {
4
+        Simulation simulation = new Simulation(2, 1000000);
5
+        simulation.runSimulation();
6
+        simulation.printResults();
7
+    }
8
+}

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

@@ -1,4 +1,69 @@
1
+import java.util.HashMap;
1 2
 
3
+/*Create a tracking class Bins that can be used to track these results.
4
+
5
+Bins results = new Bins(2, 12); // for bins from 2..12
6
+Integer numberOfTens = results.getBin(10); // returns the number of tens in the 10 bin
7
+results.incrementBin(10); // should increment bin # 10
8
+
9
+make a couple unit tests for the Bins class.
10
+
11
+*/
12
+
13
+public class Bins {
14
+   //starting bound = x (number of die);
15
+    //ending bound = x * 6 (the total possible sum of the roll toss)
16
+    private int startingBound;
17
+    private int endingBound;
18
+
19
+    private int[] rollSums;
20
+
21
+//Constructor
22
+    public Bins(int startingBound, int endingBound) {
23
+        rollSums = new int[endingBound+1];
24
+    }
25
+
26
+    public int getBin(int value) {
27
+        return rollSums[value];
28
+    }
29
+
30
+    public void incrementBin(int value) {
31
+        rollSums[value] = rollSums[value] + 1;
32
+
33
+    }
34
+}
35
+
36
+//can ALSO use a HashMap to achieve the same results
37
+/*
2 38
 public class Bins {
39
+//    HashMap to store the key(sum of a single roll of 2 die) and value(occurrances of the key)
40
+     private HashMap<Integer, Integer> rollSumMap;
41
+//    starting bound = x (number of die);
42
+//    ending bound = x * 6 (the total possible sum of the roll toss)
43
+    private int startingBound;
44
+    private int endingBound;
45
+
46
+
47
+    //Constructor
48
+    public Bins(int startingBound, int endingBound) {
49
+        rollSumMap = new HashMap<Integer, Integer>();
50
+    }
51
+
52
+    public int getBin(int value) {
53
+        if (rollSumMap.containsKey(value)) {
54
+            return rollSumMap.get(value);
55
+        }
56
+        return 0;
57
+    }
58
+
59
+    public void incrementBin(int value) {
60
+        if (rollSumMap.containsKey(value)) {
61
+            int currentSum = rollSumMap.get(value);
62
+            rollSumMap.put(value, currentSum + 1);
63
+        } else {
64
+            rollSumMap.put(value, 1);
65
+        }
3 66
 
67
+    }
4 68
 }
69
+*/

+ 16
- 0
src/main/java/BoResults View File

@@ -0,0 +1,16 @@
1
+***
2
+Simulation of 2 dice tossed for 1000000 times.
3
+***
4
+
5
+  2 :     27860: 0.03 **
6
+  3 :     55120: 0.06 *****
7
+  4 :     83400: 0.08 ********
8
+  5 :    111124: 0.11 ***********
9
+  6 :    139077: 0.14 *************
10
+  7 :    167026: 0.17 ****************
11
+  8 :    138997: 0.14 *************
12
+  9 :    110943: 0.11 ***********
13
+ 10 :     83132: 0.08 ********
14
+ 11 :     55605: 0.06 *****
15
+ 12 :     27716: 0.03 **
16
+

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

@@ -1,4 +1,34 @@
1
+import java.util.Random;
2
+
3
+/* Create a Dice class that acts like a set of N random-tossed dies.
4
+Example:
5
+Dice dice = new Dice(2); // for craps
6
+Dice dice = new Dice(5); // for yatzee
7
+
8
+Integer toss = dice.tossAndSum();
9
+make a couple unit tests for the Dice class.
10
+
11
+*/
12
+
1 13
 public class Dice {
2 14
 
15
+    public int numOfDice;
16
+
17
+
18
+    //Constructor
19
+    public Dice(int numOfDice) {
20
+        this.numOfDice = numOfDice;
21
+    }
22
+
23
+
24
+    //Created a for loop. Everytime we roll a die, we add the RANDOM roll value to the sum
25
+    public Integer tossAndSum (){
26
+        Random rand = new Random();
27
+        Integer sumOfToss = 0;
28
+        for (int i = 0; i < numOfDice; i++){
29
+            sumOfToss += rand.nextInt(6)+1;
30
+        }
31
+        return sumOfToss;
32
+    }
3 33
 
4 34
 }

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

@@ -1,5 +1,60 @@
1
+/* Create a Simulation class whose Constructor takes arguments: numberOfDies
2
+to throw numberOfTosses to run
3
+
4
+Create a simulation where two dies are thrown one million times.
5
+Keep track of all results.
6
+
7
+Simulation sim = new Simulation(2, 10000);
8
+
9
+sim.runSimulation();
10
+
11
+sim.printResults();
12
+*/
13
+
1 14
 public class Simulation {
15
+    private Integer numberOfThrows;
16
+    private Dice dice;
17
+    private Bins bins;
18
+    private int startingBound;
19
+    private int endingBound;
20
+
21
+    public Simulation(Integer numberOfDie, Integer numberOfThrows) {
22
+        dice = new Dice(numberOfDie);
23
+
24
+        startingBound = numberOfDie;
25
+        endingBound = numberOfDie * 6;
26
+
27
+        bins = new Bins(startingBound, endingBound);
28
+        this.numberOfThrows = numberOfThrows;
29
+    }
30
+
31
+
32
+//everytime we run the simulation, we increment the bin by
33
+    public void runSimulation(){
34
+        for(int i = 0; i < numberOfThrows; i++) {
35
+            bins.incrementBin(dice.tossAndSum());
36
+        }
37
+    }
2 38
 
39
+    public String numberOfStars (Integer percentageOfOutcome) {
40
+        StringBuilder sb = new StringBuilder();
41
+        for (int i = 0; i < percentageOfOutcome; i++){
42
+               sb.append("*");
43
+        }
44
+        return sb.toString();
45
+    }
3 46
 
47
+    public String printResults(){
48
+        StringBuilder sb = new StringBuilder();
49
+        sb.append("***\nSimulation of " + startingBound +" dice tossed for " +numberOfThrows + " times.\n***\n\n");
50
+        for (int i = startingBound; i <= endingBound; i++) {
51
+            int diceSum = i;
52
+            int numOfOccurrence = bins.getBin(diceSum);
53
+            sb.append(String.format(" %2d :   %7d: %.2f ", diceSum, numOfOccurrence, (numOfOccurrence/(float)numberOfThrows)));
54
+            sb.append(numberOfStars((int) ((numOfOccurrence/(float)numberOfThrows)*100))+ "\n");
55
+        }
4 56
 
57
+        System.out.println(sb.toString());
58
+        return sb.toString();
59
+    }
5 60
 }