Browse Source

completed lab

Xcuello 6 years ago
parent
commit
59fe04a401

+ 3
- 3
README.md View File

@@ -9,15 +9,15 @@ Integer toss = dice.tossAndSum();
9 9
 ```
10 10
 make a couple unit tests for the Dice class. 
11 11
 
12
-Create a tracking class `Bins` that can be used to track the results of dice tosses.
12
+Create a tracking class `Cuello.Xzavia.Bins` that can be used to track the results of dice tosses.
13 13
 
14 14
 ```
15
-Bins results = new Bins(2, 12); // for bins from 2..12
15
+Cuello.Xzavia.Bins results = new Cuello.Xzavia.Bins(2, 12); // for bins from 2..12
16 16
 Integer numberOfTens = results.getBin(10); // returns the number of tens in the 10 bin
17 17
 results.incrementBin(10); // should increment bin # 10
18 18
 
19 19
 ```
20
-make a couple unit tests for the Bins class. Your test methods should follow this template:
20
+make a couple unit tests for the Cuello.Xzavia.Bins class. Your test methods should follow this template:
21 21
 
22 22
     in the tests
23 23
     Create a new Bin

+ 9
- 0
pom.xml View File

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

+ 35
- 0
src/Test/Java/Cuello/Xzavia/BinsTest.java View File

@@ -0,0 +1,35 @@
1
+package Cuello.Xzavia;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.util.HashMap;
7
+
8
+public class BinsTest {
9
+
10
+    @Test
11
+    public void constructorTest() {
12
+        //Given
13
+        Bins bins = new Bins(2, 12);
14
+
15
+        Integer actual = bins.bins.get(12);
16
+
17
+        Assert.assertTrue(actual == 0);
18
+    }
19
+
20
+    @Test
21
+    public void trackTossTest() {
22
+        //Given
23
+        Bins bins = new Bins(2,12);
24
+        Dice dice = new Dice(2);
25
+        Integer tossSum = dice.tossAndSum();
26
+        bins.trackToss(tossSum);
27
+
28
+        //When
29
+        Integer actual = bins.bins.get(tossSum);
30
+
31
+        //Then
32
+        Assert.assertTrue(actual ==1);
33
+
34
+    }
35
+}

+ 29
- 0
src/Test/Java/Cuello/Xzavia/DiceTest.java View File

@@ -0,0 +1,29 @@
1
+package Cuello.Xzavia;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class DiceTest {
7
+
8
+    @Test
9
+    public void getNumberOfDiceTest() {
10
+        //Given
11
+        Dice dice = new Dice(2);
12
+
13
+        Integer expected = 2;
14
+
15
+        Integer actual = dice.getNumberOfDice();
16
+
17
+        Assert.assertEquals(expected,actual);
18
+    }
19
+
20
+    @Test
21
+    public void tossAndSumTest() {
22
+        //Given
23
+        Dice dice = new Dice(2);
24
+
25
+        Integer actual = dice.tossAndSum();
26
+
27
+        Assert.assertTrue(actual >= 2 && actual <= 12);
28
+    }
29
+}

+ 24
- 0
src/Test/Java/Cuello/Xzavia/DieTest.java View File

@@ -0,0 +1,24 @@
1
+package Cuello.Xzavia;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class DieTest {
7
+
8
+    @Test
9
+    public void rollTest() {
10
+        //Given
11
+        Die die = new Die();
12
+        Integer actual = die.toss();
13
+
14
+        Assert.assertTrue(actual >= 1 && actual <= 6);
15
+    }
16
+    @Test
17
+    public void roll20Test() {
18
+        //Given
19
+        Die die = new Die();
20
+        Integer actual = die.toss();
21
+
22
+        Assert.assertTrue(actual >= 1 && actual <= 20);
23
+    }
24
+}

+ 12
- 0
src/Test/Java/SimulationTest.java View File

@@ -0,0 +1,12 @@
1
+import Cuello.Xzavia.Simulation;
2
+import org.junit.Test;
3
+
4
+public class SimulationTest {
5
+
6
+    @Test
7
+    public void simulationTest() {
8
+
9
+        Simulation simulation = new Simulation(2, 100);
10
+        simulation.runSimulation();
11
+    }
12
+}

+ 0
- 4
src/main/java/Bins.java View File

@@ -1,4 +0,0 @@
1
-
2
-public class Bins {
3
-
4
-}

+ 33
- 0
src/main/java/Cuello/Xzavia/Bins.java View File

@@ -0,0 +1,33 @@
1
+package Cuello.Xzavia;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
+
6
+public class Bins {
7
+
8
+    HashMap<Integer, Integer> bins;
9
+
10
+    public Bins(Integer min, Integer max) {
11
+
12
+        bins = new HashMap<Integer, Integer>();
13
+
14
+        initializeBins(min, max);
15
+    }
16
+
17
+    private void initializeBins(Integer min, Integer max) {
18
+        for(int i = min; i <= max; i++) {
19
+
20
+            bins.put(i, 0);
21
+        }
22
+
23
+
24
+
25
+        }
26
+
27
+
28
+
29
+    public void trackToss(int tossSum) {
30
+
31
+        bins.put(tossSum, bins.get(tossSum) + 1);
32
+    }
33
+}

+ 39
- 0
src/main/java/Cuello/Xzavia/Dice.java View File

@@ -0,0 +1,39 @@
1
+package Cuello.Xzavia;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Random;
5
+
6
+public class Dice {
7
+
8
+    ArrayList<Die> dice;
9
+
10
+    public Dice(Integer number) {
11
+        dice = new ArrayList<Die>();
12
+        initializeDice(number);
13
+
14
+    }
15
+
16
+    private void initializeDice(Integer number) {
17
+        for(int i = 0; i < number; i++) {
18
+            Die die = new Die();
19
+            dice.add(die);
20
+        }
21
+    }
22
+
23
+    public Integer getNumberOfDice() {
24
+
25
+        return dice.size();
26
+    }
27
+
28
+    public Integer tossAndSum() {
29
+        Integer sum = 0;
30
+        for(Die die : dice) {
31
+            sum += die.toss();
32
+        }
33
+
34
+
35
+        return sum;
36
+
37
+    }
38
+
39
+}

+ 23
- 0
src/main/java/Cuello/Xzavia/Die.java View File

@@ -0,0 +1,23 @@
1
+package Cuello.Xzavia;
2
+
3
+import java.util.Random;
4
+
5
+public class Die {
6
+    int sides;
7
+
8
+    public Die(Integer sides){
9
+        this.sides = sides;
10
+
11
+    }
12
+
13
+    public Die() {
14
+        this(6);
15
+
16
+    }
17
+
18
+    public Integer toss() {
19
+        Random random = new Random();
20
+
21
+        return random.nextInt(6) + 1;
22
+    }
23
+}

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

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

+ 44
- 0
src/main/java/Cuello/Xzavia/Simulation.java View File

@@ -0,0 +1,44 @@
1
+package Cuello.Xzavia;
2
+
3
+public class Simulation {
4
+
5
+    //Simulation sim = new Simulation(2, 10000);
6
+
7
+//sim.runSimulation();
8
+
9
+//sim.printResults();
10
+
11
+    private int numberOfDie;
12
+    private int numberOfTosses;
13
+    Dice dice;
14
+    Bins bins;
15
+
16
+    public Simulation(int numberOfDie,int numberOfTosses) {
17
+
18
+        this.numberOfDie = numberOfDie;
19
+        this.numberOfTosses = numberOfTosses;
20
+        this.dice = new Dice(numberOfDie);
21
+        this.bins = new Bins(numberOfDie, numberOfDie * 6);
22
+
23
+        Dice dice = new Dice(numberOfDie);
24
+        Bins bins = new Bins(numberOfDie, numberOfDie * 6);
25
+    }
26
+
27
+    public void runSimulation() {
28
+
29
+        for(int i = 1; i <= numberOfTosses; i++) {
30
+
31
+            bins.trackToss(dice.tossAndSum());
32
+
33
+        }
34
+
35
+    }
36
+
37
+    public Bins returnResults() {
38
+        return this.bins;
39
+    }
40
+
41
+
42
+
43
+
44
+}

+ 0
- 0
src/main/java/Cuello/Xzavia/package-info.java View File


+ 0
- 4
src/main/java/Dice.java View File

@@ -1,4 +0,0 @@
1
-public class Dice {
2
-
3
-
4
-}

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

@@ -1,5 +0,0 @@
1
-public class Simulation {
2
-
3
-
4
-
5
-}