Procházet zdrojové kódy

methods written tests passed

Jacob Andersen před 6 roky
rodič
revize
1b8399d664

binární
.DS_Store Zobrazit soubor


+ 11
- 11
README.md Zobrazit soubor

@@ -1,23 +1,23 @@
1
-# Kris-Tof Dice Toss
1
+# Kris-Tof Andersen.Jacob.Dice Toss
2 2
 
3
-Create a `Dice` class that acts like a set of N random-tossed dies.
3
+Create a `Andersen.Jacob.Dice` class that acts like a set of N random-tossed dies.
4 4
 ```
5
-Dice dice = new Dice(2); // for craps
6
-Dice dice = new Dice(5); // for yatzee
5
+Andersen.Jacob.Dice dice = new Andersen.Jacob.Dice(2); // for craps
6
+Andersen.Jacob.Dice dice = new Andersen.Jacob.Dice(5); // for yatzee
7 7
 
8 8
 Integer toss = dice.tossAndSum();
9 9
 ```
10
-make a couple unit tests for the Dice class. 
10
+make a couple unit tests for the Andersen.Jacob.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 `Andersen.Jacob.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
+Andersen.Jacob.Bins results = new Andersen.Jacob.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 Andersen.Jacob.Bins class. Your test methods should follow this template:
21 21
 
22 22
     in the tests
23 23
     Create a new Bin
@@ -27,7 +27,7 @@ make a couple unit tests for the Bins class. Your test methods should follow thi
27 27
     
28 28
     
29 29
 
30
-Create a `Simulation` class whose Constructor takes arguments:
30
+Create a `Andersen.Jacob.Simulation` class whose Constructor takes arguments:
31 31
     numberOfDies to throw
32 32
     numberOfTosses to run
33 33
 
@@ -35,7 +35,7 @@ Create a simulation where two dies are thrown *one million times*.
35 35
 Keep track of each roll by incrementing the correct Bin.
36 36
 
37 37
 ```
38
-Simulation sim = new Simulation(2, 10000);
38
+Andersen.Jacob.Simulation sim = new Andersen.Jacob.Simulation(2, 10000);
39 39
 
40 40
 sim.runSimulation();
41 41
 
@@ -53,7 +53,7 @@ the results of the sim.printResults() should be an output string that looks like
53 53
 
54 54
 ```
55 55
 ***
56
-Simulation of 2 dice tossed for 1000000 times.
56
+Andersen.Jacob.Simulation of 2 dice tossed for 1000000 times.
57 57
 ***
58 58
 
59 59
  2 :    27917: 0.03 **

+ 11
- 0
jacobresults.md Zobrazit soubor

@@ -0,0 +1,11 @@
1
+ 2 :    28046: 0.03 **   
2
+  3 :    55669: 0.06 *****
3
+  4 :    83555: 0.08 ********
4
+  5 :   110838: 0.11 ***********
5
+  6 :   138899: 0.14 *************
6
+  7 :   166007: 0.17 ****************
7
+  8 :   138650: 0.14 *************
8
+  9 :   111201: 0.11 ***********
9
+ 10 :    83719: 0.08 ********
10
+ 11 :    55728: 0.06 *****
11
+ 12 :    27687: 0.03 **   

+ 8
- 1
pom.xml Zobrazit soubor

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

binární
src/.DS_Store Zobrazit soubor


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


binární
src/Test/java/.DS_Store Zobrazit soubor


binární
src/Test/java/Andersen/.DS_Store Zobrazit soubor


+ 36
- 0
src/Test/java/Andersen/Jacob/Binstest.java Zobrazit soubor

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

+ 4
- 0
src/Test/java/Andersen/Jacob/Checkbin.java Zobrazit soubor

@@ -0,0 +1,4 @@
1
+package Andersen.Jacob;
2
+
3
+public class Checkbin {
4
+}

+ 23
- 0
src/Test/java/Andersen/Jacob/DiceTest2.java Zobrazit soubor

@@ -0,0 +1,23 @@
1
+package Andersen.Jacob;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class DiceTest2 {
7
+    @Test
8
+    public void getNumberOFDiceTest() {
9
+        //given
10
+        Dice dice = new Dice(2);
11
+        Integer expected = 2;
12
+        Integer actual = dice.getNumberOfDice();
13
+        Assert.assertEquals(actual, expected);
14
+    }
15
+
16
+    @Test
17
+    public void tossAndSumTest() {
18
+        //given
19
+        Dice dice = new Dice(2);
20
+        Integer  actual = dice.tossAndSum();
21
+        Assert.assertTrue(actual >= 2 && actual <= 12);
22
+    }
23
+}

+ 17
- 0
src/Test/java/Andersen/Jacob/Dicetest.java Zobrazit soubor

@@ -0,0 +1,17 @@
1
+package Andersen.Jacob;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class Dicetest {
7
+
8
+    @Test
9
+    public void rolltest(){
10
+        //Given
11
+        Die die = new Die();
12
+        Integer actual = die.toss();
13
+        Assert.assertTrue(actual >=1 && actual <=666);
14
+    }
15
+
16
+}
17
+

+ 15
- 0
src/Test/java/SimulationTest.java Zobrazit soubor

@@ -0,0 +1,15 @@
1
+import Andersen.Jacob.Simulation;
2
+import org.junit.Test;
3
+
4
+public class SimulationTest {
5
+    @Test
6
+    public void runSimulation(){
7
+        //Given
8
+        //numberOfDies
9
+        //When
10
+        Simulation simulation = new Simulation(2,100);
11
+        simulation.runSimulation();
12
+
13
+        //Then
14
+    }
15
+}

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


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


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


+ 22
- 0
src/main/java/Andersen/Jacob/Bins.java Zobrazit soubor

@@ -0,0 +1,22 @@
1
+package Andersen.Jacob;
2
+
3
+import java.util.HashMap;
4
+
5
+public class Bins {
6
+    HashMap<Integer, Integer> bins;
7
+
8
+    public Bins(Integer min, Integer max) {
9
+        bins = new HashMap<Integer, Integer>();
10
+        initializeBins(min,max);
11
+    }
12
+
13
+    private void initializeBins(Integer min, Integer max) {
14
+        for (int i = min; i <= max; i++) {
15
+            bins.put(i, 0);
16
+        }
17
+    }
18
+    public void trackToss(int tossSum){
19
+        bins.put(tossSum, bins.get(tossSum)+1);
20
+
21
+    }
22
+}

+ 33
- 0
src/main/java/Andersen/Jacob/Dice.java Zobrazit soubor

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

+ 20
- 0
src/main/java/Andersen/Jacob/Die.java Zobrazit soubor

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

+ 14
- 0
src/main/java/Andersen/Jacob/Main.java Zobrazit soubor

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

+ 55
- 0
src/main/java/Andersen/Jacob/Simulation.java Zobrazit soubor

@@ -0,0 +1,55 @@
1
+package Andersen.Jacob;
2
+
3
+public class Simulation {
4
+
5
+    int numberOfDies;
6
+    int numberOfTosses;
7
+    Dice dice;
8
+    public Bins bin;
9
+
10
+    public Simulation(int numberOfDies, int numberOfTosses) {
11
+        this.numberOfDies = numberOfDies;
12
+        this.numberOfTosses = numberOfTosses;
13
+        this.dice = new Dice(numberOfDies);
14
+        this.bin = new Bins(numberOfDies, numberOfDies * 6);
15
+
16
+    }
17
+
18
+    public void runSimulation() {
19
+
20
+        for (int i = 1; i < numberOfTosses; i++) {
21
+            bin.trackToss(dice.tossAndSum());
22
+        }
23
+
24
+    }
25
+
26
+    public String countStars(double percent){
27
+        String result="";
28
+        int d=(int)(percent*100);
29
+        for (int i=0;i<d;i++){
30
+            result+="*";
31
+        }
32
+        return result;
33
+
34
+    }
35
+
36
+    public void returnResults() {
37
+
38
+        int d=0;
39
+        double freqPerc=0;
40
+        double freqPerc2=bin.bins.get(2)/10000000;
41
+        for(int i=2;i<13;i++){
42
+            d=bin.bins.get(i);
43
+
44
+            freqPerc=((double)d/numberOfTosses);
45
+
46
+            String str=String.format("%3d : %8d: %-2.2f %-5s",i,d, freqPerc, countStars(freqPerc));
47
+            System.out.println(str);
48
+
49
+        }
50
+    }
51
+}
52
+
53
+
54
+
55
+

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

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

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

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

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

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