CHU1TA26 6 lat temu
rodzic
commit
bad443bb7d

+ 14
- 0
pom.xml Wyświetl plik

@@ -7,6 +7,20 @@
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>4.12</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+        <dependency>
18
+            <groupId>junit</groupId>
19
+            <artifactId>junit</artifactId>
20
+            <version>4.12</version>
21
+            <scope>test</scope>
22
+        </dependency>
23
+    </dependencies>
10 24
 
11 25
 
12 26
 </project>

+ 33
- 0
src/main/java/Bins.java Wyświetl plik

@@ -1,4 +1,37 @@
1 1
 
2
+import java.util.HashMap;
3
+
2 4
 public class Bins {
5
+private int [] bin;
6
+private int numberOfDice;
7
+HashMap< Integer, Integer> binHolder=new HashMap<Integer, Integer>();
8
+
9
+
10
+public Bins( int numberOfDice) {
11
+       this.numberOfDice = numberOfDice;
12
+
13
+       for (int i = numberOfDice; i <= numberOfDice * 6; i++) {
14
+           binHolder.put(i, 0);
15
+
16
+       }
17
+   }
18
+
19
+
20
+
3 21
 
22
+
23
+
24
+    public void incrementBin(int binnum) {
25
+       int value=binHolder.get(binnum);
26
+       value ++;
27
+       binHolder.put(binnum, value);
28
+
29
+        }
30
+
31
+
32
+    public int getBin(int binNumber) {
33
+        return this.binHolder.get(binNumber);
34
+
35
+    }
4 36
 }
37
+

+ 24
- 0
src/main/java/Dice.java Wyświetl plik

@@ -1,4 +1,28 @@
1
+import java.util.Random;
2
+
1 3
 public class Dice {
2 4
 
5
+    int diceNumber = 0;
6
+
7
+    public Dice(int diceNumber) {
8
+
9
+        this.diceNumber = diceNumber;
10
+    }
11
+
12
+    public int tossAndSum () {
13
+        int toss= 0;
14
+        for (int i=0; i<diceNumber; i++){
15
+            Random die =new Random ();
16
+            toss+= 1 + (die.nextInt(6));
17
+            System.out.println(toss);
18
+        }
19
+        return toss;
20
+    }
21
+
22
+
3 23
 
4 24
 }
25
+
26
+
27
+
28
+

+ 44
- 0
src/main/java/Simulation.java Wyświetl plik

@@ -1,4 +1,48 @@
1 1
 public class Simulation {
2
+    private int  numberOfDice=0;
3
+    private int numberOftoss=0;
4
+    private int binCount=0;
5
+    private Bins bins;
6
+    private Dice dice;
7
+
8
+public Simulation(int numberOfDice, int numberOftoss){
9
+    this.numberOftoss=numberOftoss;
10
+    this.numberOfDice=numberOfDice;
11
+    this.binCount=0;
12
+    this.bins= new Bins(numberOfDice);
13
+    this.dice= new Dice(numberOfDice);
14
+
15
+
16
+
17
+    }
18
+    public void runSimulation(){
19
+    for (int i=1; i<=this.numberOftoss; i++){
20
+       this.bins.incrementBin(this.dice.tossAndSum());
21
+        binCount++;
22
+
23
+    }
24
+
25
+
26
+    }
27
+public int getBinCount(){
28
+    return binCount;
29
+}
30
+
31
+//public double getPersentage(int binNumber ){
32
+//    double persentage=(bins.getBin(binNumber)/this.numberOftoss);
33
+//    return  persentage;
34
+//}
35
+
36
+public void printSimulation(){
37
+    System.out.println("Simulation of " + numberOfDice+  " " + numberOftoss+  " times.");
38
+
39
+    for (int i = this.numberOfDice; i <= (this.numberOfDice * 6); i++) {
40
+        System.out.println(String.format("%3d : %9d : ", (i), this.bins.getBin(i)) + String.format("%.2f", (double) bins.getBin(i) / this.numberOftoss));
41
+
42
+    }
43
+
44
+}
45
+
2 46
 
3 47
 
4 48
 

+ 10
- 0
src/main/java/main.java Wyświetl plik

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

+ 77
- 0
src/test/test/BinTest.java Wyświetl plik

@@ -0,0 +1,77 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertEquals;
5
+
6
+public class BinTest {
7
+    @Test
8
+    public void testGetBin(){
9
+        //given
10
+        Bins bins = new Bins(2);
11
+        int expected=0;
12
+        //when
13
+        int actual = bins.getBin(3);
14
+        //then
15
+        assertEquals(expected, actual);
16
+
17
+    }
18
+
19
+
20
+    @Test
21
+    public void testIncrementBin(){
22
+        //given
23
+        Bins bins= new Bins(5);
24
+
25
+        int expected=1;
26
+        int binNumber=5;
27
+        //when
28
+        bins.incrementBin(binNumber);
29
+
30
+       int actual= bins.getBin(binNumber);
31
+        //actual
32
+        Assert.assertEquals(expected,actual);
33
+    }
34
+
35
+    @Test
36
+    public void testIncrementBin100Times(){
37
+        //given
38
+        Bins bins= new Bins(5);
39
+
40
+        int expected=100;
41
+        int binNumber=5;
42
+        //when
43
+        for (int i= 1; i<=100; i++) {
44
+            bins.incrementBin(binNumber);
45
+        }
46
+        int actual= bins.getBin(binNumber);
47
+        //actual
48
+        Assert.assertEquals(expected,actual);
49
+    }
50
+    @Test
51
+    public void testIncrementBin1000Times(){
52
+        //given
53
+        Bins bins= new Bins(5);
54
+
55
+        int expected=1000;
56
+        int binNumber=5;
57
+        //when
58
+        for (int i= 1; i<=1000; i++) {
59
+            bins.incrementBin(binNumber);
60
+        }
61
+        int actual= bins.getBin(binNumber);
62
+        //actual
63
+        Assert.assertEquals(expected,actual);
64
+    }
65
+
66
+    @Test
67
+    public void getBinValueTest(){
68
+        // given
69
+        Bins bin = new Bins(6);
70
+        int expected=0;
71
+        //when
72
+        int actual= bin.getBin(6);
73
+        //then
74
+        Assert.assertEquals(expected, actual);
75
+    }
76
+
77
+}

+ 7
- 0
src/test/test/DiceyLabTest.java Wyświetl plik

@@ -0,0 +1,7 @@
1
+//import sun.jvm.hotspot.utilities.Assert;
2
+//import org.junit.Test;
3
+
4
+//public class DiceyLabTest {
5
+
6
+
7
+//}

+ 50
- 0
src/test/test/SimulationTest.java Wyświetl plik

@@ -0,0 +1,50 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class SimulationTest {
5
+    @Test
6
+    public void testRunSim (){
7
+        //given
8
+        int numberOfDice=2;
9
+        int numberOfToss=4;
10
+        Simulation simulation= new Simulation(numberOfDice, numberOfToss);
11
+        int expected = numberOfToss;
12
+        //when
13
+        simulation.runSimulation();
14
+        int actual = numberOfToss;
15
+        //then
16
+        Assert.assertEquals(expected, actual);
17
+    }
18
+    @Test
19
+   public void testRunSim100Toss() {
20
+        //Given
21
+        int numbDies = 2;
22
+        int numbTosses = 100;
23
+        Simulation sim2 = new Simulation(numbDies, numbTosses);
24
+        int expected = numbTosses;
25
+
26
+        //When
27
+        sim2.runSimulation();
28
+        int actual = sim2.getBinCount();
29
+
30
+        //Then
31
+        Assert.assertEquals(expected,actual);
32
+    }@Test
33
+    public void testRunSim1000Toss() {
34
+        //Given
35
+        int numbDies = 2;
36
+        int numbTosses = 1000;
37
+        Simulation sim2 = new Simulation(numbDies, numbTosses);
38
+        int expected = numbTosses;
39
+
40
+        //When
41
+        sim2.runSimulation();
42
+        int actual = sim2.getBinCount();
43
+
44
+        //Then
45
+        Assert.assertEquals(expected,actual);
46
+    }
47
+
48
+
49
+
50
+}

+ 16
- 0
src/test/test/diceTest.java Wyświetl plik

@@ -0,0 +1,16 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class diceTest {
5
+    @Test
6
+    public void testTossAndSum(){
7
+        //given
8
+        Dice dice = new Dice(2);
9
+        boolean expected=true;
10
+        //when
11
+
12
+       boolean actual = dice.tossAndSum() <= 12 && dice.tossAndSum() >= 1;
13
+        //then
14
+        Assert.assertEquals(expected, actual);
15
+    }
16
+}