Ver código fonte

finished dicey lab and comleted tests

Connor Dunnigan 6 anos atrás
pai
commit
dc458c92ca

+ 17
- 0
ConnorResults.md Ver arquivo

@@ -0,0 +1,17 @@
1
+***********************************************
2
+Simulation of 2 dice tossed for 1000000 times.
3
+***********************************************
4
+
5
+  2 :     27510 ->   0.03 **   
6
+  3 :     55225 ->   0.06 *****
7
+  4 :     83303 ->   0.08 ********
8
+  5 :    110520 ->   0.11 ***********
9
+  6 :    138690 ->   0.14 *************
10
+  7 :    167183 ->   0.17 ****************
11
+  8 :    139012 ->   0.14 *************
12
+  9 :    111402 ->   0.11 ***********
13
+ 10 :     83567 ->   0.08 ********
14
+ 11 :     55923 ->   0.06 *****
15
+ 12 :     27665 ->   0.03 **   
16
+
17
+

+ 9
- 1
pom.xml Ver arquivo

@@ -7,6 +7,14 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
-
10
+    <dependencies>
11
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.12</version>
16
+            <scope>test</scope>
17
+        </dependency>
18
+    </dependencies>
11 19
 
12 20
 </project>

+ 16
- 0
src/main/java/Bins.java Ver arquivo

@@ -1,4 +1,20 @@
1
+import java.util.*;
1 2
 
2 3
 public class Bins {
3 4
 
5
+    HashMap<Integer, Integer> binResults = new HashMap<Integer, Integer>();
6
+
7
+    Bins(int numOfDice){
8
+        int size = getPossibleSize(numOfDice);
9
+        for(int i = 0; i <= size; i++){
10
+            binResults.put(i+numOfDice, 0);
11
+        }
12
+    }
13
+
14
+    public int getPossibleSize(int num){ return ((num*6)-(num)); }
15
+
16
+    public void incrementBin(int rollVal){
17
+        int newVal = binResults.get(rollVal)+1;
18
+        this.binResults.put(rollVal, newVal);
19
+    }
4 20
 }

+ 14
- 1
src/main/java/Dice.java Ver arquivo

@@ -1,4 +1,17 @@
1 1
 public class Dice {
2 2
 
3
+    private int numDice;
3 4
 
4
-}
5
+    Dice(){ this.numDice = 1; }
6
+    Dice(int totalDice){ this.numDice = totalDice; }
7
+
8
+    public int getNumDice(){ return this.numDice; }
9
+
10
+    public int tossSum(){
11
+        int sum = 0;
12
+        for(int i = 0; i < getNumDice(); i ++){
13
+            sum += (int)(Math.random() * 6) + 1;
14
+        }
15
+        return sum;
16
+    }
17
+}

+ 8
- 0
src/main/java/MainApp.java Ver arquivo

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

+ 41
- 0
src/main/java/Simulation.java Ver arquivo

@@ -1,5 +1,46 @@
1 1
 public class Simulation {
2 2
 
3
+    int numOfDice, numOfTosses;
4
+    Dice dice;
5
+    Bins bin;
3 6
 
7
+    Simulation(int numDice, int numToss){
8
+        this.numOfDice = numDice;
9
+        this.numOfTosses = numToss;
10
+        dice = new Dice(numDice);
11
+        bin = new Bins(numDice);
12
+    }
4 13
 
14
+    public int getRoll(){ return dice.tossSum(); }
15
+
16
+    public void simulate(){
17
+        for(int i = 0; i< this.numOfTosses; i++){
18
+            bin.incrementBin(getRoll());
19
+        }
20
+    }
21
+
22
+    public double toPercent(int diceVal){
23
+        double result = (double)bin.binResults.get(diceVal)/this.numOfTosses;
24
+        return result;
25
+    }
26
+
27
+    public String getStars(double percent) {
28
+        String output = new String();
29
+        int numOfStars = (int)(percent*100);
30
+        for(int i = 0; i < numOfStars; i++){
31
+            output += "*";
32
+        }
33
+        return output;
34
+    }
35
+
36
+    public void printResults(){
37
+        System.out.println("***********************************************");
38
+        System.out.println("Simulation of " + this.numOfDice + " dice tossed for " + this.numOfTosses + " times.");
39
+        System.out.println("***********************************************\n");
40
+        for(Integer num : bin.binResults.keySet()) {
41
+            String output = String.format("%3d : %9d -> %6.2f %-5s", num, bin.binResults.get(num),
42
+                    toPercent(num), getStars(toPercent(num)));
43
+            System.out.println(output);
44
+        }
45
+    }
5 46
 }

+ 89
- 0
src/test/java/DiceTest.java Ver arquivo

@@ -0,0 +1,89 @@
1
+import org.junit.Test;
2
+import org.junit.Assert;
3
+
4
+public class DiceTest {
5
+
6
+    @Test
7
+    public void testDice() {
8
+        //Given
9
+        Dice die = new Dice();
10
+        //When
11
+        int[] diceVals = {1, 2, 3, 4, 5, 6};
12
+        //Then
13
+        boolean contains = false;
14
+        int rollVal = die.tossSum();
15
+        for (int val : diceVals) {
16
+            if (val == rollVal) {
17
+                contains = true;
18
+                break;
19
+            }
20
+        }
21
+        Assert.assertEquals(true, contains);
22
+    }
23
+
24
+    @Test
25
+    public void testDice2() {
26
+        //Given
27
+        Dice dice = new Dice(2);
28
+        //When
29
+        int[] possVals = {2, 3, 4, 5, 6,7,8,9,10,11,12};
30
+        //Then
31
+        boolean contains = false;
32
+        int rollVal = dice.tossSum();
33
+        for (int val : possVals) {
34
+            if (val == rollVal) {
35
+                contains = true;
36
+                break;
37
+            }
38
+        }
39
+        Assert.assertEquals(true, contains);
40
+    }
41
+
42
+    @Test
43
+    public void testBin1() {
44
+        //Given
45
+        Bins bin1 = new Bins(2);
46
+        int expVal = 10;
47
+        //When
48
+        int actVal = bin1.getPossibleSize(2);
49
+        //Then
50
+        Assert.assertEquals(expVal, actVal);
51
+    }
52
+
53
+    @Test
54
+    public void testBin2() {
55
+        //Given
56
+        Bins bin2 = new Bins(3);
57
+        int expVal = 15;
58
+        //When
59
+        int actVal = bin2.getPossibleSize(3);
60
+        //Then
61
+        Assert.assertEquals(expVal, actVal);
62
+    }
63
+
64
+    @Test
65
+    public void testSim1() {
66
+        //Given
67
+        Bins bin = new Bins(2);
68
+        Simulation sim = new Simulation(2,10);
69
+        sim.simulate();
70
+        String expOut = "*****";
71
+        //When
72
+        String actOut = sim.getStars(.05);
73
+        //Then
74
+        Assert.assertEquals(expOut, actOut);
75
+    }
76
+
77
+    @Test
78
+    public void testSim2() {
79
+        //Given
80
+        Bins bin = new Bins(2);
81
+        Simulation sim = new Simulation(2,100000);
82
+        sim.simulate();
83
+        double expOut = .17;
84
+        //When
85
+        double actOut = sim.toPercent(7);
86
+        //Then
87
+        Assert.assertEquals(expOut, actOut, .01);
88
+    }
89
+}