Skip to content

Code compiled and run as expected #43

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 5 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
19 changes: 19 additions & 0 deletions NathanResults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
##Nathan's Result
Code compiled and gave expected output
```java
***
Simulation of 2 dice tossed for 1000000 times.
***
2 : 27470: 0.03 ***
3 : 55425: 0.06 ******
4 : 83023: 0.08 ********
5 : 110886: 0.11 ***********
6 : 139311: 0.14 ***************
7 : 166757: 0.17 *****************
8 : 139440: 0.14 ***************
9 : 110721: 0.11 ***********
10 : 83396: 0.08 ********
11 : 55470: 0.06 ******
12 : 28101: 0.03 ***
```
~yay~
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>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
41 changes: 41 additions & 0 deletions src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;

public class Bins {

private Integer[] boxes;
private final Integer min;
private final Integer max;


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

public void fillBins(Integer number){
int index = number - min;
boxes[index]++;
}

public Integer getBin(int binIndex) {
return boxes[binIndex - min];
}

public void incrementBin(int binIndex) {
boxes[binIndex - min]++;
}

public double getPercent(Integer boxOfResult){
Integer sum = 0;
for(Integer number: boxes){
sum += number;
}
double raw = (double) this.getBin(boxOfResult)/sum;
BigDecimal rounded = new BigDecimal(raw).setScale(2, RoundingMode.HALF_EVEN);
double result = rounded.doubleValue();
return result;
}
}
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.ArrayList;

public class Dice {
private ArrayList<Integer> rollResults;

public Dice(Integer countOfDice) {
this.rollResults = new ArrayList<>();
for (int i = 0; i < countOfDice; i++) {
this.rollResults.add((int) ((Math.random() * (7 - 1)) + 1));
}
}

public ArrayList<Integer> getDiceList() {
return rollResults;
}

public void toss(){
for (int i = 0; i < rollResults.size(); i++) {
rollResults.set(i, (int) ((Math.random() * (7 - 1)) + 1));
}
}

public Integer tossAndSum() {
Integer sum = 0;
for (int i = 0; i < rollResults.size(); i++) {
rollResults.set(i, (int) ((Math.random() * (7 - 1)) + 1));
sum += rollResults.get(i);
}
return sum;
}
}
9 changes: 9 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Main {

public static void main(String[] args) {
Simulation newSim = new Simulation(2, 1000000);
newSim.runSim();
newSim.printResult();

}
}
65 changes: 65 additions & 0 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
import java.math.BigDecimal;

public class Simulation {

private Dice dice;
private Bins bin;
private Integer numOfToss;
private Integer numOfDice;
private Integer min;

public Simulation(Integer numOfDice, Integer numOfToss){
this.numOfToss = numOfToss;
this.numOfDice = numOfDice;
this.min = numOfDice;
this.dice = new Dice(numOfDice);
this.bin = new Bins(numOfDice, numOfDice * 6);
}

public void runSim(){
for (int i = 0; i < numOfToss; i++) {
bin.fillBins(dice.tossAndSum());
}
}

public void printResult(){
//Heading
String result = "***\n" +
"Simulation of " + numOfDice + " dice tossed for " + numOfToss+ " times.\n" +
"***\n";
//Iterating through each box and constructing string of boxNumber, values, percentage, stars.
Integer[] thisBin = bin.getBoxes();
for(int i = 0; i < thisBin.length; i++) {
int boxId = i + min;
String addToResult = getBoxNumber(boxId) + " :" + getBoxValue(bin.getBin(boxId)) + ": " + bin.getPercent(boxId) +
" " + getStars(bin.getPercent(boxId)) + "\n";
result += addToResult;
}
System.out.println(result);
}

private String getBoxNumber(Integer boxID){
String result = ""; //3 spaces
Integer whiteSpaces = 3 - boxID.toString().length();
for (int j = 0; j < whiteSpaces; j++) {
result += " ";
}
result += boxID.toString();
return result;
}

private String getBoxValue(Integer value){
String result = ""; //9 spaces
Integer spaces = 9 - value.toString().length();
for (int i = 0; i < spaces; i++) {
result += " ";
}
result += value.toString();
return result;
}

public String getStars(double number){
StringBuilder result = new StringBuilder();
double numOfStars = number * 100;
for (int i = 0; i < numOfStars; i++) {
result.append("*");
}
return result.toString();
}

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

import java.math.BigDecimal;
import java.util.Arrays;

public class BinsTest {
@Test
public void binsTest(){
//given
Integer min = 2;
Integer max = 12;
//when
Bins bin = new Bins(min, max);
//then
System.out.println(Arrays.toString(bin.getBoxes()));
}

@Test
public void fillBinsTest(){
//given
Bins bin = new Bins(2, 12);
Dice dice = new Dice(2);
//when
for (int i = 0; i < 1000000; i++) {
Integer input = dice.tossAndSum();
bin.fillBins(input);
}
String result = Arrays.toString(bin.getBoxes());
//then -check by looking at
System.out.println(result);
}

@Test
public void getBinTest(){
//given
Bins bin = new Bins(2, 12);
Dice dice = new Dice(2);
//when
for (int i = 0; i < 1000; i++) {
Integer input = dice.tossAndSum();
bin.fillBins(input);
}
String wholeBin = Arrays.toString(bin.getBoxes());
Integer bin5 = bin.getBin(5);
Integer bin12 = bin.getBin(12);
//Then
System.out.println(wholeBin);
System.out.println(bin5);
System.out.println(bin12);

}

@Test
public void incrementBinTest(){
//given
Bins bin = new Bins(2, 12);
Dice dice = new Dice(2);
//when
for (int i = 0; i < 1000; i++) {
Integer input = dice.tossAndSum();
bin.fillBins(input);
}
String wholeBin = Arrays.toString(bin.getBoxes());
Integer bin12 = bin.getBin(12);
bin.incrementBin(12);
Integer bin12AfterIncrement = bin.getBin(12);

//Then
System.out.println(wholeBin);
System.out.println(bin12);
System.out.println(bin12AfterIncrement);

}

@Test
public void getPercentTest(){
//given
Bins bin = new Bins(2, 12);
Dice dice = new Dice(2);
for (int i = 0; i < 1000; i++) {
Integer input = dice.tossAndSum();
bin.fillBins(input);
}
String wholeBin = Arrays.toString(bin.getBoxes());
//when
double result = bin.getPercent(12);
//then
System.out.println(wholeBin);
System.out.println(result);
}

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

import java.util.Arrays;

public class DiceTest {
@Test
public void diceTest(){
//given
Dice newDices = new Dice(10);
//when
Integer[] dices = newDices.getDiceList().toArray(new Integer[0]);
//then
System.out.println(Arrays.toString(dices));
}

@Test
public void tossAndSumTest(){
//given
Dice newDices = new Dice(10);
Integer[] preToss = newDices.getDiceList().toArray(new Integer[0]);

//when
Integer toss = newDices.tossAndSum();
Integer[] postToss = newDices.getDiceList().toArray(new Integer[0]);

//then
//Check with output
System.out.println(Arrays.toString(preToss));
System.out.println("================After Tossing================");
System.out.println(Arrays.toString(postToss));
System.out.println(toss);

}

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

import java.math.BigDecimal;
import java.math.RoundingMode;

public class SimulationTest {

@Test
public void getStarsTest(){
//given
Simulation newSim = new Simulation(2, 10);
newSim.runSim();
double number = .05;
BigDecimal num = new BigDecimal(number).setScale(2, RoundingMode.HALF_DOWN);
String result = newSim.getStars(number);
System.out.println(result);



}

}