#14 nafisn

Ouvert
nafisn veut fusionner 9 révision(s) depuis nafisn/ZCW-DiceyLab:master vers master

BIN
.DS_Store Voir le fichier


BIN
DiceyLab/Bins.class Voir le fichier


+ 11
- 0
DiceyLab/Bins.ctxt Voir le fichier

@@ -0,0 +1,11 @@
1
+#BlueJ class context
2
+comment0.target=Bins
3
+comment1.params=
4
+comment1.target=Bins()
5
+comment2.params=
6
+comment2.target=java.lang.Integer\ getTens()
7
+comment3.params=key
8
+comment3.target=void\ increment(java.lang.Integer)
9
+comment4.params=key
10
+comment4.target=java.lang.Integer\ getN(int)
11
+numComments=5

+ 35
- 0
DiceyLab/Bins.java Voir le fichier

@@ -0,0 +1,35 @@
1
+import java.util.TreeMap;
2
+public class Bins
3
+{
4
+    public TreeMap<Integer, Integer> bins = new TreeMap<Integer,Integer>(); 
5
+
6
+    public Bins()
7
+    {
8
+        bins.put(2,0);
9
+        bins.put(3,0);
10
+        bins.put(4,0);
11
+        bins.put(5,0);
12
+        bins.put(6,0);
13
+        bins.put(7,0);
14
+        bins.put(8,0);
15
+        bins.put(9,0);
16
+        bins.put(10,0);
17
+        bins.put(11,0);
18
+        bins.put(12,0);
19
+    }
20
+
21
+    public Integer getTens(){
22
+        return bins.get(10);
23
+    }
24
+
25
+    public void increment(Integer key){
26
+        Integer count = bins.get(key);
27
+        bins.put(key, count + 1);
28
+    }
29
+    
30
+    public Integer getN(int key){
31
+        return bins.get(key);
32
+    }
33
+
34
+
35
+}

BIN
DiceyLab/BinsTest.class Voir le fichier


+ 9
- 0
DiceyLab/BinsTest.ctxt Voir le fichier

@@ -0,0 +1,9 @@
1
+#BlueJ class context
2
+comment0.target=BinsTest
3
+comment1.params=
4
+comment1.target=void\ setUp()
5
+comment2.params=
6
+comment2.target=void\ testGetBin()
7
+comment3.params=
8
+comment3.target=void\ testIncrementBin()
9
+numComments=4

+ 36
- 0
DiceyLab/BinsTest.java Voir le fichier

@@ -0,0 +1,36 @@
1
+
2
+
3
+import static org.junit.Assert.*;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+
9
+public class BinsTest
10
+{
11
+
12
+    @Before
13
+    public void setUp()
14
+    {   
15
+    }
16
+
17
+    @Test
18
+    public void testGetBin(){
19
+        Bins test = new Bins();
20
+        
21
+        Integer expected = 0;
22
+        Integer actual = test.getTens();
23
+        assertEquals(expected, actual);
24
+    }
25
+    
26
+    @Test
27
+    public void testIncrementBin(){
28
+        Bins test = new Bins();
29
+        
30
+        Integer expected = 1;
31
+        test.increment(10);
32
+        Integer actual = test.getTens();
33
+        assertEquals(expected, actual);
34
+    }
35
+    
36
+}

BIN
DiceyLab/Dice.class Voir le fichier


+ 11
- 0
DiceyLab/Dice.ctxt Voir le fichier

@@ -0,0 +1,11 @@
1
+#BlueJ class context
2
+comment0.target=Dice
3
+comment1.params=
4
+comment1.target=Dice()
5
+comment2.params=numberOfDice
6
+comment2.target=Dice(int)
7
+comment3.params=
8
+comment3.target=java.lang.Integer\ roll()
9
+comment4.params=numberOfRolls
10
+comment4.target=java.lang.Integer\ roll(int)
11
+numComments=5

+ 33
- 0
DiceyLab/Dice.java Voir le fichier

@@ -0,0 +1,33 @@
1
+
2
+public class Dice
3
+{
4
+    private int numberOfDice = 1;
5
+
6
+    public Dice()
7
+    {
8
+    }
9
+
10
+    public Dice (int numberOfDice){
11
+        this.numberOfDice = numberOfDice;    
12
+    }
13
+
14
+    public Integer roll(){ 
15
+        int counter = numberOfDice;
16
+        Integer sum = 0;
17
+        while (counter > 0){
18
+            sum += (int)(Math.random()*6)+1;
19
+            counter--;
20
+        }
21
+        return sum;
22
+    }
23
+
24
+    public Integer roll(int numberOfRolls)
25
+    {
26
+        Integer sum = 0;
27
+        while (numberOfRolls > 0){
28
+            sum += roll();
29
+            numberOfRolls--;
30
+        }
31
+        return sum;
32
+    }
33
+}

BIN
DiceyLab/DiceTest.class Voir le fichier


+ 15
- 0
DiceyLab/DiceTest.ctxt Voir le fichier

@@ -0,0 +1,15 @@
1
+#BlueJ class context
2
+comment0.target=DiceTest
3
+comment1.params=
4
+comment1.target=void\ setUp()
5
+comment2.params=
6
+comment2.target=void\ rollDiceTest()
7
+comment3.params=
8
+comment3.target=void\ rollTwoDiceTest()
9
+comment4.params=
10
+comment4.target=void\ rollTwoDiceTwiceTest()
11
+comment5.params=
12
+comment5.target=void\ rollThreeDiceTest()
13
+comment6.params=
14
+comment6.target=void\ rollThreeDiceTwiceTest()
15
+numComments=7

+ 58
- 0
DiceyLab/DiceTest.java Voir le fichier

@@ -0,0 +1,58 @@
1
+
2
+
3
+import static org.junit.Assert.*;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+
9
+public class DiceTest
10
+{
11
+
12
+    @Before
13
+    public void setUp()
14
+    {
15
+    }
16
+    
17
+    @Test
18
+    public void rollDiceTest(){
19
+        Dice dice = new Dice();
20
+        Integer given = dice.roll();
21
+        Integer min = 1;
22
+        Integer max = 6;
23
+        assertTrue(min <= given && given <= max );    
24
+    }
25
+    
26
+    @Test
27
+    public void rollTwoDiceTest(){
28
+        Dice dice = new Dice(2);
29
+        Integer expected = 6;         
30
+        Integer given = dice.roll();
31
+        assertEquals(expected, given, 6);    
32
+    }
33
+    
34
+    @Test
35
+    public void rollTwoDiceTwiceTest(){
36
+        Dice dice = new Dice(2);
37
+        Integer expected = 14;
38
+        Integer given = dice.roll(2);
39
+        assertEquals(expected, given, 10);        
40
+    }
41
+    
42
+    @Test
43
+    public void rollThreeDiceTest(){
44
+        Dice dice = new Dice(3);
45
+        Integer expected = 10;
46
+        Integer given = dice.roll();
47
+        assertEquals(expected, given, 7.5);        
48
+    }
49
+    
50
+    @Test
51
+    public void rollThreeDiceTwiceTest(){
52
+        Dice dice = new Dice(3);
53
+        Integer expected = 21;
54
+        Integer given = dice.roll(2);
55
+        assertEquals(expected, given, 15);        
56
+    }
57
+
58
+}

+ 12
- 0
DiceyLab/README.TXT Voir le fichier

@@ -0,0 +1,12 @@
1
+------------------------------------------------------------------------
2
+This is the project README file. Here, you should describe your project.
3
+Tell the reader (someone who does not know anything about this project)
4
+all he/she needs to know. The comments should usually include at least:
5
+------------------------------------------------------------------------
6
+
7
+PROJECT TITLE:
8
+PURPOSE OF PROJECT:
9
+VERSION or DATE:
10
+HOW TO START THIS PROJECT:
11
+AUTHORS:
12
+USER INSTRUCTIONS:

BIN
DiceyLab/Simulation.class Voir le fichier


+ 9
- 0
DiceyLab/Simulation.ctxt Voir le fichier

@@ -0,0 +1,9 @@
1
+#BlueJ class context
2
+comment0.target=Simulation
3
+comment1.params=numberOfDice\ numberOfRolls
4
+comment1.target=Simulation(int,\ int)
5
+comment2.params=
6
+comment2.target=void\ runSimulation()
7
+comment3.params=bin
8
+comment3.target=void\ printResults(Bins)
9
+numComments=4

+ 46
- 0
DiceyLab/Simulation.java Voir le fichier

@@ -0,0 +1,46 @@
1
+import java.util.TreeMap;
2
+public class Simulation
3
+{
4
+    private int numberOfDice;
5
+    private int numberOfRolls;
6
+
7
+    public Simulation(int numberOfDice, int numberOfRolls){
8
+        this.numberOfDice = numberOfDice;
9
+        this.numberOfRolls = numberOfRolls;
10
+    }
11
+
12
+    public void runSimulation(){
13
+        Simulation sims = new Simulation(2, numberOfRolls);
14
+        Dice dice = new Dice(numberOfDice);
15
+        Bins results = new Bins();
16
+
17
+        int currentRoll = 0;
18
+        while(currentRoll < numberOfRolls){
19
+            int result = dice.roll();
20
+            currentRoll++;
21
+            results.increment(result);
22
+        }
23
+        printResults(results);
24
+    }
25
+
26
+    public void printResults(Bins bin){
27
+        System.out.println("********************************\n" +
28
+                           " Rolling 2 Dice 1,000,000 times \n" +
29
+                           "********************************");
30
+        for (int i = 2; i <= 12; i++){
31
+            StringBuilder stars = new StringBuilder();
32
+            Float percent = (bin.getN(i) /1000000f);
33
+            double uh = (float)(((int)(Math.pow(10,2)*percent))/Math.pow(10,2));
34
+            
35
+            while(uh > 0){
36
+                stars.append("*");
37
+                uh -= 0.01;
38
+            }
39
+            String s = stars.toString();
40
+            
41
+            System.out.println(String.format("%2s : %7s : %2.2f : %s", i, bin.getN(i), percent,s));
42
+        }
43
+        
44
+    }
45
+
46
+}

+ 42
- 0
DiceyLab/package.bluej Voir le fichier

@@ -0,0 +1,42 @@
1
+#BlueJ package file
2
+dependency1.from=DiceTest
3
+dependency1.to=Dice
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=682
6
+editor.fx.0.width=800
7
+editor.fx.0.x=240
8
+editor.fx.0.y=23
9
+objectbench.height=101
10
+objectbench.width=776
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.8007380073800738
13
+package.editor.height=427
14
+package.editor.width=674
15
+package.editor.x=302
16
+package.editor.y=112
17
+package.frame.height=600
18
+package.frame.width=800
19
+package.numDependencies=1
20
+package.numTargets=2
21
+package.showExtends=true
22
+package.showUses=true
23
+project.charset=UTF-8
24
+readme.height=58
25
+readme.name=@README
26
+readme.width=47
27
+readme.x=10
28
+readme.y=10
29
+target1.height=50
30
+target1.name=Dice
31
+target1.showInterface=false
32
+target1.type=ClassTarget
33
+target1.width=80
34
+target1.x=211
35
+target1.y=171
36
+target2.height=50
37
+target2.name=DiceTest
38
+target2.showInterface=false
39
+target2.type=UnitTestTargetJunit4
40
+target2.width=80
41
+target2.x=261
42
+target2.y=148

+ 14
- 0
simresults Voir le fichier

@@ -0,0 +1,14 @@
1
+********************************
2
+ Rolling 2 Dice 1,000,000 times
3
+********************************
4
+ 2 :   27834 : 0.03 : **
5
+ 3 :   55943 : 0.06 : ******
6
+ 4 :   83687 : 0.08 : ********
7
+ 5 :  110414 : 0.11 : ***********
8
+ 6 :  138788 : 0.14 : *************
9
+ 7 :  166853 : 0.17 : ****************
10
+ 8 :  139144 : 0.14 : *************
11
+ 9 :  110972 : 0.11 : ***********
12
+10 :   83185 : 0.08 : ********
13
+11 :   55401 : 0.06 : ******
14
+12 :   27779 : 0.03 : **