Soujanya Buragapu 6 gadus atpakaļ
vecāks
revīzija
ab3710ca9f

+ 8
- 0
pom.xml Parādīt failu

@@ -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>

+ 31
- 1
src/main/java/Bins.java Parādīt failu

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

+ 18
- 1
src/main/java/Dice.java Parādīt failu

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

+ 12
- 0
src/main/java/MainApplication.java Parādīt failu

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

+ 49
- 0
src/main/java/Simulation.java Parādīt failu

@@ -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/test/java/DiceTest.java Parādīt failu

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