Selaa lähdekoodia

Merge 22d19a91f3e1635c7010e6841886af45bbbe07d1 into 1eaff706f0dcc7a467d1c6648bcb494e313ba6a5

Katrice 6 vuotta sitten
vanhempi
commit
8e03b67b4d
No account linked to committer's email

+ 7
- 2
pom.xml Näytä tiedosto

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

+ 28
- 0
src/main/java/Bins.java Näytä tiedosto

@@ -1,4 +1,32 @@
1
+import java.util.TreeMap;
1 2
 
2 3
 public class Bins {
4
+    //The treemap will hold the starting roll, and the ending roll
5
+    private TreeMap<Integer, Integer> sumOfRollsMap;
6
+    private Integer startingRoll;
7
+    private Integer endingRoll;
3 8
 
9
+    //the constructor
10
+    public Bins(Integer startingRoll, Integer endingRoll){
11
+        sumOfRollsMap = new TreeMap<Integer, Integer>();
12
+    }
13
+
14
+    //methods
15
+    public int getBin(Integer val){
16
+        if(sumOfRollsMap.containsKey(val)){
17
+            return sumOfRollsMap.get(val);
18
+        }
19
+        return 0;
20
+    }
21
+
22
+    public void addToBin(Integer val) {
23
+        if (sumOfRollsMap.containsKey(val)) {
24
+            int currentSum = sumOfRollsMap.get(val);
25
+            sumOfRollsMap.put(startingRoll, currentSum + 1);
26
+        } else {
27
+            sumOfRollsMap.put(val, 1);
28
+        }
29
+
30
+
31
+    }
4 32
 }

+ 37
- 0
src/main/java/BinsTest.java Näytä tiedosto

@@ -0,0 +1,37 @@
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 testConstructorStartRollandEndingRoll(){
10
+        //Given
11
+        Bins bins = new Bins(2, 12);
12
+        //When
13
+        Integer expected = 0;
14
+        Integer actual = bins.getBin(2);
15
+
16
+        Assert.assertEquals(expected, actual);
17
+
18
+    }
19
+
20
+    @Test
21
+    public void testGetBin(){
22
+        //Given
23
+        Bins bins = new Bins(2, 9);
24
+        //When
25
+        Integer expected = 0;
26
+        Integer actual = bins.getBin(2);
27
+        //Actual
28
+        Assert.assertEquals(expected, actual);
29
+    }
30
+
31
+    @Test
32
+    public void testAddToBin(){
33
+        //Given
34
+        Bins bins = new Bins(2, 10);
35
+    }
36
+
37
+}

+ 21
- 0
src/main/java/Dice.java Näytä tiedosto

@@ -1,4 +1,25 @@
1
+import java.util.Random;
2
+
1 3
 public class Dice {
4
+    public Integer numberOfDice;
5
+    public Integer dice;
6
+
7
+    //constructor, to refer to the #ofD has to use this.
8
+    public Dice(Integer numberOfDice){
9
+        this.numberOfDice = numberOfDice;
10
+    }
11
+
12
+    public Integer sumOfToss(){
13
+        Random random = new Random();
14
+
15
+        Integer sumOfRoll = 0;
16
+        for(Integer i =0; i<numberOfDice; i++){
17
+            sumOfRoll += random.nextInt(6)+1;
18
+
19
+        }
20
+
21
+        return sumOfRoll;
22
+    }
2 23
 
3 24
 
4 25
 }

+ 51
- 0
src/main/java/DiceTest.java Näytä tiedosto

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

+ 52
- 0
src/main/java/Simulation.java Näytä tiedosto

@@ -1,5 +1,57 @@
1 1
 public class Simulation {
2
+    private Dice dice;
3
+    private Integer numOfTosses;
4
+    private Integer startBound;
5
+    private Integer endBound;
6
+    private Bins bins;
2 7
 
8
+    //constructor
9
+    public Simulation(Integer numOfTosses, Integer numOfOfDies){
10
+        dice = new Dice(numOfOfDies);
3 11
 
12
+        startBound = numOfOfDies;
13
+        endBound = numOfOfDies * 6;
4 14
 
15
+        this.numOfTosses = numOfTosses;
16
+
17
+        bins = new Bins(startBound, endBound);
18
+    }
19
+
20
+    public void runSimulation(){
21
+
22
+        for (int i = 0; i < numOfTosses; i++){
23
+            bins.addToBin(dice.sumOfToss());
24
+        }
25
+    }
26
+
27
+    public static String stars(Integer percentages){
28
+        StringBuilder sb = new StringBuilder();
29
+
30
+        for (int i = 0; i < percentages; i++){
31
+             sb.append("*");
32
+        }
33
+        return sb.toString();
34
+
35
+        }
36
+
37
+    public String printResults(){
38
+        StringBuilder sb = new StringBuilder();
39
+        sb.append("***\nSimulation of " + startBound +" dice tossed for " + numOfTosses + " times.\n***\n\n");
40
+        for (int i = startBound; i <= endBound; i++) {
41
+            int diceSum = i;
42
+            int numOfOccurrence = bins.getBin(diceSum);
43
+            sb.append(String.format(" %2d :   %7d: %.2f ", diceSum, numOfOccurrence, (numOfOccurrence/(float)numOfTosses)));
44
+            sb.append(stars((int)((numOfOccurrence/(float)numOfTosses)*100))+ "\n");
45
+            }
46
+
47
+        System.out.println(sb.toString());
48
+        return sb.toString();
49
+        }
50
+
51
+
52
+    public static void main(String[] args) {
53
+        Simulation simulation = new Simulation(2, 1000000);
54
+        simulation.runSimulation();
55
+        simulation.printResults();
56
+    }
5 57
 }

+ 40
- 0
src/main/java/SimulationTest.java Näytä tiedosto

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