From 9c376dc231276dade3e8665d16eafd77b07e11a0 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 12 May 2015 13:44:12 +0200 Subject: Week12 fibonacci --- Week12 Fibonacci/build.xml | 73 + Week12 Fibonacci/manifest.mf | 3 + Week12 Fibonacci/nbproject/build-impl.xml | 1413 ++++++++++++++++++++ Week12 Fibonacci/nbproject/genfiles.properties | 8 + Week12 Fibonacci/nbproject/project.properties | 73 + Week12 Fibonacci/nbproject/project.xml | 15 + .../src/com/camilstaps/fibonacci/ParFib.java | 91 ++ .../com/camilstaps/fibonacci/Week12Fibonacci.java | 54 + 8 files changed, 1730 insertions(+) create mode 100644 Week12 Fibonacci/build.xml create mode 100644 Week12 Fibonacci/manifest.mf create mode 100644 Week12 Fibonacci/nbproject/build-impl.xml create mode 100644 Week12 Fibonacci/nbproject/genfiles.properties create mode 100644 Week12 Fibonacci/nbproject/project.properties create mode 100644 Week12 Fibonacci/nbproject/project.xml create mode 100644 Week12 Fibonacci/src/com/camilstaps/fibonacci/ParFib.java create mode 100644 Week12 Fibonacci/src/com/camilstaps/fibonacci/Week12Fibonacci.java (limited to 'Week12 Fibonacci') diff --git a/Week12 Fibonacci/build.xml b/Week12 Fibonacci/build.xml new file mode 100644 index 0000000..33cc971 --- /dev/null +++ b/Week12 Fibonacci/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project Week12 Fibonacci. + + + diff --git a/Week12 Fibonacci/manifest.mf b/Week12 Fibonacci/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/Week12 Fibonacci/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/Week12 Fibonacci/nbproject/build-impl.xml b/Week12 Fibonacci/nbproject/build-impl.xml new file mode 100644 index 0000000..f4151c5 --- /dev/null +++ b/Week12 Fibonacci/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/Week12 Fibonacci/nbproject/genfiles.properties b/Week12 Fibonacci/nbproject/genfiles.properties new file mode 100644 index 0000000..58474a3 --- /dev/null +++ b/Week12 Fibonacci/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=1eb85472 +build.xml.script.CRC32=00ae1354 +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=1eb85472 +nbproject/build-impl.xml.script.CRC32=3b7d264b +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/Week12 Fibonacci/nbproject/project.properties b/Week12 Fibonacci/nbproject/project.properties new file mode 100644 index 0000000..cffb640 --- /dev/null +++ b/Week12 Fibonacci/nbproject/project.properties @@ -0,0 +1,73 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +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}/Week12_Fibonacci.jar +dist.javadoc.dir=${dist.dir}/javadoc +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} +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.fibonacci.Week12Fibonacci +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/Week12 Fibonacci/nbproject/project.xml b/Week12 Fibonacci/nbproject/project.xml new file mode 100644 index 0000000..5ed44f1 --- /dev/null +++ b/Week12 Fibonacci/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + Week12 Fibonacci + + + + + + + + + diff --git a/Week12 Fibonacci/src/com/camilstaps/fibonacci/ParFib.java b/Week12 Fibonacci/src/com/camilstaps/fibonacci/ParFib.java new file mode 100644 index 0000000..098ba5a --- /dev/null +++ b/Week12 Fibonacci/src/com/camilstaps/fibonacci/ParFib.java @@ -0,0 +1,91 @@ +/* + * 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.fibonacci; + +/** + * Calculate a fibonacci number + * @author Camil Staps s4498062 + */ +public class ParFib implements Runnable { + + private final int n; + private int result = 0; + + /** + * Create a new instance + * @param n the index of the fibonacci number to calculate + */ + public ParFib(int n) { + this.n = n; + } + + @Override + public void run() { + calculate(n); + } + + public int getResult() { + return result; + } + + /** + * Calculate the nth number of the Fibonacci sequence. + * + * This implementation uses two threads if n > 2: one to calculate the + * (n-1)th fibonacci number, and one to calculate the (n-2)th fibonacci + * number. That is a really silly approach, since many threads will be + * created and many numbers will be calculated many times. + * As an example, to calculate for n=10, we will calculate the 8th number + * twice: once for n=10 (10-2=8) and once for n=9 (9-1=8). The 7th number + * will be calculated thrice, and this amount increases for lower numbers. + * But hey, the assignment asked for a solution with threads. + * + * @param n + */ + private void calculate(int n) { + if (n < 3) { + result = 1; + return; + } + + ParFib pf1 = new ParFib(n - 1); + ParFib pf2 = new ParFib(n - 2); + Thread t1 = new Thread(pf1); + Thread t2 = new Thread(pf2); + t1.start(); + t2.start(); + + try { + t1.join(); + t2.join(); + } catch (InterruptedException ex) { + System.err.println(ex.toString()); + } + + if (pf1.getResult() != 0 && pf2.getResult() != 0) { + result = pf1.getResult() + pf2.getResult(); + } + } + +} diff --git a/Week12 Fibonacci/src/com/camilstaps/fibonacci/Week12Fibonacci.java b/Week12 Fibonacci/src/com/camilstaps/fibonacci/Week12Fibonacci.java new file mode 100644 index 0000000..2115cfb --- /dev/null +++ b/Week12 Fibonacci/src/com/camilstaps/fibonacci/Week12Fibonacci.java @@ -0,0 +1,54 @@ +/* + * 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.fibonacci; + +/** + * Solutions to week 12, part 2 + * @author Camil Staps s4498062 + */ +public class Week12Fibonacci { + + /** + * Usage: java Week12Fibonacci n + * Where n is the index of the number from the fibonacci sequence you want + * @param args the command line arguments + */ + public static void main(String[] args) { + if (args.length != 1) { + System.err.println("Usage: java Week12Fibonacci n"); + System.exit(-1); + } + + ParFib pf = new ParFib(Integer.parseInt(args[0])); + Thread t = new Thread(pf); + t.start(); + try { + t.join(); + System.out.println(Integer.toString(pf.getResult())); + } catch (InterruptedException ex) { + System.err.println(ex.toString()); + } + } + +} -- cgit v1.2.3