thulasi před 6 roky
rodič
revize
ab0f3ec882

binární
.DS_Store Zobrazit soubor


+ 8
- 0
pom.xml Zobrazit soubor

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

binární
src/.DS_Store Zobrazit soubor


binární
src/main/.DS_Store Zobrazit soubor


+ 51
- 0
src/main/java/Bins.java Zobrazit soubor

@@ -1,4 +1,55 @@
1
+import java.util.HashMap;
2
+import java.util.HashSet;
3
+import java.util.Map;
4
+import java.util.Set;
1 5
 
2 6
 public class Bins {
3 7
 
8
+
9
+    private int min, max;
10
+    private Map<Integer, Integer> bins;
11
+    private int noOfDies;
12
+
13
+    public Map<Integer, Integer> getBins() {
14
+        return bins;
15
+    }
16
+
17
+    public Bins(int noOfDies){
18
+        min= noOfDies;
19
+        max = noOfDies*6;
20
+        bins = new HashMap<Integer, Integer>();
21
+        this.noOfDies = noOfDies;
22
+
23
+
24
+        //bins = new int[((noOfDies*6)-noOfDies)+1];
25
+        createBins();
26
+    }
27
+
28
+    private void createBins(){
29
+        for(int i=min; i<=max;i++)
30
+        {
31
+            bins.put(i,0);
32
+        }
33
+    }
34
+
35
+    public void IncrementBin(int toss){
36
+
37
+       if(bins.containsKey(toss)){
38
+           int value = bins.get(toss);
39
+           bins.put(toss,value+1);
40
+       }
41
+
42
+    }
43
+
44
+    public int getBin(int toss){
45
+
46
+        if(bins.containsKey(toss)){
47
+            return bins.get(toss);
48
+        }
49
+        return 0;
50
+    }
51
+
52
+
53
+
54
+
4 55
 }

+ 22
- 0
src/main/java/Dice.java Zobrazit soubor

@@ -1,4 +1,26 @@
1 1
 public class Dice {
2
+    private int noOfDies;
3
+   // private Bins bin;
4
+
5
+    public Dice(int number){
6
+        this.noOfDies = number;
7
+       // bin = new Bins(noOfDies);
8
+    }
9
+
10
+
11
+
12
+
13
+
14
+    public int tossAndSum(){
15
+        int sum = 0;
16
+        for(int i=0;i<this.noOfDies;i++){
17
+            int toss = (int)(Math.random()*6 + 1);
18
+            ////bin.IncrementBin(toss);
19
+            sum += toss;
20
+        }
21
+        return sum;
22
+    }
23
+
2 24
 
3 25
 
4 26
 }

+ 16
- 0
src/main/java/MainApplication.java Zobrazit soubor

@@ -0,0 +1,16 @@
1
+public class MainApplication {
2
+
3
+    public static void main(String[] args) {
4
+
5
+        Dice dice = new Dice(2);
6
+        Bins bins = new Bins(2);
7
+
8
+        Simulation simulation = new Simulation(2,1000000, dice, bins);
9
+
10
+        simulation.runSimulation();
11
+
12
+        simulation.printBinValues();
13
+
14
+
15
+    }
16
+}

+ 50
- 0
src/main/java/Simulation.java Zobrazit soubor

@@ -1,5 +1,55 @@
1
+import java.util.Map;
2
+import java.util.Set;
3
+
1 4
 public class Simulation {
2 5
 
6
+private int diceNum;
7
+private int tossNum;
8
+private Dice dice;
9
+private Bins bin;
10
+
11
+    public Simulation(int diceNum, int tossNum, Dice dice, Bins bin) {
12
+        this.diceNum = diceNum;
13
+        this.tossNum = tossNum;
14
+        this.dice = dice;
15
+        this.bin = bin;
16
+
17
+    }
18
+
19
+
20
+    public void runSimulation(){
21
+        for(int i = 0; i < tossNum; i++){
22
+             bin.IncrementBin(dice.tossAndSum());
23
+        }
24
+    }
25
+
26
+
27
+    public void printBinValues(){
28
+        runSimulation();
29
+
30
+        StringBuilder sb = new StringBuilder();
31
+        sb.append("**\nSimulation of " + diceNum + " dice tossed for " + tossNum + " times.\n** \n\n\n");
32
+
33
+        Map<Integer,Integer> map = bin.getBins();
34
+        Set<Integer> keySet = map.keySet();
35
+
36
+
37
+
38
+        for(Integer key : keySet){
39
+            sb.append(String.format("%3d : " + "%8d : " + "%.2f", key, map.get(key), (double)map.get(key)/(double)tossNum * 100.0));
40
+            sb.append("\t");
41
+           // int starCount = (int) ((map.get(key)/tossNum) * 100);
42
+            for (int i = 0; i < (double)map.get(key)/(double)tossNum * 100.0; i++) {
43
+                sb.append("*");
44
+            }
45
+            sb.append("\n");
46
+        }
47
+
48
+        sb.append("\n");
49
+
50
+        System.out.println(sb.toString());
51
+
3 52
 
53
+    }
4 54
 
5 55
 }

+ 50
- 0
src/test/java/BinsTest.java Zobrazit soubor

@@ -0,0 +1,50 @@
1
+
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.HashMap;
8
+import java.util.Map;
9
+
10
+import static org.junit.Assert.*;
11
+
12
+public class BinsTest {
13
+
14
+
15
+    Bins bin = new Bins(2);
16
+    int noOfDies = 2;
17
+    Map<Integer,Integer> map = new HashMap<Integer, Integer>();
18
+
19
+    @Test
20
+    public void testIncrementBins(){
21
+        int toss =2;
22
+        int expected = 1;
23
+
24
+        bin.IncrementBin(2);
25
+        int actual = bin.getBin(toss);
26
+        Assert.assertEquals(expected, actual);
27
+    }
28
+
29
+    @Test
30
+    public void testCreateBins(){
31
+        for(int i=noOfDies; i<=noOfDies*6;i++)
32
+        {
33
+            map.put(i,0);
34
+        }
35
+        Assert.assertEquals(map, bin.getBins());
36
+    }
37
+
38
+   @Test
39
+    public void testGetBins(){
40
+        map.put(2,0);
41
+        int expected = 0;
42
+        int actual = bin.getBin(2);
43
+        Assert.assertEquals(expected, actual);
44
+   }
45
+
46
+
47
+
48
+
49
+
50
+}

+ 16
- 0
src/test/java/DiceTest.java Zobrazit soubor

@@ -0,0 +1,16 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class DiceTest {
5
+
6
+    @Test
7
+    public void testTossAndSum(){
8
+
9
+        Dice dice = new Dice(2);
10
+        int actual = dice.tossAndSum();
11
+
12
+        Assert.assertTrue(actual>2 && actual<12);
13
+
14
+    }
15
+
16
+}