Skip to content

Done this lab #53

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 1 commit 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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<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>


</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 @@
import java.util.Arrays;

public class Bins {
Integer[] bins;
Integer min;
Integer max;

public Bins(Integer min, Integer max) {
bins = new Integer[max - min + 1];
this.min = min;
this.max = max;
Arrays.fill(bins, 0);
}

public Integer getBinValue(Integer diceRoll){
return bins[diceRoll - min];
}

public Integer[] getBins(){
return bins;
}

public Integer incrementBins(Integer diceRoll){
return bins[diceRoll - min] ++;
}
}
27 changes: 27 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
import java.util.Random;

public class Dice {

int numberOfDice;


public Dice(int numberOfDice) {
this.numberOfDice = numberOfDice;
}

public int tossAndSum() {
int sumOfDice = 0;
for (int i = 0; i < numberOfDice; i++) {
sumOfDice += rollDie();
}
return sumOfDice;
}

private int rollDie() {
return (int) (Math.random() * 6 + 1);
}

public int getNumberOfDice(){
return numberOfDice;
}
public void setNumberOfDice(int numberOfDice) {
this.numberOfDice = numberOfDice;
}

}
47 changes: 47 additions & 0 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
public class Simulation {

Integer numberOfDice;
Integer numberOfTosses;
Dice dice;
Bins bins;

public Simulation(Integer numberOfDice, Integer numberOfTosses) {
this.numberOfDice = numberOfDice;
this.numberOfTosses = numberOfTosses;
}

public void runSimulation() {
this.dice = new Dice(2);
this.bins = new Bins(2, 12);

for (int i = 0; i < numberOfTosses; i++) {
Integer sumUp = dice.tossAndSum();
bins.incrementBins(sumUp);
}
System.out.println("");
}

public void printResults() {
for(Integer i = 2; i <= 12; i++) {
Double percentage = (double) bins.getBinValue(i) / numberOfTosses;
System.out.printf("%2d : %7d: %.2f ", i, bins.getBinValue(i), percentage);
for(int j = 1; j < percentage * 100; j++)
{
System.out.print("*");
}
System.out.println();
}
}


public double getPercentage(Integer numberOfTosses, Bins bins) {
double percentage = 0;
for( int element : bins.getBins()) {
percentage = element/ numberOfTosses;
}
return percentage;
}

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


}
2 changes: 2 additions & 0 deletions src/test/java/DiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class DiceTest {
}