Browse Source

finished labgit status

Kate Moore 6 years ago
parent
commit
89fad1529d

+ 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>compile</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

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

@@ -17,7 +17,7 @@ public class Bins {
17 17
         }
18 18
     }
19 19
 
20
-    public HashMap getResults(){
21
-        return this.results;
20
+    public void addingToKey(int key) {
21
+        this.results.put(key, this.results.get(key) + 1);
22 22
     }
23 23
 }

+ 43
- 0
src/main/java/BinsTest.java View File

@@ -0,0 +1,43 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class BinsTest {
5
+
6
+    Dice testDice;
7
+    Bins testBins;
8
+
9
+    public BinsTest() {
10
+
11
+        testDice = new Dice(1);
12
+        testBins = new Bins(1, 6);
13
+    }
14
+
15
+    @Test
16
+    public void testBinKey(){
17
+
18
+        //Given
19
+        int key = 1;
20
+        int expected = 0;
21
+
22
+        //When
23
+        int actual = testBins.results.get(key);
24
+
25
+        Assert.assertEquals(expected, actual);
26
+
27
+    }
28
+
29
+    @Test
30
+    public void addingToKeyTest() {
31
+        int key = 1;
32
+        int expected = 100;
33
+        int actual = 0;
34
+
35
+        for(int i = 0; i < expected; i++) {
36
+            testBins.addingToKey(key);
37
+        }
38
+        actual = testBins.results.get(key);
39
+
40
+        Assert.assertEquals(expected, actual);
41
+
42
+    }
43
+}

+ 33
- 0
src/main/java/DiceTest.java View File

@@ -0,0 +1,33 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class DiceTest {
5
+
6
+    Dice testDice;
7
+
8
+    public DiceTest() {
9
+        testDice= new Dice(1);
10
+
11
+    }
12
+
13
+
14
+    @Test
15
+    public void tossAndSumTest() {
16
+        int[] numbers = {1, 2, 3, 4, 5, 6};
17
+        boolean check = false;
18
+
19
+        int actual = testDice.tossAndSum();
20
+
21
+        for(int i = 0; i <numbers.length; i++) {
22
+            if(actual == numbers[i]) {
23
+                check = true;
24
+                break;
25
+            }
26
+        }
27
+
28
+        Assert.assertTrue(check);
29
+
30
+
31
+
32
+    }
33
+}

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

@@ -0,0 +1,11 @@
1
+public class Main {
2
+
3
+
4
+    public static void main(String[] args) {
5
+        Simulation simulation = new Simulation(2, 1000000);
6
+
7
+        System.out.println(simulation.toString());
8
+    }
9
+
10
+
11
+}

+ 41
- 0
src/main/java/Simulation.java View File

@@ -1,5 +1,46 @@
1 1
 public class Simulation {
2 2
 
3
+    Dice dice;
4
+    Bins bins;
5
+    int numOfDice;
6
+    int numOfTosses;
3 7
 
8
+    public Simulation(int numOfDice, int numOfTosses) {
9
+        dice = new Dice(numOfDice);
10
+        bins = new Bins(numOfDice, numOfDice * 6);
11
+        this.numOfDice = numOfDice;
12
+        this.numOfTosses = numOfTosses;
13
+
14
+        for(int i = 0; i< numOfTosses; i++){
15
+            int key = dice.tossAndSum();
16
+            bins.addingToKey(key);
17
+        }
18
+    }
19
+
20
+    public String toString() {
21
+        StringBuilder str = new StringBuilder();
22
+
23
+
24
+        str.append("*** \n");
25
+        str.append("Simulation of " + this.numOfDice + " dice tossed for " + this.numOfTosses + " times. \n");
26
+        str.append("*** \n");
27
+
28
+        for(int element : bins.results.keySet()) {
29
+            double percent =  ((double)bins.results.get(element) / (double)this.numOfTosses);
30
+            int starNum = (int) Math.floor((double) bins.results.get(element) / (this.numOfTosses / 100));
31
+            str.append(String.format("%3d : %8d : %.2f  %s %s", element, bins.results.get(element), percent, stars(starNum), "\n" ));
32
+
33
+        }
34
+        return str.toString();
35
+    }
36
+
37
+    public String stars(int number) {
38
+        String str = "";
39
+
40
+        for(int i =0; i < number; i++) {
41
+            str+= "*";
42
+        }
43
+        return str;
44
+    }
4 45
 
5 46
 }