Skip to content

Dicey Lab complete #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions DeesResults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Simulation of 2 Dice tossed for 1000000 times

2 : 27922 : 0.03 **
3 : 56240 : 0.06 *****
4 : 83366 : 0.08 ********
5 : 111484 : 0.11 ***********
6 : 139353 : 0.14 *************
7 : 166432 : 0.17 ****************
8 : 138708 : 0.14 *************
9 : 110307 : 0.11 ***********
10 : 82890 : 0.08 ********
11 : 55627 : 0.06 *****
12 : 27671 : 0.03 **
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>Dicey-Lab</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
22 changes: 22 additions & 0 deletions src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@

public class Bins {

private int min;
private int max;
Integer[] sumOfRolls;

public Bins(int min, int max){
this.min = min;
this.max = max;
this.sumOfRolls = new Integer[(max - min) + 1];
for (int i = 0; i < sumOfRolls.length; i++) {
sumOfRolls[i] = 0;
}
}

public Integer getBin(int oneSum){
int index = oneSum-min;
return sumOfRolls[index];
}

public void incrementBin(int sameSum){
int index = sameSum-min;
sumOfRolls[index]++;
}
}
18 changes: 18 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
public class Dice {

public static void main(String[] args) {
Dice dice = new Dice(2);
Integer testInt = dice.tossAndSum();
System.out.println(testInt);
}
private int numberOfDie;

public Dice(int numOfDie){
this.numberOfDie = numOfDie;
}

public int tossAndSum(){
int sum = 0;
for (int i = 0; i < numberOfDie; i++) {
sum += ((Math.random()*6)+1);
} // 0.0000001 - 0.9999999

return sum;
}
}
46 changes: 46 additions & 0 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
import java.util.Arrays;

public class Simulation {

public static void main(String[] args) {
Simulation sim = new Simulation(2, 1000000);
sim.runSimulation();
sim.printResults();

}
int numberOfDie;
int numberOfRolls;
Integer[] results;

public Simulation(int numberOfDie, int rolls){
this.numberOfDie = numberOfDie;
this.numberOfRolls = rolls;
}

public Integer[] runSimulation(){

Dice simDice = new Dice(this.numberOfDie);
Bins testBin = new Bins(this.numberOfDie,(this.numberOfDie*6));

for (int i = 0; i < this.numberOfRolls; i++) {
int simSum = simDice.tossAndSum();
testBin.incrementBin(simSum);
}
return testBin.sumOfRolls;
}

public void printResults() {
Integer[] results = runSimulation();

for (int i = 0; i < results.length; i++) {
int index = i+2;
int freq = results[i];
float percent = (float) results[i] / this.numberOfRolls;
int asterickValue = (int) (percent * 100); //casting takes precedence over order of operations
System.out.printf("%2d : %8d : %.2f ", index, freq, percent);
for (int j = 0; j < asterickValue; j++) {
System.out.print("*");
}
System.out.println();
}

}



}
63 changes: 63 additions & 0 deletions src/test/java/BinTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;

public class BinTest {

@Test
public void testBinConstructor(){
//given
int testMin = 2;
int testMax = 12;
int expected = (testMax - testMin) + 1;

//when
Bins test = new Bins(testMin, testMax);
int actualLength = test.sumOfRolls.length;
int actualValue = test.sumOfRolls[1];

//then
Assert.assertEquals(expected, actualLength);
Assert.assertEquals(0, actualValue);

}

@Test
public void testBinIncrement(){
//given
Bins test = new Bins(2, 12);
int testSum = 7;

//when

test.incrementBin(testSum);

//then
int actual = test.getBin(testSum);
String testString = Arrays.toString(test.sumOfRolls);
//Do not use String.valueOf for Integer ARRAYS!
System.out.println(testString);
Assert.assertEquals(1, actual);

}

@Test
public void testBinIncrement2() {
//given
Bins test = new Bins(2, 12);
int testSum = 7;

//when

test.incrementBin(testSum);
test.incrementBin(testSum);

//then
int actual = test.getBin(testSum);
String testString = Arrays.toString(test.sumOfRolls);
//Do not use String.valueOf for Integer ARRAYS!
System.out.println(testString);
Assert.assertEquals(2, actual);
}
}
19 changes: 19 additions & 0 deletions src/test/java/DiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import org.junit.Assert;
import org.junit.Test;

public class DiceTest {

@Test
public void testDiceConstructor(){
//given
int numOfDie = 2;
Dice dice = new Dice(numOfDie);

//when
Integer testInt = dice.tossAndSum();

//then
System.out.println(testInt); //should be a number between 2 and 12
Assert.assertTrue(testInt < 13 && testInt > 1);
}
}