#32 WJB3003 William Brown's Dice

Open
WJB3003 wants to merge 2 commits from WJB3003/ZCW-DiceyLab:master into master

+ 8
- 0
pom.xml View File

@@ -7,6 +7,14 @@
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
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

+ 6
- 2
src/main/java/Bins.java View File

@@ -1,4 +1,8 @@
1
-
2 1
 public class Bins {
3 2
 
4
-}
3
+    public int[] addToBinArray(int sumOfDice, int[] array, int numberOfDice) {
4
+        array[sumOfDice - numberOfDice] = array[sumOfDice - numberOfDice] + 1;
5
+        return array;
6
+    }
7
+
8
+}

+ 16
- 1
src/main/java/Dice.java View File

@@ -1,4 +1,19 @@
1 1
 public class Dice {
2 2
 
3
+    public Dice() {
4
+    }
3 5
 
4
-}
6
+    public int roll() {
7
+        int dice = (int) (Math.random() * 6) + 1;
8
+        return dice;
9
+    }
10
+
11
+    public int tossAndSum(int numberOfDice) {
12
+        int sum = 0;
13
+        for (int i = 0; i < numberOfDice; i++) {
14
+            sum = sum + roll();
15
+        }
16
+        return sum;
17
+    }
18
+
19
+}

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

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

+ 70
- 1
src/main/java/Simulation.java View File

@@ -1,5 +1,74 @@
1 1
 public class Simulation {
2 2
 
3
+    public int numberOfRolls;
4
+    public int numberOfDice;
5
+    public int[] array;
6
+    public double[] probabilityArray;
7
+    public int[] numberOfStars;
8
+    public String[] printStars;
3 9
 
10
+    Bins bins = new Bins();
4 11
 
5
-}
12
+    public Simulation(int numberOfDice, int numberOfRolls) {
13
+        this.numberOfRolls = numberOfRolls;
14
+        this.numberOfDice = numberOfDice;
15
+        this.array = new int[(this.numberOfDice * 6) - numberOfDice + 1];
16
+        this.probabilityArray = new double[array.length];
17
+        this.numberOfStars = new int[array.length];
18
+        this.printStars = new String[array.length];
19
+    }
20
+
21
+    public void runSimulation(){
22
+        sim();
23
+        probability();
24
+        getNumberOfStars();
25
+        stringStars();
26
+        printResults();
27
+    }
28
+
29
+//    public int[] addToBinArray(int sumOfDice) {
30
+//        array[sumOfDice - numberOfDice] = array[sumOfDice - numberOfDice] + 1;
31
+//        return array;
32
+//    }
33
+
34
+    public void sim(){
35
+        Dice dice = new Dice();
36
+        for(int i = 0; i < numberOfRolls; i++){
37
+            bins.addToBinArray(dice.tossAndSum(numberOfDice), array, numberOfDice);
38
+        }
39
+    }
40
+
41
+    public void probability(){
42
+        for(int i = 0; i < array.length; i++){
43
+            probabilityArray[i] = (double)array[i] / numberOfRolls;
44
+        }
45
+    }
46
+
47
+    public void getNumberOfStars(){
48
+        for(int i = 0; i < array.length; i++){
49
+            numberOfStars[i] = (int)(probabilityArray[i] * 100);
50
+        }
51
+    }
52
+
53
+    public void stringStars(){
54
+        for(int j = 0; j < numberOfStars.length; j++) {
55
+            printStars[j] = "";
56
+            for (int i = 0; i < numberOfStars[j]; i++) {
57
+                printStars[j] = printStars[j] + "*";
58
+            }
59
+        }
60
+    }
61
+
62
+    public void printResults() {
63
+        System.out.println("***");
64
+        System.out.println("Simulation of " + numberOfDice + " dice tossed for " + numberOfRolls + " times.");
65
+        System.out.println("***");
66
+        System.out.print("\n");
67
+        for (int i = 0; i < array.length; i++) {
68
+            //System.out.println(array[i]);
69
+            //System.out.println((i + 2) + " :" + array[i] + ": " + probabilityArray[i] + " " + printStars[i]);
70
+            System.out.printf("%2d :%9d: %.2f %s\n" ,(i + numberOfDice),array[i],probabilityArray[i],printStars[i]);
71
+            //System.out.printf(i + " " + numberOfStars);
72
+        }
73
+    }
74
+}

+ 64
- 0
src/test/test/SimulationTest.java View File

@@ -0,0 +1,64 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class SimulationTest {
5
+
6
+    @Test
7
+    public void testNumberOfDice(){
8
+        Simulation sim = new Simulation(2, 10000);
9
+        int expected = 2;
10
+        int actual = sim.numberOfDice;
11
+        Assert.assertEquals(expected,actual);
12
+    }
13
+
14
+    @Test
15
+    public void testNumberOfRolls(){
16
+        Simulation sim = new Simulation(2, 10000);
17
+        int expected = 10000;
18
+        int actual = sim.numberOfRolls;
19
+        Assert.assertEquals(expected,actual);
20
+    }
21
+
22
+    @Test
23
+    public void testArraySize(){
24
+        Simulation sim = new Simulation(2, 1000000);
25
+        int expected = 11;
26
+        int actual = sim.array.length;
27
+        Assert.assertEquals(expected,actual);
28
+    }
29
+
30
+    @Test
31
+    public void testProbabilitySize(){
32
+        Simulation sim = new Simulation(2, 1000000);
33
+        int expected = 11;
34
+        int actual = sim.probabilityArray.length;
35
+        Assert.assertEquals(expected,actual);
36
+    }
37
+
38
+    @Test
39
+    public void testStarSize(){
40
+        Simulation sim = new Simulation(2, 1000000);
41
+        int expected = 11;
42
+        int actual = sim.numberOfStars.length;
43
+        Assert.assertEquals(expected,actual);
44
+    }
45
+
46
+    @Test
47
+    public void testPrintStarsSize(){
48
+        Simulation sim = new Simulation(2, 1000000);
49
+        int expected = 11;
50
+        int actual = sim.printStars.length;
51
+        Assert.assertEquals(expected,actual);
52
+    }
53
+
54
+    @Test
55
+    public void testAddingToBin(){
56
+        Bins bins = new Bins();
57
+        int[] array = new int[1];
58
+        int expected = 1;
59
+        int[] binArray = bins.addToBinArray(2, array, 2);
60
+        int actual = binArray.length;
61
+        Assert.assertEquals(expected,actual);
62
+    }
63
+
64
+}