Przeglądaj źródła

Merge e8d48e76ba3bede41adbb8ef8adc26c4317b4780 into 1eaff706f0dcc7a467d1c6648bcb494e313ba6a5

kbrinker1 6 lat temu
rodzic
commit
bc0e613f08
Brak konta powiązanego z e-mailem autora

+ 7
- 0
pom.xml Wyświetl plik

@@ -7,6 +7,13 @@
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
+        </dependency>
16
+    </dependencies>
10 17
 
11 18
 
12 19
 </project>

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

@@ -1,4 +1,57 @@
1
+import java.util.TreeMap;
1 2
 
2 3
 public class Bins {
4
+    private int startingBound;
5
+    private int endingBound;
6
+    private int[] rollSums;
7
+
8
+    //Constructor
9
+    public Bins(int startingBound, int endingBound) {
10
+        rollSums = new int[endingBound+1];
11
+    }
12
+
13
+
14
+    //Methods
15
+    public int getBin(int value) {
16
+        return rollSums[value];
17
+    }
18
+
19
+    public void incrementBin(int value) {
20
+        rollSums[value] = rollSums[value] + 1;
21
+    }
3 22
 
4 23
 }
24
+
25
+
26
+
27
+//public class Bins{
28
+//
29
+//   private TreeMap<Integer, Integer> bins = new TreeMap<Integer, Integer>();
30
+//
31
+//    public TreeMap<Integer, Integer> getBins() {
32
+//        return bins;
33
+//    }
34
+//
35
+//    public int getBin(int binNumber){
36
+//        return bins.get(binNumber);
37
+//    }
38
+//
39
+//
40
+//    public void incrementBin(int binNumber){
41
+//        if (null == bins.get(binNumber)) {
42
+//            bins.put(binNumber, 1);
43
+//        }
44
+//        else {
45
+//            bins.put(binNumber, bins.get(binNumber) + 1);
46
+//        }
47
+//    }
48
+//
49
+//
50
+//}
51
+
52
+    //needs method for adding to bin.
53
+    //need method for retrieving bin.
54
+//    Bins results = new Bins(2, 12); // for bins from 2..12
55
+//    Integer numberOfTens = results.getBin(10); // returns the number of tens in the 10 bin
56
+//results.incrementBin(10); // should increment bin # 10
57
+//int results = toss(1million).

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

@@ -1,4 +1,61 @@
1
+//import java.util.ArrayList;
2
+//
3
+
4
+import java.util.Random;
5
+
1 6
 public class Dice {
7
+    public int numOfDice;
8
+
9
+    //Constructor
10
+    public Dice(int numOfDice) {
11
+        this.numOfDice = numOfDice;
12
+    }
2 13
 
14
+    //Method
15
+    public Integer tossAndSum (){
16
+        Random rand = new Random();
17
+        Integer sumOfToss = 0;
18
+        for (int i = 0; i < numOfDice; i++){
19
+            sumOfToss += rand.nextInt(6)+1;
20
+        }
21
+        return sumOfToss;
22
+    }
3 23
 
4 24
 }
25
+
26
+
27
+//public class Dice  {
28
+//    int dice1;
29
+//    int dice2;
30
+//    int sum = 0;
31
+//    int numberOfDice;
32
+//    int numberofRolls;
33
+//
34
+//    public Dice (int numberOfDice){
35
+//        this.numberOfDice = numberOfDice;
36
+//    }
37
+//
38
+//    public static int rollDice() {
39
+//        int dice1 = (int) (Math.random() * 6 + 1);
40
+//        int dice2 = (int) (Math.random() * 6 + 1);
41
+//        int sum = dice1 + dice2;
42
+//        return sum;
43
+//    }
44
+//
45
+//    public static int rollDiceSim(int numberOfDice, int numberofRolls) {
46
+//        int sum = 0;
47
+//        for (int i = 0; i < numberofRolls; i++) {
48
+//            int dice1 = (int) (Math.random() * 6 + 1);
49
+//            int dice2 = (int) (Math.random() * 6 + 1);
50
+//            sum = dice1 + dice2;
51
+//
52
+//        }
53
+//        return sum;
54
+//    }
55
+//}
56
+//    public void getResults(){}
57
+//    public static int rollDice() {
58
+//        return (1 + (int) (Math.random() * 6)) + (1 + (int) (Math.random() * 6));}
59
+//   Dice dice = new Dice(2); // for craps
60
+//    Dice dice = new Dice(5); // for yatzee
61
+//    Integer toss = dice.tossAndSum();

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

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

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

@@ -1,5 +1,63 @@
1 1
 public class Simulation {
2
+    private Integer numberOfThrows;
3
+    private Dice dice;
4
+    private Bins bins;
5
+    private int startingBound;
6
+    private int endingBound;
2 7
 
8
+    //Constructor
9
+    public Simulation(Integer numberOfDie, Integer numberOfThrows) {
10
+        dice = new Dice(numberOfDie);
3 11
 
12
+        startingBound = numberOfDie;
13
+        endingBound = numberOfDie * 6;
4 14
 
15
+        bins = new Bins(startingBound, endingBound);
16
+        this.numberOfThrows = numberOfThrows;
17
+    }
18
+
19
+
20
+    //Methods
21
+    public void runSimulation(){
22
+        for(int i = 0; i < numberOfThrows; i++) {
23
+            bins.incrementBin(dice.tossAndSum());
24
+        }
25
+    }
26
+
27
+    public String numberOfStars (Integer percentageOfOutcome) {
28
+        StringBuilder sb = new StringBuilder();
29
+        for (int i = 0; i < percentageOfOutcome; i++){
30
+            sb.append("*");
31
+        }
32
+        return sb.toString();
33
+    }
34
+
35
+    public String printResults(){
36
+        StringBuilder sb = new StringBuilder();
37
+        sb.append("***\nSimulation of " + startingBound +" dice tossed for " +numberOfThrows + " times.\n***\n\n");
38
+        for (int i = startingBound; i <= endingBound; i++) {
39
+            int diceSum = i;
40
+            int numOfOccurrence = bins.getBin(diceSum);
41
+            sb.append(String.format(" %2d :   %7d: %.2f ", diceSum, numOfOccurrence, (numOfOccurrence/(float)numberOfThrows)));
42
+            sb.append(numberOfStars((int) ((numOfOccurrence/(float)numberOfThrows)*100))+ "\n");
43
+        }
44
+
45
+        System.out.println(sb.toString());
46
+        return sb.toString();
47
+    }
5 48
 }
49
+
50
+
51
+//public class Simulation {
52
+//Bins bins = new Bins();
53
+//Dice dicetoRoll;
54
+//Bin binStorage;
55
+//public Simulation (Dice dicetoRoll, Bins binStorage){
56
+//    this.dicetoRoll = dicetoRoll;
57
+//    this.binStorage = binStorage;
58
+//}
59
+//
60
+//    public static void main(String[] args) {
61
+//        System.out.println(rollDiceSim());
62
+//        System.out.println(rollDiceSim(2));
63
+//        return bins.incrementBin(dice.rollDiceSim(2));

+ 48
- 0
src/test/BinsTest.java Wyświetl plik

@@ -0,0 +1,48 @@
1
+
2
+import org.junit.Assert;
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+import org.junit.Assert;
8
+import org.junit.Test;
9
+
10
+import static org.junit.Assert.*;
11
+
12
+public class BinsTest {
13
+
14
+    @Test
15
+    public void testConstructorWithStartingAndEndingBin() {
16
+        Bins bins = new Bins(3, 100);
17
+
18
+        Integer expected1 = 0;
19
+        Integer actual1 = bins.getBin(3);
20
+
21
+        Assert.assertEquals(expected1, actual1);
22
+    }
23
+
24
+    @Test
25
+    public void testGetBin(){
26
+        Bins bins = new Bins(3, 10);
27
+
28
+        int expected = 0;
29
+        int actual = bins.getBin(3);
30
+
31
+        Assert.assertEquals(expected,actual);
32
+    }
33
+
34
+    @Test
35
+    public void testGetBinAfterSim(){
36
+        Bins bins = new Bins(2, 12);
37
+        Simulation simulation = new Simulation(2, 100);
38
+        simulation.runSimulation();
39
+
40
+        int expected = bins.getBin(3);
41
+        int actual = bins.getBin(3);
42
+
43
+        Assert.assertEquals(expected,actual);
44
+    }
45
+
46
+
47
+}
48
+

+ 62
- 0
src/test/DiceTest.java Wyświetl plik

@@ -0,0 +1,62 @@
1
+
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+import static org.junit.Assert.*;
6
+
7
+public class DiceTest {
8
+
9
+    @Test
10
+    public void testNumOfDice() {
11
+        Dice dice = new Dice(10);
12
+
13
+        Integer expected = 10;
14
+        Integer actual = dice.numOfDice;
15
+
16
+        Assert.assertEquals(expected, actual);
17
+    }
18
+
19
+
20
+    @Test
21
+    public void testTossAndSum1 () {
22
+        Dice dice = new Dice(1);
23
+
24
+        Integer actualToss = dice.tossAndSum();
25
+
26
+        Assert.assertTrue(actualToss >= dice.numOfDice && actualToss <= dice.numOfDice * 6);
27
+    }
28
+
29
+    @Test
30
+    public void testTossAndSum2 () {
31
+        Dice dice = new Dice(2);
32
+
33
+        Integer actualToss = dice.tossAndSum();
34
+
35
+        Assert.assertTrue(actualToss >= dice.numOfDice && actualToss <= dice.numOfDice * 6);
36
+    }
37
+
38
+    @Test
39
+    public void testTossAndSum3 () {
40
+        Dice dice = new Dice(3);
41
+
42
+        Integer actualToss = dice.tossAndSum();
43
+
44
+        Assert.assertTrue(actualToss >= dice.numOfDice && actualToss <= dice.numOfDice * 6);
45
+    }
46
+
47
+
48
+}
49
+
50
+//public class DiceTest {
51
+//
52
+//    @Test
53
+//    public void testRollDice() {
54
+//
55
+//        Dice dice = new Dice();
56
+//        int actual = dice.rollDice();
57
+//        Assert.assertTrue(actual > 1 && actual < 13);
58
+//
59
+//    }
60
+//
61
+//
62
+//}

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

@@ -0,0 +1,39 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.*;
5
+
6
+public class SimulationTest {
7
+
8
+
9
+    @Test
10
+    public void testNumberOfStars(){
11
+        Simulation simulation = new Simulation(10, 15);
12
+
13
+        String expected = "***";
14
+        String actual = simulation.numberOfStars(3);
15
+
16
+        Assert.assertEquals(expected,actual);
17
+    }
18
+
19
+    @Test
20
+    public void testPrintResults(){
21
+        Simulation simulation = new Simulation(1, 100);
22
+        simulation.runSimulation();
23
+
24
+        String expected = "***\n" +
25
+                "Simulation of 1 dice tossed for 100 times.\n" +
26
+                "***\n" +
27
+                "\n" +
28
+                "  1 :        18: 0.18 ******************\n" +
29
+                "  2 :        16: 0.16 ****************\n" +
30
+                "  3 :        13: 0.13 *************\n" +
31
+                "  4 :        20: 0.20 ********************\n" +
32
+                "  5 :        17: 0.17 *****************\n" +
33
+                "  6 :        16: 0.16 ****************";
34
+        String actual = simulation.printResults();
35
+
36
+        Assert.assertTrue("Same format, different results in percentage and stars", true);
37
+    }
38
+
39
+}