Browse Source

making progress

Keith Brinker 6 years ago
parent
commit
e8d48e76ba

+ 50
- 22
src/main/java/Bins.java View File

@@ -1,29 +1,57 @@
1
+import java.util.TreeMap;
1 2
 
2
-public class Bins extends Dice {
3
-//    Bins results = new Bins(2, 12); // for bins from 2..12
4
-//    Integer numberOfTens = results.getBin(10); // returns the number of tens in the 10 bin
5
-//results.incrementBin(10); // should increment bin # 10
6
-//int results = toss(1million).
3
+public class Bins {
4
+    private int startingBound;
5
+    private int endingBound;
6
+    private int[] rollSums;
7 7
 
8
-    int bin2 = 2;
9
-    int bin3 = 3;
10
-    int bin4 = 4;
11
-    int bin5 = 5;
12
-    int bin6;
13
-    int bin7;
14
-    int bin8;
15
-    int bin9;
16
-    int bin10;
17
-    int bin11;
18
-    int bin12;
19
-    int range;
20
-    int end;
21
-    public Bins(){ }
8
+    //Constructor
9
+    public Bins(int startingBound, int endingBound) {
10
+        rollSums = new int[endingBound+1];
11
+    }
22 12
 
23 13
 
14
+    //Methods
15
+    public int getBin(int value) {
16
+        return rollSums[value];
17
+    }
24 18
 
25
-
26
-//needs method for adding to bin.
27
-    //need method for retrieving bin.
19
+    public void incrementBin(int value) {
20
+        rollSums[value] = rollSums[value] + 1;
21
+    }
28 22
 
29 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).

+ 47
- 18
src/main/java/Dice.java View File

@@ -1,32 +1,61 @@
1
-import java.util.ArrayList;
1
+//import java.util.ArrayList;
2
+//
2 3
 
3
-public class Dice  {
4
-    int dice1;
5
-    int dice2;
6
-    int sum = 0;
7
-    public static int rollDice() {
8
-        int dice1 = (int) (Math.random() * 6 + 1);
9
-        int dice2 = (int) (Math.random() * 6 + 1);
10
-        int sum = dice1 + dice2;
11
-        return sum;
4
+import java.util.Random;
5
+
6
+public class Dice {
7
+    public int numOfDice;
8
+
9
+    //Constructor
10
+    public Dice(int numOfDice) {
11
+        this.numOfDice = numOfDice;
12 12
     }
13 13
 
14
-    public static int rollDiceSim(int rolls) {
15
-        int sum = 0;
16
-        for (int i = 0; i < rolls; i++) {
17
-            int dice1 = (int) (Math.random() * 6 + 1);
18
-            int dice2 = (int) (Math.random() * 6 + 1);
19
-            sum = dice1 + dice2;
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 20
         }
21
-        return sum;
21
+        return sumOfToss;
22 22
     }
23
+
23 24
 }
24 25
 
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
+//}
26 56
 //    public void getResults(){}
27 57
 //    public static int rollDice() {
28 58
 //        return (1 + (int) (Math.random() * 6)) + (1 + (int) (Math.random() * 6));}
29
-
30 59
 //   Dice dice = new Dice(2); // for craps
31 60
 //    Dice dice = new Dice(5); // for yatzee
32 61
 //    Integer toss = dice.tossAndSum();

+ 8
- 0
src/main/java/Main.java View File

@@ -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
- 13
src/main/java/Simulation.java View File

@@ -1,18 +1,63 @@
1
-import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
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
 
3
-public class Simulation extends Bins {
8
+    //Constructor
9
+    public Simulation(Integer numberOfDie, Integer numberOfThrows) {
10
+        dice = new Dice(numberOfDie);
4 11
 
12
+        startingBound = numberOfDie;
13
+        endingBound = numberOfDie * 6;
5 14
 
6
-    public static void main(String[] args) {
7
-        System.out.println(rollDice());
8
-        System.out.println(rollDiceSim(2));
9
-//        System.out.println(rollDice);
10
-//
11
-//        Simulation sim = new Simulation(2, 10000);
12
-//
13
-//        sim.runSimulation();
14
-//
15
-//        sim.printResults();
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
+    }
16 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();
17 33
     }
18
-}
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
+    }
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));

+ 37
- 2
src/test/BinsTest.java View File

@@ -1,13 +1,48 @@
1 1
 
2
+import org.junit.Assert;
2 3
 import org.junit.Test;
3 4
 
4 5
 import static org.junit.Assert.*;
5 6
 
6
-//@Test
7
+import org.junit.Assert;
8
+import org.junit.Test;
9
+
10
+import static org.junit.Assert.*;
7 11
 
8 12
 public class BinsTest {
9
- //@Test
10 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);
11 32
     }
12 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
+}
13 48
 

+ 46
- 11
src/test/DiceTest.java View File

@@ -1,27 +1,62 @@
1 1
 
2
+
2 3
 import org.junit.Assert;
3 4
 import org.junit.Test;
4
-import org.junit.runner.RunWith;
5
-
6 5
 import static org.junit.Assert.*;
7 6
 
8
-
9 7
 public class DiceTest {
10 8
 
11 9
     @Test
12
-    public void testRollDice() {
13
-            // Given
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();
14 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);
15 32
 
16
-            // When
17
-          //  ();
18
-           //  int actual = dice.rollDice(1);
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);
19 41
 
20
-            // Then
42
+        Integer actualToss = dice.tossAndSum();
21 43
 
22
-           // Assert.assertEquals(expected, actual);
23
-        }
44
+        Assert.assertTrue(actualToss >= dice.numOfDice && actualToss <= dice.numOfDice * 6);
45
+    }
24 46
 
25 47
 
26 48
 }
27 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 View File

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