Browse Source

Finally Done

NiraParikh 6 years ago
parent
commit
0bd39e96cd

BIN
.DS_Store View File


+ 14
- 0
pom.xml View File

@@ -7,6 +7,20 @@
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>compile</scope>
16
+        </dependency>
17
+        <dependency>
18
+            <groupId>junit</groupId>
19
+            <artifactId>junit</artifactId>
20
+            <version>RELEASE</version>
21
+            <scope>compile</scope>
22
+        </dependency>
23
+    </dependencies>
10 24
 
11 25
 
12 26
 </project>

BIN
src/.DS_Store View File


BIN
src/main/.DS_Store View File


BIN
src/main/java/.DS_Store View File


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

@@ -1,4 +1,32 @@
1 1
 
2
-public class Bins {
2
+    public class Bins
3
+    {
4
+        int[][] binArray;
5
+        int minRoll;
6
+        int maxRoll;
7
+        int binCount;
3 8
 
4
-}
9
+        Bins(int minRoll, int maxRoll) {
10
+            this.minRoll = minRoll;
11
+            this.maxRoll = maxRoll;
12
+
13
+            this.binCount = maxRoll - minRoll + 1;
14
+            binArray = new int[binCount][2];
15
+            int binNumber = minRoll;
16
+
17
+            for (int i = 0; i < binCount; i++) {
18
+                binArray[i][0] = binNumber;
19
+                binNumber++;
20
+            }
21
+        }
22
+
23
+        public int getBin(int currentRoll) {
24
+            return binArray[currentRoll-minRoll][1];
25
+        }
26
+
27
+        public void incrementBin (int currentRoll) {
28
+            int currentValue = getBin(currentRoll);
29
+            currentValue++;
30
+            binArray[currentRoll-minRoll][1] = currentValue;
31
+        }
32
+    }

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

@@ -1,4 +1,21 @@
1
+import java.util.Random;
2
+
1 3
 public class Dice {
4
+    public int diceCnt;
5
+
6
+    Dice(int diceCnt) {
7
+        this.diceCnt = diceCnt;
8
+    }
9
+
10
+    public int tossAndSum() {
11
+        Random random = new Random();
2 12
 
13
+        int rollSum = 0;
14
+        for (int i = 0; i < diceCnt; i++) {
15
+            int roll = random.nextInt(6)+1;
16
+            rollSum += roll;
17
+        }
18
+        return rollSum;
19
+    }
3 20
 
4
-}
21
+}

+ 10
- 0
src/main/java/Ma.java View File

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

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

@@ -1,5 +1,54 @@
1
+import java.util.Arrays;
2
+
1 3
 public class Simulation {
2 4
 
5
+    int diceCount;
6
+    int rollCount;
7
+
8
+    int minRoll;
9
+    int maxRoll;
10
+
11
+    Bins bins;
12
+    Dice dice;
13
+
14
+    public Simulation (int dieCount, int rollCount) {
15
+        this.diceCount = dieCount;
16
+        this.minRoll = dieCount;
17
+        this.maxRoll = dieCount*6;
18
+        this.rollCount = rollCount;
19
+        this.bins = new Bins(minRoll, maxRoll);
20
+        this.dice = new Dice(dieCount);
21
+    }
22
+
23
+    public void runSimulation()
24
+    {
25
+        for (int i = 0; i < rollCount; i++) {
26
+            int currentRoll = dice.tossAndSum();
27
+            bins.incrementBin(currentRoll);
28
+        }
29
+
30
+    }
31
+
32
+    public void printResults() {
33
+        StringBuilder builder = new StringBuilder("***\nSimulation of " + diceCount + " dice tossed " + rollCount + " times.\n***\n\n");
34
+
35
+
36
+
37
+        for (int i = 0; i < bins.binCount; i++)
38
+        {
39
+            int binNumber = i + minRoll;
40
+            int rollValue = bins.getBin(binNumber);
41
+            float rollStat = (float) rollValue / rollCount;
42
+            builder.append(String.format("%3d :" + "%8d:" + " %.2f ", binNumber, rollValue, rollStat));
43
+            int starCount = (int) (rollStat * 100);
44
+            for (int j = 0; j < starCount; j++)
45
+            {
46
+                builder.append("*");
47
+            }
48
+            builder.append("\n");
49
+        }
3 50
 
51
+        System.out.println(builder);
52
+    }
4 53
 
5
-}
54
+}

+ 110
- 0
src/main/java/TestDice.java View File

@@ -0,0 +1,110 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class TestDice {
5
+
6
+
7
+        @Test
8
+        public void tossAndSumTest0()
9
+        {
10
+            //Given
11
+            Dice dice = new Dice(2);
12
+            long actualNumber = dice.tossAndSum();
13
+            //When
14
+            boolean test2To12 = 1 < actualNumber && actualNumber < 13;
15
+            //Then
16
+            Assert.assertTrue(test2To12);
17
+            System.out.println(actualNumber);
18
+
19
+        }
20
+
21
+        @Test
22
+        public void tossAndSumTest1()
23
+        {
24
+            //Given
25
+            Dice dice = new Dice(10);
26
+            long actualNumber = dice.tossAndSum();
27
+            //When
28
+            boolean test10To60 = 9 < actualNumber && actualNumber < 61;
29
+            //Then
30
+            Assert.assertTrue(test10To60);
31
+        }
32
+
33
+        @Test
34
+        public void binArrayTest1()
35
+        {
36
+            //Given
37
+            Bins bins = new Bins(3,18);
38
+            //When
39
+            int expectedValue = (3);
40
+            //Then
41
+            Assert.assertEquals(expectedValue, bins.binArray[0][0]);
42
+        }
43
+
44
+        @Test
45
+        public void binArrayTest2()
46
+        {
47
+            //Given
48
+            int maxRoll = 12;
49
+            int minRoll = 2;
50
+            Bins bins = new Bins(minRoll,maxRoll);
51
+            //When
52
+            int expectedValue = maxRoll;
53
+            int result = maxRoll-minRoll;
54
+            //Then
55
+            Assert.assertEquals(expectedValue, bins.binArray[result][0]);
56
+        }
57
+
58
+        @Test
59
+        public void binArrayTest3()
60
+        {
61
+            //Given
62
+            int maxRoll = 6;
63
+            int minRoll = 1;
64
+            Bins bins = new Bins(minRoll,maxRoll);
65
+            //When
66
+            int expectedValue = maxRoll;
67
+            int result = bins.binArray.length-1;
68
+            //Then
69
+            Assert.assertEquals(expectedValue, bins.binArray[result][0]);
70
+        }
71
+
72
+        @Test
73
+        public void getBinTest1()
74
+        {
75
+            //Given
76
+            Bins bins = new Bins(3,18);
77
+            int expectedValue = (0);
78
+            //When
79
+            int actualValue = bins.getBin(3);
80
+            //Then
81
+            Assert.assertEquals(expectedValue, actualValue);
82
+        }
83
+
84
+        @Test
85
+        public void getBinTest2()
86
+        {
87
+            //Given
88
+            Bins bins = new Bins(2,12);
89
+            int expectedValue = (0);
90
+            //When
91
+            int actualValue = bins.getBin(12);
92
+            //Then
93
+            Assert.assertEquals(expectedValue, actualValue);
94
+        }
95
+
96
+        @Test
97
+        public void incrementBinTest1()
98
+        {
99
+            //Given
100
+            Bins bins = new Bins(3,18);
101
+            int expectedValue = (1);
102
+            //When
103
+            bins.incrementBin(3);
104
+            int actualValue = bins.getBin(3);
105
+            //Then
106
+            Assert.assertEquals(expectedValue, actualValue);
107
+        }
108
+
109
+    }
110
+