Selaa lähdekoodia

completed all classes and tests and out printed result

mpierse 6 vuotta sitten
vanhempi
commit
23eeaa5c24

+ 15
- 7
src/main/java/Bins.java Näytä tiedosto

@@ -1,17 +1,22 @@
1 1
 
2 2
 public class Bins {
3 3
 
4
-    private int diceNum = 0;
4
+    private int diceNum;
5 5
     private int[] values;
6 6
 
7
-    public Bins(int diceNum){
8
-        values = new int[(diceNum*6)-(diceNum)];
9
-        for (int i=0; i<values.length; i++){
10
-            values[i] = 0;
11
-        }
7
+    public Bins(int diceNum) {
12 8
         this.diceNum = diceNum;
9
+        doLogic(diceNum);
13 10
     }
14 11
 
12
+    public void doLogic(int diceNum) {
13
+        values = new int[(diceNum * 6) - (diceNum)+1];
14
+
15
+
16
+        for (int i = 0; i < values.length; i++) {
17
+            values[i] = 0;
18
+        }
19
+    }
15 20
 
16 21
 
17 22
     public void incrementBin(int binNumber) {
@@ -19,6 +24,9 @@ public class Bins {
19 24
     }
20 25
 
21 26
     public int getBin(int binNumber) {
22
-        return values[binNumber-diceNum];
27
+//        System.out.print(values[(binNumber-diceNum)]);
28
+        int test = values[(binNumber-diceNum)];
29
+        return test;
30
+
23 31
     }
24 32
 }

+ 2
- 2
src/main/java/BinsTest.java Näytä tiedosto

@@ -21,11 +21,11 @@ public class BinsTest {
21 21
         //given
22 22
         Bins bin2 = new Bins(2);
23 23
         for(int i=0; i<101; i++) {
24
-            bin2.incrementBin(7);
24
+            bin2.incrementBin(10);
25 25
         }
26 26
         //when
27 27
         int expected = 101;
28
-        int actual = bin2.getBin(7);
28
+        int actual = bin2.getBin(10);
29 29
         //then
30 30
         assertEquals(expected, actual);
31 31
     }

+ 4
- 1
src/main/java/DiceTest.java Näytä tiedosto

@@ -1,6 +1,8 @@
1 1
 import org.junit.Assert;
2 2
 import org.junit.Test;
3 3
 
4
+import static org.junit.Assert.assertTrue;
5
+
4 6
 public class DiceTest {
5 7
     @Test
6 8
     public void dieIsValid(){
@@ -16,7 +18,8 @@ public class DiceTest {
16 18
         }
17 19
 
18 20
         // Then
19
-        Assert.assertEquals(expected, actual);
21
+//        Assert.assertEquals(expected, actual);
22
+        assertTrue(actual);
20 23
 
21 24
     }
22 25
 

+ 8
- 0
src/main/java/Main.java Näytä tiedosto

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

+ 18
- 12
src/main/java/Simulation.java Näytä tiedosto

@@ -2,16 +2,16 @@ import java.util.Formatter;
2 2
 
3 3
 public class Simulation {
4 4
 
5
-    private int diceNum=0;
6
-    private int tossNum=0;
7
-    private Dice dice = new Dice(diceNum);
8
-    private Bins bin = new Bins(diceNum);
5
+    private int diceNum;
6
+    private int tossNum;
7
+    private Dice dice;
8
+    private Bins bin;
9 9
 
10 10
     public Simulation(int diceNum, int tossNum){
11 11
         this.diceNum = diceNum;
12 12
         this.tossNum = tossNum;
13
-        this.bin = bin;
14
-        this.dice = dice;
13
+        this.bin = new Bins(diceNum);
14
+        this.dice = new Dice(diceNum);
15 15
 }
16 16
 
17 17
     public void runSimulation(){
@@ -20,8 +20,8 @@ public class Simulation {
20 20
         }
21 21
 }
22 22
 
23
-    public void printSimulation() {
24
-
23
+    public String printSimulation() {
24
+        runSimulation();
25 25
         StringBuilder result = new StringBuilder();
26 26
         Formatter format = new Formatter(result);
27 27
 
@@ -30,16 +30,22 @@ public class Simulation {
30 30
         result.append(" dice tossed for ");
31 31
         result.append(tossNum);
32 32
         result.append(" times.\n*** \n \n");
33
-        for (int i=0; i<((diceNum*6)-(diceNum)); i++) {
34
-            result.append(i+diceNum);
33
+        for (int i=0; i<((diceNum*6)-(diceNum)+1); i++) {
34
+            int numTimesThrown = bin.getBin(i+diceNum);
35
+            format.format("%2d", i+diceNum);
35 36
             result.append(" : ");
36 37
             format.format("%10d", bin.getBin(i+diceNum));
37 38
             result.append(": ");
38
-            format.format("%.2f", (double)((bin.getBin(i+diceNum))/tossNum));
39
+            format.format("%.2f", (double)numTimesThrown/(double)tossNum);
40
+             for(int j=0; j<(((double)numTimesThrown/(double)tossNum)*100); j++){
41
+
42
+                 {result.append(" *");}
43
+             }
44
+            result.append("\n");
39 45
 
40 46
         }
41 47
         String printAns = result.toString();
42
-        System.out.print(printAns);
48
+        return printAns;
43 49
     }
44 50
 
45 51