From c78f13b422d5f8ea61bda50a270d557b0dfceb95 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 19 May 2015 23:25:28 +0200 Subject: Week13 die --- .gitignore | 5 +- Week13 Die/Assignment (in Dutch).pdf | Bin 0 -> 116977 bytes Week13 Die/build.xml | 73 ++ Week13 Die/manifest.mf | 3 + Week13 Die/nbproject/build-impl.xml | 1413 ++++++++++++++++++++++ Week13 Die/nbproject/genfiles.properties | 8 + Week13 Die/nbproject/project.properties | 76 ++ Week13 Die/nbproject/project.xml | 15 + Week13 Die/src/com/camilstaps/die/Die.java | 47 + Week13 Die/src/com/camilstaps/die/Player.java | 65 + Week13 Die/src/com/camilstaps/die/Week13Die.java | 45 + Week13 Die/test/com/camilstaps/die/DieTest.java | 106 ++ 12 files changed, 1855 insertions(+), 1 deletion(-) create mode 100644 Week13 Die/Assignment (in Dutch).pdf create mode 100644 Week13 Die/build.xml create mode 100644 Week13 Die/manifest.mf create mode 100644 Week13 Die/nbproject/build-impl.xml create mode 100644 Week13 Die/nbproject/genfiles.properties create mode 100644 Week13 Die/nbproject/project.properties create mode 100644 Week13 Die/nbproject/project.xml create mode 100644 Week13 Die/src/com/camilstaps/die/Die.java create mode 100644 Week13 Die/src/com/camilstaps/die/Player.java create mode 100644 Week13 Die/src/com/camilstaps/die/Week13Die.java create mode 100644 Week13 Die/test/com/camilstaps/die/DieTest.java diff --git a/.gitignore b/.gitignore index fbe3c4b..6ec1edf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ /Week12 Find File/nbproject/private/ /Week12 Find File/build/ /Week12 Fibonacci/nbproject/private/ -/Week12 Fibonacci/build/ \ No newline at end of file +/Week12 Fibonacci/build/ +/Week13 Dice/nbproject/private/ +/Week13 Dice/build/ +/Week13 Die/nbproject/private/ \ No newline at end of file diff --git a/Week13 Die/Assignment (in Dutch).pdf b/Week13 Die/Assignment (in Dutch).pdf new file mode 100644 index 0000000..cac5aee Binary files /dev/null and b/Week13 Die/Assignment (in Dutch).pdf differ diff --git a/Week13 Die/build.xml b/Week13 Die/build.xml new file mode 100644 index 0000000..bfc122d --- /dev/null +++ b/Week13 Die/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project Week13 Die. + + + diff --git a/Week13 Die/manifest.mf b/Week13 Die/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/Week13 Die/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/Week13 Die/nbproject/build-impl.xml b/Week13 Die/nbproject/build-impl.xml new file mode 100644 index 0000000..e6566be --- /dev/null +++ b/Week13 Die/nbproject/build-impl.xml @@ -0,0 +1,1413 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Week13 Die/nbproject/genfiles.properties b/Week13 Die/nbproject/genfiles.properties new file mode 100644 index 0000000..3a96bcb --- /dev/null +++ b/Week13 Die/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=f75c1aa2 +build.xml.script.CRC32=e9e7ccf2 +build.xml.stylesheet.CRC32=8064a381@1.75.2.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=f75c1aa2 +nbproject/build-impl.xml.script.CRC32=fa2b43ad +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/Week13 Die/nbproject/project.properties b/Week13 Die/nbproject/project.properties new file mode 100644 index 0000000..522cfc7 --- /dev/null +++ b/Week13 Die/nbproject/project.properties @@ -0,0 +1,76 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=Week13 Die +application.vendor=camilstaps +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/Week13_Die.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${libs.junit_4.classpath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=com.camilstaps.die.Week13Die +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/Week13 Die/nbproject/project.xml b/Week13 Die/nbproject/project.xml new file mode 100644 index 0000000..b395a19 --- /dev/null +++ b/Week13 Die/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + Week13 Die + + + + + + + + + diff --git a/Week13 Die/src/com/camilstaps/die/Die.java b/Week13 Die/src/com/camilstaps/die/Die.java new file mode 100644 index 0000000..d83906a --- /dev/null +++ b/Week13 Die/src/com/camilstaps/die/Die.java @@ -0,0 +1,47 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.camilstaps.die; + +import java.util.Random; + +/** + * A simple die + * @author Camil Staps, s4498062 + */ +public class Die { + private final Random rand; + public static final int MAX_PIPS = 6; + + public Die() { + rand = new Random(); + } + + /** + * Roll the die + * @return the number of pips + */ + public int roll() { + return rand.nextInt(MAX_PIPS) + 1; + } +} \ No newline at end of file diff --git a/Week13 Die/src/com/camilstaps/die/Player.java b/Week13 Die/src/com/camilstaps/die/Player.java new file mode 100644 index 0000000..86b379f --- /dev/null +++ b/Week13 Die/src/com/camilstaps/die/Player.java @@ -0,0 +1,65 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.camilstaps.die; + +/** + * A player rolling a die + * @author Camil Staps, s4498062 + */ +public class Player implements Runnable { + private final Die die; + private final String name; + private final int numberOfThrows; + private int pipss[]; + + /** + * The player will perform a number of rolls of a die + * @param name the name of the player + * @param die the die to use + * @param numberOfThrows the number of rolls + */ + public Player(String name, Die die, int numberOfThrows) { + this.die = die; + this.name = name; + this.numberOfThrows = numberOfThrows; + } + + @Override + public void run() { + pipss = new int[numberOfThrows]; + for (int i = 0; i < numberOfThrows; i++) { + int pips = die.roll(); + pipss[i] = pips; + System.err.println(name + " throws " + pips + " in turn " + i); // Only debugging output; so stderr + } + } + + /** + * The results we've found + * @return the results of the die rolls + */ + public int[] getPipss() { + return pipss; + } +} diff --git a/Week13 Die/src/com/camilstaps/die/Week13Die.java b/Week13 Die/src/com/camilstaps/die/Week13Die.java new file mode 100644 index 0000000..1e26b09 --- /dev/null +++ b/Week13 Die/src/com/camilstaps/die/Week13Die.java @@ -0,0 +1,45 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.camilstaps.die; + +/** + * Solutions to week 13, part 1 + * @author Camil Staps, s4498062 + */ +public class Week13Die { + + private static final int THROWS = 1; + + /** + * Usage: java Week13Die Alfred Bert Chris ... + * @param args the command line arguments / the names of the players + */ + public static void main(String[] args) { + Die die = new Die(); + for (String name : args) { + (new Thread(new Player(name, die, THROWS))).start(); + } + } + +} diff --git a/Week13 Die/test/com/camilstaps/die/DieTest.java b/Week13 Die/test/com/camilstaps/die/DieTest.java new file mode 100644 index 0000000..2f1556e --- /dev/null +++ b/Week13 Die/test/com/camilstaps/die/DieTest.java @@ -0,0 +1,106 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.camilstaps.die; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author camilstaps + */ +public class DieTest { + + private static final int PLAYERS = 100; + private static final int THROWS = 100; + + private static final double ACCEPTABLE_LOWER_BOUND = 0.95; + private static final double ACCEPTABLE_UPPER_BOUND = 1.05; + + public DieTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test that the probabilities for every different possible outcome are more or less close to the expected probability + * Accuracy of this test depends on PLAYERS, THROWS, ACCEPTABLE_LOWER_BOUND and ACCEPTABLE_UPPER_BOUND + * + * With non-thread safe solutions, this test most of the time fails. With thread-safe solutions, the test normally passes. + */ + @Test + public void correctProbabilities() { + // Start players + Die die = new Die(); + Player players[] = new Player[PLAYERS]; + Thread threads[] = new Thread[PLAYERS]; + for (int i = 0; i < PLAYERS; i++) { + players[i] = new Player("Player " + i, die, THROWS); + threads[i] = new Thread(players[i]); + threads[i].start(); + } + + // Gather data + int pips_counts[] = new int[Die.MAX_PIPS]; + for (int i = 0; i < PLAYERS; i++) { + try { + threads[i].join(); + for (int p : players[i].getPipss()) { + pips_counts[p - 1]++; + } + } catch (InterruptedException e) { + System.err.println(e); + } + } + + // Verification + int lower_bound = (int) (PLAYERS * THROWS / Die.MAX_PIPS * ACCEPTABLE_LOWER_BOUND); + int upper_bound = (int) (PLAYERS * THROWS / Die.MAX_PIPS * ACCEPTABLE_UPPER_BOUND); + System.out.println("Normal: " + PLAYERS * THROWS / Die.MAX_PIPS); + System.out.println("Lower bound: " + lower_bound + " (" + ACCEPTABLE_LOWER_BOUND + ")"); + System.out.println("Upper bound: " + upper_bound + " (" + ACCEPTABLE_UPPER_BOUND + ")"); + for (int i = 0; i < Die.MAX_PIPS; i++) { + System.out.println("Number of " + i + ": " + pips_counts[i]); + assertTrue("Number of " + i + " out of range", pips_counts[i] > lower_bound && pips_counts[i] < upper_bound); + } + } +} -- cgit v1.2.3