Bläddra i källkod

Finished the Lab

Saurav Kamath 6 år sedan
förälder
incheckning
5f2c0eff70
9 ändrade filer med 189 tillägg och 0 borttagningar
  1. Binär
      .DS_Store
  2. 9
    0
      pom.xml
  3. Binär
      src/.DS_Store
  4. 60
    0
      src/Test/java/DiceTest.java
  5. Binär
      src/main/.DS_Store
  6. 47
    0
      src/main/java/Bins.java
  7. 29
    0
      src/main/java/Dice.java
  8. 34
    0
      src/main/java/Simulation.java
  9. 10
    0
      src/main/java/main.java

Binär
.DS_Store Visa fil


+ 9
- 0
pom.xml Visa fil

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

Binär
src/.DS_Store Visa fil


+ 60
- 0
src/Test/java/DiceTest.java Visa fil

@@ -0,0 +1,60 @@
1
+import org.junit.*;
2
+
3
+public class DiceTest {
4
+
5
+    @Test
6
+    public void diceTossTest(){
7
+        //Given
8
+        Dice die = new Dice(3);
9
+        boolean actual;
10
+
11
+        //When
12
+        int check = die.tossAndSum();
13
+        if (check >= 3 && check <= 18){
14
+            actual = true;
15
+        }
16
+        else{
17
+            actual = false;
18
+        }
19
+
20
+        //Then
21
+        Assert.assertTrue(actual);
22
+
23
+    }
24
+
25
+    @Test
26
+    public void diceTossTest2(){
27
+        //Given
28
+        Dice die = new Dice(1);
29
+        boolean actual;
30
+
31
+        //When
32
+        int check = die.tossAndSum();
33
+        if (check >= 10 && check <= 60){
34
+            actual = true;
35
+        }
36
+        else{
37
+            actual = false;
38
+        }
39
+
40
+        //Then
41
+        Assert.assertFalse(actual);
42
+
43
+    }
44
+    @Test
45
+    public void getBinTest(){
46
+        //Given
47
+        Dice die = new Dice(2);
48
+        Bins results = new Bins(2, 12);
49
+        Integer expected = 1;
50
+
51
+        //When
52
+        results.incrementBin(10);
53
+        Integer actual = results.getBin(10);
54
+
55
+
56
+        //Then
57
+        Assert.assertEquals(expected, actual);
58
+
59
+    }
60
+}

Binär
src/main/.DS_Store Visa fil


+ 47
- 0
src/main/java/Bins.java Visa fil

@@ -1,4 +1,51 @@
1
+import sun.rmi.server.InactiveGroupException;
2
+
3
+import java.util.*;
4
+
1 5
 
2 6
 public class Bins {
7
+    private int lowerBound;
8
+    private int upperBound;
9
+    private Map<Integer, Integer> resultList = new HashMap<Integer, Integer>();
10
+
11
+
12
+    public Bins() {
13
+
14
+    }
15
+    public Bins(int lowerBound, int upperBound) {
16
+        this.lowerBound = lowerBound;
17
+        this.upperBound = upperBound;
18
+        for (int i = lowerBound; i <= upperBound; i++) {
19
+
20
+            resultList.put(i,0);
21
+        }
22
+    }
23
+
24
+    public Map<Integer, Integer> getResultList() {
25
+
26
+        return resultList;
27
+    }
28
+
29
+    public void incrementBin(Integer binNumber) {
30
+
31
+        Integer valueAtBinNumber = resultList.get(binNumber);
32
+        resultList.put(binNumber, valueAtBinNumber + 1);
33
+    }
34
+    public Integer getLowerBound(){
35
+        return lowerBound;
36
+    }
37
+
38
+    public Integer getUpperBound(){
39
+        return upperBound;
40
+    }
41
+
42
+    public Integer getBin(Integer binNumber){
43
+        return resultList.get(binNumber);
44
+    }
45
+
46
+    public void dispBin(){
47
+        System.out.println(resultList.toString());
48
+
49
+    }
3 50
 
4 51
 }

+ 29
- 0
src/main/java/Dice.java Visa fil

@@ -1,4 +1,33 @@
1
+import java.util.*;
2
+
1 3
 public class Dice {
4
+    private Random rand = new Random();
5
+    private Integer numberOfDice;
6
+
7
+    public Dice(){
8
+
9
+    }
10
+
11
+    public Dice(Integer numberOfDice) {
12
+        this.numberOfDice = numberOfDice;
13
+    }
14
+
15
+    public Integer getNumberOfDice() {
16
+        return numberOfDice;
17
+    }
18
+
19
+    public void setNumberOfDice(Integer numberOfDice) {
20
+        this.numberOfDice = numberOfDice;
21
+    }
22
+
23
+    public Integer tossAndSum(){
24
+        Integer sum = 0;
25
+        for(int i = 0; i < numberOfDice; i++){
26
+            Integer diceValue = rand.nextInt(6) + 1;
27
+            sum += diceValue;
28
+        }
29
+        return  sum;
30
+    }
2 31
 
3 32
 
4 33
 }

+ 34
- 0
src/main/java/Simulation.java Visa fil

@@ -1,5 +1,39 @@
1
+
1 2
 public class Simulation {
3
+Integer numberOfDie;
4
+Integer numberOfThrows;
5
+Bins storage;
6
+Dice die;
7
+    public Simulation(Integer numberOfDie, Integer numberOfThrows) {
8
+        this.numberOfDie = numberOfDie;
9
+        this.numberOfThrows = numberOfThrows;
10
+        this.storage = new Bins(numberOfDie, (numberOfDie*6));
11
+        this.die = new Dice(numberOfDie);
12
+    }
13
+
14
+    public void runSimulation(){
15
+        for (Integer i = 0; i < numberOfThrows; i++){
16
+
17
+            if(storage.getResultList().containsKey(die.tossAndSum())){
18
+                storage.incrementBin(die.tossAndSum());
19
+            }
20
+        }
21
+        storage.dispBin();
22
+    }
23
+    public void printResults(){
24
+
25
+        for(Integer i = storage.getLowerBound(); i <= storage.getUpperBound(); i++){
26
+            double elementValue = storage.getBin(i);
27
+            double portion = (elementValue/numberOfThrows);
28
+            System.out.printf("%3s: %7s: %.2f: ", i.toString(), storage.getBin(i), portion);
29
+            for(int j = 0; j < portion*100; j++){
30
+                System.out.print("*");
31
+            }
32
+            System.out.print("\n");
33
+
34
+        }
2 35
 
36
+    }
3 37
 
4 38
 
5 39
 }

+ 10
- 0
src/main/java/main.java Visa fil

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