Skip to content

Finished the lab! #44

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 2 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
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>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>


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

public class Bins {

static Integer[] values;
private final Integer min;
private final Integer max;

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

public void incrementBin(int binIndex) {
int index = binIndex - min;
values[index]++;
}
}
53 changes: 53 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
import java.util.ArrayList;
import java.util.List;

public class Dice {

private ArrayList<Integer> rollingResults;
private Integer numOfDice = 2;
private Integer sumOfDice;

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

public ArrayList<Integer> getRollingResults() {
return rollingResults;
}

public void setRollingResults(ArrayList<Integer> rollingResults) {
this.rollingResults = rollingResults;
}

public Integer tossAndSum () {
Integer sum = 0;
for (int i = 0; i < rollingResults.size(); i++) {
rollingResults.set(i, (int) ((Math.random() * (7 - 1)) + 1));
sum += rollingResults.get(i);
}
return sum;
}

// public void initializeDiceList () {
// Integer numOfDice = getNumOfDice();
// List<Integer> rollingResults = this.getRollingResults();
// for (int i = 0; i < numOfDice; i++) {
// rollingResults.add(0);
// }


public Integer getNumOfDice() {
return numOfDice;
}

public void setNumOfDice(Integer numOfDice) {
this.numOfDice = numOfDice;
}

public Integer getSumOfDice() {
return sumOfDice;
}

public void setSumOfDice(Integer sumOfDice) {
this.sumOfDice = sumOfDice;
}
}
5 changes: 5 additions & 0 deletions src/main/java/MainApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class MainApplication {
public static void main(String[] args) {

}
}
54 changes: 53 additions & 1 deletion src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.io.PrintStream;
import java.util.Arrays;

public class Simulation {
public static void main(String[] args) {
run(2, 1000000);
}

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

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 static void run (Integer numOfDice, Integer numOfToss) {
Dice dice = new Dice(numOfDice);
Bins bin = new Bins(numOfDice, numOfDice * 6);

for (int i = 0; i < numOfToss; i++) {
dice.tossAndSum();
bin.incrementBin(dice.tossAndSum());
}
// System.out.println(Arrays.toString(Bins.values));
// System.out.println("2's: " + Bins.values[0] + " : %.2d");
printer();
}

}
public static void printer () {
String jawner = "";
String jawnski = "";
for (int i = 0; i < Bins.values.length; i++) {
if (i < 3) {
jawnski = " ";
} else {
jawnski = "";
}
Double result = Bins.values[i] / 1000000.0;
jawner += (i + 2) + ": " + Bins.values[i] + jawnski + " : " + String.format("%.2f", result) + " ";
for (int j = 0; j < (result * 100) - 1; j++) {
jawner += "*";
}
jawner += "\n";
}
System.out.println(jawner);
}
}
32 changes: 32 additions & 0 deletions src/test/java/BinsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class BinsTest {
@Test
public void testBinConstructor () {
int testMin = 2;
int testMax = 12;
int expected = testMax - testMin;

Bins test = new Bins(testMin, testMax);
int actualLength = test.values.length;

Assert.assertEquals(expected, actualLength);
}

@Test
public void testBinConstructor1 () {
int testMin = 2;
int testMax = 12;
int expected = testMax - testMin;

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

Assert.assertEquals(expected, actualLength);
Assert.assertEquals(0, actualValue);
}
}
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;

import java.util.ArrayList;
import java.util.List;

public class DiceTest {

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

Integer testInt = dice.tossAndSum();

System.out.println(testInt);
Assert.assertTrue(testInt < 13 && testInt > 1);
}
}
5 changes: 5 additions & 0 deletions src/test/java/SimulationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import org.junit.Test;

public class SimulationTest {

}