From 9acea903216dbe371dd7b41cbf23b46a5732bcb4 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sun, 7 Dec 2014 19:15:58 +0100 Subject: Refactored the packagenames fo better sorting --- .../fhswf/in/inf/java1/aufgabe01/HelloWorld.java | 45 +++++++ src/de/fhswf/in/inf/java1/aufgabe04/Fill.java | 117 ++++++++++++++++++ src/de/fhswf/in/inf/java1/aufgabe05/Bank.java | 54 ++++++++ src/de/fhswf/in/inf/java1/aufgabe05/Konto.java | 119 ++++++++++++++++++ src/de/fhswf/in/inf/java1/aufgabe05/Person.java | 86 +++++++++++++ src/de/fhswf/in/inf/java1/aufgabe05/Student.java | 28 +++++ src/de/fhswf/in/inf/java1/aufgabe06/Aufgabe6.java | 81 ++++++++++++ .../in/inf/java1/aufgabe06/ListBenchmark.java | 137 +++++++++++++++++++++ .../in/inf/java1/aufgabe07/GleichSpieler.java | 25 ++++ .../fhswf/in/inf/java1/aufgabe07/Simulation.java | 84 +++++++++++++ src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java | 86 +++++++++++++ src/de/fhswf/in/inf/java1/aufgabe07/Spieler.java | 47 +++++++ .../in/inf/java1/aufgabe07/WechselSpieler.java | 22 ++++ .../in/inf/java1/aufgabe07/ZufallsSpieler.java | 52 ++++++++ .../in/inf/java1/aufgabe08/FloydAndBentley.java | 130 +++++++++++++++++++ .../in/inf/java1/aufgabe08/Lottery6OutOf49.java | 81 ++++++++++++ .../in/inf/java1/aufgabe08/LottoZiehungMain.java | 45 +++++++ src/de/fhswf/in/inf/java1/aufgabe09/WordCount.java | 133 ++++++++++++++++++++ .../in/inf/java1/aufgabe09/WordCountTest.java | 43 +++++++ src/de/fhswf/in/inf/java1/aufgabe1/HelloWorld.java | 45 ------- src/de/fhswf/in/inf/java1/aufgabe4/Fill.java | 117 ------------------ src/de/fhswf/in/inf/java1/aufgabe5/Bank.java | 54 -------- src/de/fhswf/in/inf/java1/aufgabe5/Konto.java | 119 ------------------ src/de/fhswf/in/inf/java1/aufgabe5/Person.java | 86 ------------- src/de/fhswf/in/inf/java1/aufgabe5/Student.java | 28 ----- src/de/fhswf/in/inf/java1/aufgabe6/Aufgabe6.java | 81 ------------ .../fhswf/in/inf/java1/aufgabe6/ListBenchmark.java | 137 --------------------- .../fhswf/in/inf/java1/aufgabe7/GleichSpieler.java | 25 ---- src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java | 84 ------------- src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java | 86 ------------- src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java | 47 ------- .../in/inf/java1/aufgabe7/WechselSpieler.java | 22 ---- .../in/inf/java1/aufgabe7/ZufallsSpieler.java | 52 -------- .../in/inf/java1/aufgabe8/FloydAndBentley.java | 130 ------------------- .../in/inf/java1/aufgabe8/Lottery6OutOf49.java | 81 ------------ .../in/inf/java1/aufgabe8/LottoZiehungMain.java | 45 ------- src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java | 133 -------------------- .../fhswf/in/inf/java1/aufgabe9/WordCountTest.java | 43 ------- 38 files changed, 1415 insertions(+), 1415 deletions(-) create mode 100644 src/de/fhswf/in/inf/java1/aufgabe01/HelloWorld.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe04/Fill.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe05/Bank.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe05/Konto.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe05/Person.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe05/Student.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe06/Aufgabe6.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe06/ListBenchmark.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe07/GleichSpieler.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe07/Spieler.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe07/WechselSpieler.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe07/ZufallsSpieler.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe08/FloydAndBentley.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe08/Lottery6OutOf49.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe08/LottoZiehungMain.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe09/WordCount.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe09/WordCountTest.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe1/HelloWorld.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe4/Fill.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe5/Bank.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe5/Konto.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe5/Person.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe5/Student.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe6/Aufgabe6.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe6/ListBenchmark.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe7/ZufallsSpieler.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe8/FloydAndBentley.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe8/Lottery6OutOf49.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe8/LottoZiehungMain.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java delete mode 100644 src/de/fhswf/in/inf/java1/aufgabe9/WordCountTest.java (limited to 'src') diff --git a/src/de/fhswf/in/inf/java1/aufgabe01/HelloWorld.java b/src/de/fhswf/in/inf/java1/aufgabe01/HelloWorld.java new file mode 100644 index 0000000..f1af45e --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe01/HelloWorld.java @@ -0,0 +1,45 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe01; + +/** + * Begrüßung der Sprache Java. Diese Klasse begrüßt die Sprache Java mit einem + * traditionellen "Hello World!". + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class HelloWorld +{ + private HelloWorld() + { + + } + + /** + * Hauptmethode der Application. Begrüßung der Sprache Java. Diese Klasse + * begrüßt die Sprache Java mit einem traditionellen "Hello World!". + * + * @param args + * Übergabeparameter des Programms. + */ + public static void main(String[] args) + { + System.out.println("Anzahl der Argumente: " + args.length); + if (args.length != 0) + { + System.out.print("Ihr Name ist:"); + for (int i = 0; i < args.length; i++) + { + System.out.print(" " + args[i]); + } + System.out.println("."); + } + else + { + System.out.print("Kein Name vorhanden."); + } + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe04/Fill.java b/src/de/fhswf/in/inf/java1/aufgabe04/Fill.java new file mode 100644 index 0000000..b60c6c2 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe04/Fill.java @@ -0,0 +1,117 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe04; + +import java.util.Arrays; + +/** + * Tests the different methods to created a prefilled String. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class Fill +{ + private Fill() + { + } + + /** + * + * Creates a string of given length filled with the given char. + * + * @param length + * Length of the returned String. + * @param c + * Char the String is filled with. + * @return Returns the generated String. + */ + public static String createStringFilledWith(int length, char c) + { + // String that will be returned + String result = ""; + for (int i = 0; i < length; i++) + { + // Every iteration, a new string is created. + result += c; + } + // Return the build string + return result; + } + + /** + * + * Creates a string of given length filled with the given char. + * + * @param length + * Length of the returned String. + * @param c + * Char the String is filled with. + * @return Returns the generated String. + */ + public static String createStringFilledWith2(int length, char c) + { + // charArray that will be returned + char[] result = new char[length]; + + Arrays.fill(result, c); + + // Return the build string + return result.toString(); + } + + /** + * + * Creates a string of given length filled with the given char. + * + * @param length + * Length of the returned String. + * @param c + * Char the String is filled with. + * @return Returns the generated String. + */ + public static String createStringFilledWith3(int length, char c) + { + // String that will be returned + StringBuilder result = new StringBuilder(length); + for (int i = 0; i < length; i++) + { + // Every iteration, a new string is created. + result.append(c); + } + // Return the build string + return result.toString(); + } + + /** + * + * Calls the function createStringFilledWith() and measures its runtime. + * + * @param args + * Console line arguments. + */ + public static void main(String[] args) + { + final int anzDurchlaefe = 100000000; + + long start; + start = System.currentTimeMillis(); // Gets current time in �s + Fill.createStringFilledWith(anzDurchlaefe, 'x'); // Function call + // Calculates the time runtime of creatStringFilledWith() + System.out.println("Elapsed time: " + + (System.currentTimeMillis() - start) + " ms"); + + start = System.currentTimeMillis(); // Gets current time in �s + Fill.createStringFilledWith2(anzDurchlaefe, 'x'); // Function call + // Calculates the time runtime of creatStringFilledWith2() + System.out.println("Elapsed time: " + + (System.currentTimeMillis() - start) + " ms"); + + start = System.currentTimeMillis(); // Gets current time in �s + Fill.createStringFilledWith3(anzDurchlaefe, 'x'); // Function call + // Calculates the time runtime of creatStringFilledWith3() + System.out.println("Elapsed time: " + + (System.currentTimeMillis() - start) + " ms"); + } +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe05/Bank.java b/src/de/fhswf/in/inf/java1/aufgabe05/Bank.java new file mode 100644 index 0000000..d90c447 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe05/Bank.java @@ -0,0 +1,54 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe05; + +import java.math.BigDecimal; + +/** + * Package for main function. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class Bank +{ + + /** + * Prevents instantiation of the unity class Bank. + * + */ + private Bank() + { + + } + + /** + * Main function for testing the Bank system. + * + * @param args + * Shell arguments + */ + public static void main(String[] args) + { + Person dieter = new Person("Dieter", "Hallowegers"); + + Konto giro = new Konto(dieter, new BigDecimal(100)); + + try + { + giro.einzahlen(new BigDecimal(100)); + giro.abheben(new BigDecimal(200)); + + System.out.println(giro.getKontostand()); + + giro.abheben(new BigDecimal(1)); + } + catch (IllegalArgumentException e) + { + System.out.println(e); + } + + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe05/Konto.java b/src/de/fhswf/in/inf/java1/aufgabe05/Konto.java new file mode 100644 index 0000000..235f9af --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe05/Konto.java @@ -0,0 +1,119 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe05; + +import java.math.BigDecimal; + +/** + * An implementation of the Konto class which represents a bank account. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Konto +{ + + private BigDecimal guthaben; + + private BigDecimal dispo; + + private Person besitzer; + + /** + * Creates an account with an owner, a dispo and an initial balance. + * + * @param besitzer + * Owner of the account + * @param dispo + * Dispo of the account + */ + public Konto(Person besitzer, BigDecimal dispo) + { + + if (besitzer == null) + { + throw new IllegalArgumentException("Besitzer can't be null"); + } + if (dispo == null) + { + throw new IllegalArgumentException("Dispo can't be null"); + } + if (dispo.compareTo(BigDecimal.ZERO) < 0) + { + throw new IllegalArgumentException("Dispo can't be negative"); + } + + this.besitzer = besitzer; + this.dispo = dispo; + this.guthaben = new BigDecimal(0); + + this.besitzer.addKonto(this); + + } + + /** + * Function for withdrawing money from the account. + * + * @param betrag + * Amount of cash to be withdrawn + */ + public void abheben(BigDecimal betrag) + { + if (betrag == null) + { + throw new IllegalArgumentException("Betrag can't be null"); + } + if (betrag.compareTo(BigDecimal.ZERO) < 0) + { + throw new IllegalArgumentException("Betrag can't be negative"); + } + if (guthaben.add(dispo).compareTo(betrag) < 0) + { + throw new IllegalArgumentException("Betrag surpasses dispo limit"); + } + + guthaben = guthaben.subtract(betrag); + } + + /** + * Function to deposit money into the account. + * + * @param betrag + * Amount of cash to be disposed + */ + public void einzahlen(BigDecimal betrag) + { + if (betrag == null) + { + throw new IllegalArgumentException("Betrag can't be null"); + } + if (betrag.compareTo(BigDecimal.ZERO) < 0) + { + throw new IllegalArgumentException("Betrag can't be negative"); + } + + guthaben = guthaben.add(betrag); + } + + /** + * Returns the actual balance. + * + * @return Returns the actual balance. + */ + public BigDecimal getKontostand() + { + return guthaben; + } + + /** + * Just to get the owners name. + * + * @return Returns the owners name. + */ + public Person getBesitzer() + { + return besitzer; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe05/Person.java b/src/de/fhswf/in/inf/java1/aufgabe05/Person.java new file mode 100644 index 0000000..18b619d --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe05/Person.java @@ -0,0 +1,86 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe05; + +import java.util.ArrayList; +import java.util.List; + +/** + * A person which can own an account. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Person +{ + private String vorname; + + private String nachname; + + private List kontenliste = new ArrayList<>(); + + /** + * Creates a person with first and last name. + * + * @param vorname + * First name of the person + * @param nachname + * Last name of the person + */ + public Person(String vorname, String nachname) + { + if (vorname == null) + { + throw new IllegalArgumentException("Vorname can't be null"); + } + if (nachname == null) + { + throw new IllegalArgumentException("Nachname can't be null"); + } + if (vorname.isEmpty()) + { + throw new IllegalArgumentException("Vorname can't be empty"); + } + if (nachname.isEmpty()) + { + throw new IllegalArgumentException("Nachname can't be empty"); + } + this.vorname = vorname; + this.nachname = nachname; + } + + /** + * Just for getting the persons name. + * + * @return Returns the full name of the person + */ + @Override + public String toString() + { + return vorname + " " + nachname; + } + + /** + * For adding the back reference to the Konto. + * + * @param konto + * Konto that will be added to the person. + */ + public void addKonto(Konto konto) + { + if (konto == null) + { + throw new IllegalArgumentException("Konto can't be empty"); + } + if (konto.getBesitzer() != this) + { + throw new IllegalArgumentException("Person must be owner of Konto"); + } + if (!kontenliste.contains(konto)) + { + kontenliste.add(konto); + } + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe05/Student.java b/src/de/fhswf/in/inf/java1/aufgabe05/Student.java new file mode 100644 index 0000000..9963af9 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe05/Student.java @@ -0,0 +1,28 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe05; + +/** + * Extends Person and represents a student. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Student extends Person +{ + + /** + * Simply calls the super constructor to initialize person. + * + * @param vorname + * First name of the student + * @param nachname + * Last name of the student + */ + public Student(String vorname, String nachname) + { + super(vorname, nachname); + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe06/Aufgabe6.java b/src/de/fhswf/in/inf/java1/aufgabe06/Aufgabe6.java new file mode 100644 index 0000000..0f40448 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe06/Aufgabe6.java @@ -0,0 +1,81 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe06; + +import java.util.Iterator; +import java.util.Vector; + +/** + * Main function for testing Vectors and Lists. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class Aufgabe6 +{ + + /** + * Prevents instantiation of the utility class. + * + */ + private Aufgabe6() + { + } + + /** + * Main function for testing Vectors and Lists. + * + * @param args + * Command line arguments. + */ + public static void main(String[] args) + { + final int testLength = 10000000; + Vector test = new Vector<>(testLength); + Integer testGet; + + for (int i = 0; i < test.capacity(); i++) + { + test.add(i); + } + + long start; + start = System.currentTimeMillis(); // Gets current time in ms + + for (int i = 0; i < test.size(); i++) + { + testGet = test.elementAt(i); + } + + // Calculates the runtime of for + System.out.println("Elapsed time: " + + (System.currentTimeMillis() - start) + " ms"); + + start = System.currentTimeMillis(); // Gets current time in ms + + for (Iterator it = test.iterator(); it.hasNext();) + { + testGet = it.next(); + } + + // Calculates the runtime of for + System.out.println("Elapsed time: " + + (System.currentTimeMillis() - start) + " ms"); + + start = System.currentTimeMillis(); // Gets current time in ms + + for (Integer integer : test) + { + testGet = integer; + } + + // Calculates the runtime of foreach + System.out.println("Elapsed time: " + + (System.currentTimeMillis() - start) + " ms"); + + ListBenchmark.startBenchmark(); + + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe06/ListBenchmark.java b/src/de/fhswf/in/inf/java1/aufgabe06/ListBenchmark.java new file mode 100644 index 0000000..b03b68d --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe06/ListBenchmark.java @@ -0,0 +1,137 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe06; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import java.util.Vector; + +/** + * Class that benchmarks List implementations. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class ListBenchmark +{ + + private static final Integer TESTOBJ = new Integer(214); + + /** + * Prevents instantiation of the utility class. + * + */ + private ListBenchmark() + { + } + + /** + * Starts a benchmark of different Lists and different test cases. + * + */ + public static void startBenchmark() + { + final int times = 100000; + + benchmarkGrouping("ArrayList", new ArrayList(times), times); + benchmarkGrouping("Vector", new Vector(times), times); + benchmarkGrouping("LinkedList", new LinkedList(), times); + + } + + /** + * Groups the 3 different benchmark versions together. + * + * @param variant + * Name of the list type. + * @param list + * The list which will be benchmarked. + * @param times + * How often will be inserted. + */ + private static void benchmarkGrouping(String variant, List list, + int times) + { + System.out.print(variant + " am Anfang: "); + System.out.println(benchmarkAtBeginning(list, times) + " ms"); + System.out.print(variant + " am Ende: "); + System.out.println(benchmarkAtEnding(list, times) + " ms"); + System.out.print(variant + " zufälliger Index: "); + System.out.println(benchmarkAtRandom(list, times) + " ms"); + } + + /** + * Inserts new value at the beginning of the list. + * + * @param list + * List to be tested. + * @param times + * How many inserts are done. + * @return Time in milliseconds that the action took. + */ + private static long benchmarkAtBeginning(List list, int times) + { + long start = System.currentTimeMillis(); // Gets current time in µs + + for (int i = 0; i < times; i++) + { + list.add(0, TESTOBJ); + } + + return (System.currentTimeMillis() - start); // Returns runtime + } + + /** + * Inserts new value at the end of the list. + * + * @param list + * List to be tested. + * @param times + * How many inserts are done. + * @return Time in milliseconds that the action took. + */ + private static long benchmarkAtEnding(List list, int times) + { + long start = System.currentTimeMillis(); // Gets current time in µs + + for (int i = 0; i < times; i++) + { + list.add(TESTOBJ); + } + + return (System.currentTimeMillis() - start); // Returns runtime + } + + /** + * Inserts new value at a random index of the list. + * + * @param list + * List to be tested. + * @param times + * How many inserts are done. + * @return Time in milliseconds that the action took. + */ + private static long benchmarkAtRandom(List list, int times) + { + + int[] randVal = new int[times]; + Random rand = new Random(); + + for (int i = 0; i < randVal.length; i++) + { + randVal[i] = rand.nextInt(list.size() + i + 1); + } + + long start = System.currentTimeMillis(); // Gets current time in µs + + for (int i = 0; i < times; i++) + { + list.add(randVal[i], TESTOBJ); + } + + return (System.currentTimeMillis() - start); // Returns runtime + } +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/GleichSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe07/GleichSpieler.java new file mode 100644 index 0000000..be592cd --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/GleichSpieler.java @@ -0,0 +1,25 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +/** + * This player always chooses the same shell. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class GleichSpieler extends Spieler +{ + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe07.Spieler#zweiterTipp(int) + */ + @Override + public final int zweiterTipp(int leeresHuetchen) + { + return getErsterTipp(); + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java b/src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java new file mode 100644 index 0000000..1c6622c --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java @@ -0,0 +1,84 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +/** + * Calculates the the success rate of different players. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class Simulation +{ + + private static final int VERSUCHE = 100000; + + /** + * Prevents instantiation of the utility class. + * + */ + private Simulation() + { + } + + /** + * Makes anzahl games. + * + * @param anzahl + * How often the game will be run. + * @return Victory count + */ + private static int nDurchlaufeAusfuehren(Spiel spiel, Spieler spieler, + int anzahl) + { + + int erfolge = 0; + for (int i = 0; i < anzahl; i++) + { + if (spiel.spielDurchlauf(spieler)) + { + erfolge++; + } + } + + return erfolge; + } + + /** + * Calculates the success rate of every player. + * + * @param erfolg + * An array with the last game stats. + */ + private static double wahrscheinlichkeitErrechnen(int erfolge, int versuche) + { + return (double) erfolge / versuche; + } + + /** + * Main method of the package. + * + * @param args + * Command line arguments + */ + public static void main(String[] args) + { + + Spieler[] spieler = {new GleichSpieler(), new WechselSpieler(), + new ZufallsSpieler(0.5)}; + + Spiel spiel = new Spiel(); + + for (int i = 0; i < spieler.length; i++) + { + int spielerGewinne = nDurchlaufeAusfuehren(spiel, spieler[i], + VERSUCHE); + double spielerProb = wahrscheinlichkeitErrechnen(spielerGewinne, + VERSUCHE); + System.out.printf("%17s war %05.2f%s erfolgreich\n", spieler[i] + .getClass().getSimpleName(), spielerProb * 100, "%"); + } + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java b/src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java new file mode 100644 index 0000000..19e2998 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java @@ -0,0 +1,86 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +import java.util.Random; + +/** + * Simulates a shell game. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Spiel +{ + private int nussPlatz = -1; + + public static final int MAXHUET = 3; + + public static final Random RAND = new Random(); + + /** + * Places the pea under a shell. + * + */ + private void nussVerstecken() + { + nussPlatz = RAND.nextInt(MAXHUET); + } + + /** + * Returns an empty shell, which the user didn't choose. + * + * @param ersterTipp + * The first choice of the player. + * @return An empty shell number of the remaining shells + */ + private int zeigeHuetchen(int ersterTipp) + { + if (ersterTipp < 0 || ersterTipp > MAXHUET) + { + throw new IllegalArgumentException("Tip outside of boundaries."); + } + int erg = -1; + if (ersterTipp != nussPlatz) + { + erg = MAXHUET - nussPlatz - ersterTipp; + } + else + { + erg = (ersterTipp + RAND.nextInt(MAXHUET - 1)) % MAXHUET; + } + return erg; + } + + /** + * Returns the shell number with the pea. + * + * @return The shell number + */ + private boolean aufloesung(int zweiterTipp) + { + return nussPlatz == zweiterTipp; + } + + /** + * Simulates a complete game. + * + * @param spieler + * The player who plays. + * @return Boolean, if the player choose correct. + */ + public final boolean spielDurchlauf(Spieler spieler) + { + if (spieler == null) + { + throw new IllegalArgumentException("A player hast to play."); + } + int tipp = -1; + nussVerstecken(); + tipp = spieler.ersterTipp(); + tipp = spieler.zweiterTipp(zeigeHuetchen(tipp)); + return aufloesung(tipp); + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/Spieler.java b/src/de/fhswf/in/inf/java1/aufgabe07/Spieler.java new file mode 100644 index 0000000..14d17a8 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/Spieler.java @@ -0,0 +1,47 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +/** + * Represents a player that chooses a shell. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public abstract class Spieler +{ + + private int ersterTipp = -1; + + /** + * The first shell the player chooses. + * + * @return The first guess for a shell + */ + public final int ersterTipp() + { + ersterTipp = Spiel.RAND.nextInt(Spiel.MAXHUET); + return ersterTipp; + } + + /** + * Getter for the first choice. + * + * @return The first choice + */ + public final int getErsterTipp() + { + return ersterTipp; + } + + /** + * After the first guess, the player must choose a second shell. + * + * @param leeresHuetchen + * The shell, the game showed as empty + * @return The second guess for the right shell + */ + public abstract int zweiterTipp(int leeresHuetchen); + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/WechselSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe07/WechselSpieler.java new file mode 100644 index 0000000..5c6ec55 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/WechselSpieler.java @@ -0,0 +1,22 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +/** + * This player always changes the shell. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class WechselSpieler extends Spieler +{ + /* (non-Javadoc) + * @see de.fhswf.in.inf.java1.aufgabe07.Spieler#zweiterTipp(int) + */ + @Override + public int zweiterTipp(int leeresHuetchen) + { + return Spiel.MAXHUET - getErsterTipp() - leeresHuetchen; + } +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/ZufallsSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe07/ZufallsSpieler.java new file mode 100644 index 0000000..29a8aba --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/ZufallsSpieler.java @@ -0,0 +1,52 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +/** + * A player that switches strategy with a certain probability. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class ZufallsSpieler extends WechselSpieler +{ + + private double wahrscheinlichkeit = 0; + + /** + * Initializes a player which switches strategies. + * + * @param wahrscheinlichkeit + * Probability of switching strategy. + */ + public ZufallsSpieler(double wahrscheinlichkeit) + { + if (wahrscheinlichkeit < 0.0 | wahrscheinlichkeit > 1.0) + { + throw new IllegalArgumentException( + "Only a probability between 0 and 100 is allowed."); + } + + this.wahrscheinlichkeit = wahrscheinlichkeit; + } + + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe07.WechselSpieler#zweiterTipp(int) + */ + @Override + public final int zweiterTipp(int leeresHuetchen) + { + if (Spiel.RAND.nextDouble() < wahrscheinlichkeit) + { + return getErsterTipp(); + } + else + { + return super.zweiterTipp(leeresHuetchen); + } + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe08/FloydAndBentley.java b/src/de/fhswf/in/inf/java1/aufgabe08/FloydAndBentley.java new file mode 100644 index 0000000..8ba0a7a --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe08/FloydAndBentley.java @@ -0,0 +1,130 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe08; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * Utility class for the Floyd and Bentley algorithm. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class FloydAndBentley +{ + + private static Random rnd = new Random(); + + /** + * Private constructor for utility class. + * + */ + private FloydAndBentley() + { + } + + /** + * A function which chooses a sample from a List with the Floyd and Bentley + * algorithm. (Recursive algorithm) + * + * @param + * Type of the Lists. + * @param s + * The set from which the sample is chosen. + * @param k + * The number of items to be chosen. + * @return Return the sample. + */ + public static List sample(List s, int k) + { + if (s.isEmpty()) + { + throw new IllegalArgumentException("Set is empty"); + } + if (k > s.size()) + { + throw new IllegalArgumentException("k is bigger than the Set"); + } + + List ret = new ArrayList<>(k); + sampleRec(s, k, ret); + + return ret; + + } + + /** + * The recursive implementation of the Floyd and Bentley algorithm. + * + * @param + * Type of the Lists. + * @param s + * The set from which the sample is chosen. + * @param k + * The number of items to be chosen. + * @param ret + * Return the sample. + */ + private static void sampleRec(List s, int k, List ret) + { + if (ret.size() >= k) + { + return; + } + + T val = s.get(rnd.nextInt(s.size() - (k - ret.size() - 1))); + + if (ret.contains(val)) + { + val = s.get(s.size() - (k - ret.size() - 1)); + } + + ret.add(val); + + sampleRec(s, k, ret); + + } + + /** + * A function which chooses a sample from a List with the Floyd and Bentley + * algorithm. (Non recursive algorithm) + * + * @param + * Type of the Lists. + * @param s + * The set from which the sample is chosen. + * @param k + * The number of items to be chosen. + * @return Return the sample. + */ + public static List sample2(List s, int k) + { + if (s.isEmpty()) + { + throw new IllegalArgumentException("Set is empty"); + } + if (k > s.size()) + { + throw new IllegalArgumentException("k is bigger than the Set"); + } + + List ret = new ArrayList<>(k); + + for (int i = 0; i < k; i++) + { + T val = s.get(rnd.nextInt(s.size() - (k - ret.size() - 1))); + + if (ret.contains(val)) + { + val = s.get(s.size() - (k - ret.size() - 1)); + } + + ret.add(val); + } + + return ret; + } +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe08/Lottery6OutOf49.java b/src/de/fhswf/in/inf/java1/aufgabe08/Lottery6OutOf49.java new file mode 100644 index 0000000..b7c43f1 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe08/Lottery6OutOf49.java @@ -0,0 +1,81 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe08; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * A class, that simulates a 6 out of 49 Lottery. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Lottery6OutOf49 +{ + + private static final int MAX_NUMBER = 49; + + private ArrayList lotteryNumbers; + + private List lastDraw; + + private Integer superzahl; + + /** + * Creates a new valid object, that also starts the first draw. + * + */ + public Lottery6OutOf49() + { + lotteryNumbers = new ArrayList(MAX_NUMBER); + + for (int i = 1; i <= MAX_NUMBER; i++) + { + lotteryNumbers.add(i); + } + } + + /** + * Does the next draw. + * + */ + public void nextDraw() + { + lastDraw = FloydAndBentley.sample(lotteryNumbers, 7); + + superzahl = lastDraw.remove(lastDraw.size() - 1); + lastDraw.sort(null); + } + + /** + * Returns the last draw. + * + * @return Returns an immutable list of the last draw. + */ + public List getLastDraw() + { + if (lastDraw == null) + { + throw new IllegalStateException("First needs a draw."); + } + return Collections.unmodifiableList(lastDraw); + } + + /** + * Returns the super number. + * + * @return The super number of the last draw. + */ + public Integer getSuperzahl() + { + if (superzahl == null) + { + throw new IllegalStateException("First needs a sraw."); + } + return new Integer(superzahl); + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe08/LottoZiehungMain.java b/src/de/fhswf/in/inf/java1/aufgabe08/LottoZiehungMain.java new file mode 100644 index 0000000..32cb841 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe08/LottoZiehungMain.java @@ -0,0 +1,45 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe08; + +import java.util.ArrayList; +import java.util.List; + +/** + * Main class for the LottoZiehung. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class LottoZiehungMain +{ + + /** + * Private constructor for utility class. + * + */ + private LottoZiehungMain() + { + } + + /** + * Main function of the package. + * + * @param args + * Command line arguments. + */ + public static void main(String[] args) + { + List l = new ArrayList(); + l.add(1); + System.out.println(FloydAndBentley.sample(l, 1).size()); + System.out.println(FloydAndBentley.sample2(l, 1).size()); + + Lottery6OutOf49 spiel = new Lottery6OutOf49(); + spiel.nextDraw(); + List ziehung = spiel.getLastDraw(); + Integer sonderzahl = spiel.getSuperzahl(); + System.out.println(ziehung + " SonderZahl: " + sonderzahl); + } +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe09/WordCount.java b/src/de/fhswf/in/inf/java1/aufgabe09/WordCount.java new file mode 100644 index 0000000..ff806d1 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe09/WordCount.java @@ -0,0 +1,133 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe09; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Collections; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; + +/** + * A class, that counts words in files. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class WordCount +{ + + private TreeMap wordMap = new TreeMap<>(); + + /** + * Empty constructor. + * + */ + public WordCount() + { + + } + + /** + * Reads a file and counts the words. + * + * @param file + * The file to be read. + */ + public final void readFile(File file) + { + try (BufferedReader f = new BufferedReader(new FileReader(file))) + { + wordMap.clear(); + + String line = null; + + while ((line = f.readLine()) != null) + { + String[] lineArray = line + .split("[^\\p{IsAlphabetic}\\p{Digit}]+"); + for (String word : lineArray) + { + // Split creates empty String, if first char is a split char. + if (!word.isEmpty()) + { + word = word.toLowerCase(); + + Integer tmp = wordMap.get(word); + if (tmp != null) + { + wordMap.put(word, tmp + 1); + } + else + { + wordMap.put(word, new Integer(1)); + } + } + } + } + } + catch (IOException e) + { + e.printStackTrace(System.err); + } + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public final String toString() + { + StringBuilder ret = new StringBuilder(); + + for (Entry mapping : wordMap.entrySet()) + { + ret.append(mapping.getKey() + " = " + mapping.getValue() + "\n"); + } + + return ret.toString(); + } + + /** + * Returns all words from the file. + * + * @return An unmodifiable Set of the words from the file. + */ + public final Set getWords() + { + return Collections.unmodifiableSet(wordMap.keySet()); + } + + /** + * Returns the count of a word in a file. + * + * @param word + * The file to be counted. + * @return Returns the count of findings in the file. + */ + public final int getCount(String word) + { + if (word == null) + { + throw new IllegalArgumentException("Word must be a valid referece."); + } + if (word.isEmpty()) + { + throw new IllegalArgumentException("The word can't be empty."); + } + + Integer tmp = wordMap.get(word); + if (tmp == null) + { + return 0; + } + return tmp; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe09/WordCountTest.java b/src/de/fhswf/in/inf/java1/aufgabe09/WordCountTest.java new file mode 100644 index 0000000..c9d2e4d --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe09/WordCountTest.java @@ -0,0 +1,43 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe09; + +import java.io.File; + +/** + * Tests the WordCount class. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class WordCountTest +{ + + /** + * Private constructor for utility class. + * + */ + private WordCountTest() + { + } + + /** + * Tests the WordCount class. + * + * @param args + * Command line arguments. + */ + public static void main(String[] args) + { + File file = new File( + "C:/eclipse/workspace/java1/misc/Blatt9TestFile2.txt"); + + WordCount tmp = new WordCount(); + tmp.readFile(file); + System.out.println(tmp); + System.out.println("fgh = " + tmp.getCount("fgh")); + + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe1/HelloWorld.java b/src/de/fhswf/in/inf/java1/aufgabe1/HelloWorld.java deleted file mode 100644 index 6c72f75..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe1/HelloWorld.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe1; - -/** - * Begrüßung der Sprache Java. Diese Klasse begrüßt die Sprache Java mit einem - * traditionellen "Hello World!". - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public final class HelloWorld -{ - private HelloWorld() - { - - } - - /** - * Hauptmethode der Application. Begrüßung der Sprache Java. Diese Klasse - * begrüßt die Sprache Java mit einem traditionellen "Hello World!". - * - * @param args - * Übergabeparameter des Programms. - */ - public static void main(String[] args) - { - System.out.println("Anzahl der Argumente: " + args.length); - if (args.length != 0) - { - System.out.print("Ihr Name ist:"); - for (int i = 0; i < args.length; i++) - { - System.out.print(" " + args[i]); - } - System.out.println("."); - } - else - { - System.out.print("Kein Name vorhanden."); - } - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe4/Fill.java b/src/de/fhswf/in/inf/java1/aufgabe4/Fill.java deleted file mode 100644 index 20fdd84..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe4/Fill.java +++ /dev/null @@ -1,117 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe4; - -import java.util.Arrays; - -/** - * Tests the different methods to created a prefilled String. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public final class Fill -{ - private Fill() - { - } - - /** - * - * Creates a string of given length filled with the given char. - * - * @param length - * Length of the returned String. - * @param c - * Char the String is filled with. - * @return Returns the generated String. - */ - public static String createStringFilledWith(int length, char c) - { - // String that will be returned - String result = ""; - for (int i = 0; i < length; i++) - { - // Every iteration, a new string is created. - result += c; - } - // Return the build string - return result; - } - - /** - * - * Creates a string of given length filled with the given char. - * - * @param length - * Length of the returned String. - * @param c - * Char the String is filled with. - * @return Returns the generated String. - */ - public static String createStringFilledWith2(int length, char c) - { - // charArray that will be returned - char[] result = new char[length]; - - Arrays.fill(result, c); - - // Return the build string - return result.toString(); - } - - /** - * - * Creates a string of given length filled with the given char. - * - * @param length - * Length of the returned String. - * @param c - * Char the String is filled with. - * @return Returns the generated String. - */ - public static String createStringFilledWith3(int length, char c) - { - // String that will be returned - StringBuilder result = new StringBuilder(length); - for (int i = 0; i < length; i++) - { - // Every iteration, a new string is created. - result.append(c); - } - // Return the build string - return result.toString(); - } - - /** - * - * Calls the function createStringFilledWith() and measures its runtime. - * - * @param args - * Console line arguments. - */ - public static void main(String[] args) - { - final int anzDurchlaefe = 100000000; - - long start; - start = System.currentTimeMillis(); // Gets current time in µs - Fill.createStringFilledWith(anzDurchlaefe, 'x'); // Function call - // Calculates the time runtime of creatStringFilledWith() - System.out.println("Elapsed time: " - + (System.currentTimeMillis() - start) + " ms"); - - start = System.currentTimeMillis(); // Gets current time in µs - Fill.createStringFilledWith2(anzDurchlaefe, 'x'); // Function call - // Calculates the time runtime of creatStringFilledWith2() - System.out.println("Elapsed time: " - + (System.currentTimeMillis() - start) + " ms"); - - start = System.currentTimeMillis(); // Gets current time in µs - Fill.createStringFilledWith3(anzDurchlaefe, 'x'); // Function call - // Calculates the time runtime of creatStringFilledWith3() - System.out.println("Elapsed time: " - + (System.currentTimeMillis() - start) + " ms"); - } -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe5/Bank.java b/src/de/fhswf/in/inf/java1/aufgabe5/Bank.java deleted file mode 100644 index 22cb577..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe5/Bank.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe5; - -import java.math.BigDecimal; - -/** - * Package for main function. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public final class Bank -{ - - /** - * Prevents instantiation of the unity class Bank. - * - */ - private Bank() - { - - } - - /** - * Main function for testing the Bank system. - * - * @param args - * Shell arguments - */ - public static void main(String[] args) - { - Person dieter = new Person("Dieter", "Hallowegers"); - - Konto giro = new Konto(dieter, new BigDecimal(100)); - - try - { - giro.einzahlen(new BigDecimal(100)); - giro.abheben(new BigDecimal(200)); - - System.out.println(giro.getKontostand()); - - giro.abheben(new BigDecimal(1)); - } - catch (IllegalArgumentException e) - { - System.out.println(e); - } - - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe5/Konto.java b/src/de/fhswf/in/inf/java1/aufgabe5/Konto.java deleted file mode 100644 index faac8f6..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe5/Konto.java +++ /dev/null @@ -1,119 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe5; - -import java.math.BigDecimal; - -/** - * An implementation of the Konto class which represents a bank account. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class Konto -{ - - private BigDecimal guthaben; - - private BigDecimal dispo; - - private Person besitzer; - - /** - * Creates an account with an owner, a dispo and an initial balance. - * - * @param besitzer - * Owner of the account - * @param dispo - * Dispo of the account - */ - public Konto(Person besitzer, BigDecimal dispo) - { - - if (besitzer == null) - { - throw new IllegalArgumentException("Besitzer can't be null"); - } - if (dispo == null) - { - throw new IllegalArgumentException("Dispo can't be null"); - } - if (dispo.compareTo(BigDecimal.ZERO) < 0) - { - throw new IllegalArgumentException("Dispo can't be negative"); - } - - this.besitzer = besitzer; - this.dispo = dispo; - this.guthaben = new BigDecimal(0); - - this.besitzer.addKonto(this); - - } - - /** - * Function for withdrawing money from the account. - * - * @param betrag - * Amount of cash to be withdrawn - */ - public void abheben(BigDecimal betrag) - { - if (betrag == null) - { - throw new IllegalArgumentException("Betrag can't be null"); - } - if (betrag.compareTo(BigDecimal.ZERO) < 0) - { - throw new IllegalArgumentException("Betrag can't be negative"); - } - if (guthaben.add(dispo).compareTo(betrag) < 0) - { - throw new IllegalArgumentException("Betrag surpasses dispo limit"); - } - - guthaben = guthaben.subtract(betrag); - } - - /** - * Function to deposit money into the account. - * - * @param betrag - * Amount of cash to be disposed - */ - public void einzahlen(BigDecimal betrag) - { - if (betrag == null) - { - throw new IllegalArgumentException("Betrag can't be null"); - } - if (betrag.compareTo(BigDecimal.ZERO) < 0) - { - throw new IllegalArgumentException("Betrag can't be negative"); - } - - guthaben = guthaben.add(betrag); - } - - /** - * Returns the actual balance. - * - * @return Returns the actual balance. - */ - public BigDecimal getKontostand() - { - return guthaben; - } - - /** - * Just to get the owners name. - * - * @return Returns the owners name. - */ - public Person getBesitzer() - { - return besitzer; - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe5/Person.java b/src/de/fhswf/in/inf/java1/aufgabe5/Person.java deleted file mode 100644 index 15de304..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe5/Person.java +++ /dev/null @@ -1,86 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe5; - -import java.util.ArrayList; -import java.util.List; - -/** - * A person which can own an account. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class Person -{ - private String vorname; - - private String nachname; - - private List kontenliste = new ArrayList<>(); - - /** - * Creates a person with first and last name. - * - * @param vorname - * First name of the person - * @param nachname - * Last name of the person - */ - public Person(String vorname, String nachname) - { - if (vorname == null) - { - throw new IllegalArgumentException("Vorname can't be null"); - } - if (nachname == null) - { - throw new IllegalArgumentException("Nachname can't be null"); - } - if (vorname.isEmpty()) - { - throw new IllegalArgumentException("Vorname can't be empty"); - } - if (nachname.isEmpty()) - { - throw new IllegalArgumentException("Nachname can't be empty"); - } - this.vorname = vorname; - this.nachname = nachname; - } - - /** - * Just for getting the persons name. - * - * @return Returns the full name of the person - */ - @Override - public String toString() - { - return vorname + " " + nachname; - } - - /** - * For adding the back reference to the Konto. - * - * @param konto - * Konto that will be added to the person. - */ - public void addKonto(Konto konto) - { - if (konto == null) - { - throw new IllegalArgumentException("Konto can't be empty"); - } - if (konto.getBesitzer() != this) - { - throw new IllegalArgumentException("Person must be owner of Konto"); - } - if (!kontenliste.contains(konto)) - { - kontenliste.add(konto); - } - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe5/Student.java b/src/de/fhswf/in/inf/java1/aufgabe5/Student.java deleted file mode 100644 index 769c91a..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe5/Student.java +++ /dev/null @@ -1,28 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe5; - -/** - * Extends Person and represents a student. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class Student extends Person -{ - - /** - * Simply calls the super constructor to initialize person. - * - * @param vorname - * First name of the student - * @param nachname - * Last name of the student - */ - public Student(String vorname, String nachname) - { - super(vorname, nachname); - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe6/Aufgabe6.java b/src/de/fhswf/in/inf/java1/aufgabe6/Aufgabe6.java deleted file mode 100644 index 0fbaf00..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe6/Aufgabe6.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe6; - -import java.util.Iterator; -import java.util.Vector; - -/** - * Main function for testing Vectors and Lists. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public final class Aufgabe6 -{ - - /** - * Prevents instantiation of the utility class. - * - */ - private Aufgabe6() - { - } - - /** - * Main function for testing Vectors and Lists. - * - * @param args - * Command line arguments. - */ - public static void main(String[] args) - { - final int testLength = 10000000; - Vector test = new Vector<>(testLength); - Integer testGet; - - for (int i = 0; i < test.capacity(); i++) - { - test.add(i); - } - - long start; - start = System.currentTimeMillis(); // Gets current time in ms - - for (int i = 0; i < test.size(); i++) - { - testGet = test.elementAt(i); - } - - // Calculates the runtime of for - System.out.println("Elapsed time: " - + (System.currentTimeMillis() - start) + " ms"); - - start = System.currentTimeMillis(); // Gets current time in ms - - for (Iterator it = test.iterator(); it.hasNext();) - { - testGet = it.next(); - } - - // Calculates the runtime of for - System.out.println("Elapsed time: " - + (System.currentTimeMillis() - start) + " ms"); - - start = System.currentTimeMillis(); // Gets current time in ms - - for (Integer integer : test) - { - testGet = integer; - } - - // Calculates the runtime of foreach - System.out.println("Elapsed time: " - + (System.currentTimeMillis() - start) + " ms"); - - ListBenchmark.startBenchmark(); - - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe6/ListBenchmark.java b/src/de/fhswf/in/inf/java1/aufgabe6/ListBenchmark.java deleted file mode 100644 index 24e4ed4..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe6/ListBenchmark.java +++ /dev/null @@ -1,137 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe6; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Random; -import java.util.Vector; - -/** - * Class that benchmarks List implementations. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public final class ListBenchmark -{ - - private static final Integer TESTOBJ = new Integer(214); - - /** - * Prevents instantiation of the utility class. - * - */ - private ListBenchmark() - { - } - - /** - * Starts a benchmark of different Lists and different test cases. - * - */ - public static void startBenchmark() - { - final int times = 100000; - - benchmarkGrouping("ArrayList", new ArrayList(times), times); - benchmarkGrouping("Vector", new Vector(times), times); - benchmarkGrouping("LinkedList", new LinkedList(), times); - - } - - /** - * Groups the 3 different benchmark versions together. - * - * @param variant - * Name of the list type. - * @param list - * The list which will be benchmarked. - * @param times - * How often will be inserted. - */ - private static void benchmarkGrouping(String variant, List list, - int times) - { - System.out.print(variant + " am Anfang: "); - System.out.println(benchmarkAtBeginning(list, times) + " ms"); - System.out.print(variant + " am Ende: "); - System.out.println(benchmarkAtEnding(list, times) + " ms"); - System.out.print(variant + " zufälliger Index: "); - System.out.println(benchmarkAtRandom(list, times) + " ms"); - } - - /** - * Inserts new value at the beginning of the list. - * - * @param list - * List to be tested. - * @param times - * How many inserts are done. - * @return Time in milliseconds that the action took. - */ - private static long benchmarkAtBeginning(List list, int times) - { - long start = System.currentTimeMillis(); // Gets current time in µs - - for (int i = 0; i < times; i++) - { - list.add(0, TESTOBJ); - } - - return (System.currentTimeMillis() - start); // Returns runtime - } - - /** - * Inserts new value at the end of the list. - * - * @param list - * List to be tested. - * @param times - * How many inserts are done. - * @return Time in milliseconds that the action took. - */ - private static long benchmarkAtEnding(List list, int times) - { - long start = System.currentTimeMillis(); // Gets current time in µs - - for (int i = 0; i < times; i++) - { - list.add(TESTOBJ); - } - - return (System.currentTimeMillis() - start); // Returns runtime - } - - /** - * Inserts new value at a random index of the list. - * - * @param list - * List to be tested. - * @param times - * How many inserts are done. - * @return Time in milliseconds that the action took. - */ - private static long benchmarkAtRandom(List list, int times) - { - - int[] randVal = new int[times]; - Random rand = new Random(); - - for (int i = 0; i < randVal.length; i++) - { - randVal[i] = rand.nextInt(list.size() + i + 1); - } - - long start = System.currentTimeMillis(); // Gets current time in µs - - for (int i = 0; i < times; i++) - { - list.add(randVal[i], TESTOBJ); - } - - return (System.currentTimeMillis() - start); // Returns runtime - } -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java deleted file mode 100644 index f5bf021..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe7; - -/** - * This player always chooses the same shell. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class GleichSpieler extends Spieler -{ - /* - * (non-Javadoc) - * - * @see de.fhswf.in.inf.java1.aufgabe7.Spieler#zweiterTipp(int) - */ - @Override - public final int zweiterTipp(int leeresHuetchen) - { - return getErsterTipp(); - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java b/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java deleted file mode 100644 index 71e23a0..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe7; - -/** - * Calculates the the success rate of different players. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public final class Simulation -{ - - private static final int VERSUCHE = 100000; - - /** - * Prevents instantiation of the utility class. - * - */ - private Simulation() - { - } - - /** - * Makes anzahl games. - * - * @param anzahl - * How often the game will be run. - * @return Victory count - */ - private static int nDurchlaufeAusfuehren(Spiel spiel, Spieler spieler, - int anzahl) - { - - int erfolge = 0; - for (int i = 0; i < anzahl; i++) - { - if (spiel.spielDurchlauf(spieler)) - { - erfolge++; - } - } - - return erfolge; - } - - /** - * Calculates the success rate of every player. - * - * @param erfolg - * An array with the last game stats. - */ - private static double wahrscheinlichkeitErrechnen(int erfolge, int versuche) - { - return (double) erfolge / versuche; - } - - /** - * Main method of the package. - * - * @param args - * Command line arguments - */ - public static void main(String[] args) - { - - Spieler[] spieler = {new GleichSpieler(), new WechselSpieler(), - new ZufallsSpieler(0.5)}; - - Spiel spiel = new Spiel(); - - for (int i = 0; i < spieler.length; i++) - { - int spielerGewinne = nDurchlaufeAusfuehren(spiel, spieler[i], - VERSUCHE); - double spielerProb = wahrscheinlichkeitErrechnen(spielerGewinne, - VERSUCHE); - System.out.printf("%17s war %05.2f%s erfolgreich\n", spieler[i] - .getClass().getSimpleName(), spielerProb * 100, "%"); - } - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java b/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java deleted file mode 100644 index f33e031..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java +++ /dev/null @@ -1,86 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe7; - -import java.util.Random; - -/** - * Simulates a shell game. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class Spiel -{ - private int nussPlatz = -1; - - public static final int MAXHUET = 3; - - public static final Random RAND = new Random(); - - /** - * Places the pea under a shell. - * - */ - private void nussVerstecken() - { - nussPlatz = RAND.nextInt(MAXHUET); - } - - /** - * Returns an empty shell, which the user didn't choose. - * - * @param ersterTipp - * The first choice of the player. - * @return An empty shell number of the remaining shells - */ - private int zeigeHuetchen(int ersterTipp) - { - if (ersterTipp < 0 || ersterTipp > MAXHUET) - { - throw new IllegalArgumentException("Tip outside of boundaries."); - } - int erg = -1; - if (ersterTipp != nussPlatz) - { - erg = MAXHUET - nussPlatz - ersterTipp; - } - else - { - erg = (ersterTipp + RAND.nextInt(MAXHUET - 1)) % MAXHUET; - } - return erg; - } - - /** - * Returns the shell number with the pea. - * - * @return The shell number - */ - private boolean aufloesung(int zweiterTipp) - { - return nussPlatz == zweiterTipp; - } - - /** - * Simulates a complete game. - * - * @param spieler - * The player who plays. - * @return Boolean, if the player choose correct. - */ - public final boolean spielDurchlauf(Spieler spieler) - { - if (spieler == null) - { - throw new IllegalArgumentException("A player hast to play."); - } - int tipp = -1; - nussVerstecken(); - tipp = spieler.ersterTipp(); - tipp = spieler.zweiterTipp(zeigeHuetchen(tipp)); - return aufloesung(tipp); - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java deleted file mode 100644 index 40c7254..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe7; - -/** - * Represents a player that chooses a shell. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public abstract class Spieler -{ - - private int ersterTipp = -1; - - /** - * The first shell the player chooses. - * - * @return The first guess for a shell - */ - public final int ersterTipp() - { - ersterTipp = Spiel.RAND.nextInt(Spiel.MAXHUET); - return ersterTipp; - } - - /** - * Getter for the first choice. - * - * @return The first choice - */ - public final int getErsterTipp() - { - return ersterTipp; - } - - /** - * After the first guess, the player must choose a second shell. - * - * @param leeresHuetchen - * The shell, the game showed as empty - * @return The second guess for the right shell - */ - public abstract int zweiterTipp(int leeresHuetchen); - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java deleted file mode 100644 index 5d9b976..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java +++ /dev/null @@ -1,22 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe7; - -/** - * This player always changes the shell. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class WechselSpieler extends Spieler -{ - /* (non-Javadoc) - * @see de.fhswf.in.inf.java1.aufgabe7.Spieler#zweiterTipp(int) - */ - @Override - public int zweiterTipp(int leeresHuetchen) - { - return Spiel.MAXHUET - getErsterTipp() - leeresHuetchen; - } -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/ZufallsSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/ZufallsSpieler.java deleted file mode 100644 index f633d31..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe7/ZufallsSpieler.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe7; - -/** - * A player that switches strategy with a certain probability. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class ZufallsSpieler extends WechselSpieler -{ - - private double wahrscheinlichkeit = 0; - - /** - * Initializes a player which switches strategies. - * - * @param wahrscheinlichkeit - * Probability of switching strategy. - */ - public ZufallsSpieler(double wahrscheinlichkeit) - { - if (wahrscheinlichkeit < 0.0 | wahrscheinlichkeit > 1.0) - { - throw new IllegalArgumentException( - "Only a probability between 0 and 100 is allowed."); - } - - this.wahrscheinlichkeit = wahrscheinlichkeit; - } - - /* - * (non-Javadoc) - * - * @see de.fhswf.in.inf.java1.aufgabe7.WechselSpieler#zweiterTipp(int) - */ - @Override - public final int zweiterTipp(int leeresHuetchen) - { - if (Spiel.RAND.nextDouble() < wahrscheinlichkeit) - { - return getErsterTipp(); - } - else - { - return super.zweiterTipp(leeresHuetchen); - } - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe8/FloydAndBentley.java b/src/de/fhswf/in/inf/java1/aufgabe8/FloydAndBentley.java deleted file mode 100644 index 5416fe5..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe8/FloydAndBentley.java +++ /dev/null @@ -1,130 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe8; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -/** - * Utility class for the Floyd and Bentley algorithm. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public final class FloydAndBentley -{ - - private static Random rnd = new Random(); - - /** - * Private constructor for utility class. - * - */ - private FloydAndBentley() - { - } - - /** - * A function which chooses a sample from a List with the Floyd and Bentley - * algorithm. (Recursive algorithm) - * - * @param - * Type of the Lists. - * @param s - * The set from which the sample is chosen. - * @param k - * The number of items to be chosen. - * @return Return the sample. - */ - public static List sample(List s, int k) - { - if (s.isEmpty()) - { - throw new IllegalArgumentException("Set is empty"); - } - if (k > s.size()) - { - throw new IllegalArgumentException("k is bigger than the Set"); - } - - List ret = new ArrayList<>(k); - sampleRec(s, k, ret); - - return ret; - - } - - /** - * The recursive implementation of the Floyd and Bentley algorithm. - * - * @param - * Type of the Lists. - * @param s - * The set from which the sample is chosen. - * @param k - * The number of items to be chosen. - * @param ret - * Return the sample. - */ - private static void sampleRec(List s, int k, List ret) - { - if (ret.size() >= k) - { - return; - } - - T val = s.get(rnd.nextInt(s.size() - (k - ret.size() - 1))); - - if (ret.contains(val)) - { - val = s.get(s.size() - (k - ret.size() - 1)); - } - - ret.add(val); - - sampleRec(s, k, ret); - - } - - /** - * A function which chooses a sample from a List with the Floyd and Bentley - * algorithm. (Non recursive algorithm) - * - * @param - * Type of the Lists. - * @param s - * The set from which the sample is chosen. - * @param k - * The number of items to be chosen. - * @return Return the sample. - */ - public static List sample2(List s, int k) - { - if (s.isEmpty()) - { - throw new IllegalArgumentException("Set is empty"); - } - if (k > s.size()) - { - throw new IllegalArgumentException("k is bigger than the Set"); - } - - List ret = new ArrayList<>(k); - - for (int i = 0; i < k; i++) - { - T val = s.get(rnd.nextInt(s.size() - (k - ret.size() - 1))); - - if (ret.contains(val)) - { - val = s.get(s.size() - (k - ret.size() - 1)); - } - - ret.add(val); - } - - return ret; - } -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe8/Lottery6OutOf49.java b/src/de/fhswf/in/inf/java1/aufgabe8/Lottery6OutOf49.java deleted file mode 100644 index 3342ebd..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe8/Lottery6OutOf49.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe8; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * A class, that simulates a 6 out of 49 Lottery. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class Lottery6OutOf49 -{ - - private static final int MAX_NUMBER = 49; - - private ArrayList lotteryNumbers; - - private List lastDraw; - - private Integer superzahl; - - /** - * Creates a new valid object, that also starts the first draw. - * - */ - public Lottery6OutOf49() - { - lotteryNumbers = new ArrayList(MAX_NUMBER); - - for (int i = 1; i <= MAX_NUMBER; i++) - { - lotteryNumbers.add(i); - } - } - - /** - * Does the next draw. - * - */ - public void nextDraw() - { - lastDraw = FloydAndBentley.sample(lotteryNumbers, 7); - - superzahl = lastDraw.remove(lastDraw.size() - 1); - lastDraw.sort(null); - } - - /** - * Returns the last draw. - * - * @return Returns an immutable list of the last draw. - */ - public List getLastDraw() - { - if (lastDraw == null) - { - throw new IllegalStateException("First needs a draw."); - } - return Collections.unmodifiableList(lastDraw); - } - - /** - * Returns the super number. - * - * @return The super number of the last draw. - */ - public Integer getSuperzahl() - { - if (superzahl == null) - { - throw new IllegalStateException("First needs a sraw."); - } - return new Integer(superzahl); - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe8/LottoZiehungMain.java b/src/de/fhswf/in/inf/java1/aufgabe8/LottoZiehungMain.java deleted file mode 100644 index f4cb322..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe8/LottoZiehungMain.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe8; - -import java.util.ArrayList; -import java.util.List; - -/** - * Main class for the LottoZiehung. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public final class LottoZiehungMain -{ - - /** - * Private constructor for utility class. - * - */ - private LottoZiehungMain() - { - } - - /** - * Main function of the package. - * - * @param args - * Command line arguments. - */ - public static void main(String[] args) - { - List l = new ArrayList(); - l.add(1); - System.out.println(FloydAndBentley.sample(l, 1).size()); - System.out.println(FloydAndBentley.sample2(l, 1).size()); - - Lottery6OutOf49 spiel = new Lottery6OutOf49(); - spiel.nextDraw(); - List ziehung = spiel.getLastDraw(); - Integer sonderzahl = spiel.getSuperzahl(); - System.out.println(ziehung + " SonderZahl: " + sonderzahl); - } -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java b/src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java deleted file mode 100644 index a6d90ef..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java +++ /dev/null @@ -1,133 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe9; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.Collections; -import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeMap; - -/** - * A class, that counts words in files. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class WordCount -{ - - private TreeMap wordMap = new TreeMap<>(); - - /** - * Empty constructor. - * - */ - public WordCount() - { - - } - - /** - * Reads a file and counts the words. - * - * @param file - * The file to be read. - */ - public final void readFile(File file) - { - try (BufferedReader f = new BufferedReader(new FileReader(file))) - { - wordMap.clear(); - - String line = null; - - while ((line = f.readLine()) != null) - { - String[] lineArray = line - .split("[^\\p{IsAlphabetic}\\p{Digit}]+"); - for (String word : lineArray) - { - // Split creates empty String, if first char is a split char. - if (!word.isEmpty()) - { - word = word.toLowerCase(); - - Integer tmp = wordMap.get(word); - if (tmp != null) - { - wordMap.put(word, tmp + 1); - } - else - { - wordMap.put(word, new Integer(1)); - } - } - } - } - } - catch (IOException e) - { - e.printStackTrace(System.err); - } - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public final String toString() - { - StringBuilder ret = new StringBuilder(); - - for (Entry mapping : wordMap.entrySet()) - { - ret.append(mapping.getKey() + " = " + mapping.getValue() + "\n"); - } - - return ret.toString(); - } - - /** - * Returns all words from the file. - * - * @return An unmodifiable Set of the words from the file. - */ - public final Set getWords() - { - return Collections.unmodifiableSet(wordMap.keySet()); - } - - /** - * Returns the count of a word in a file. - * - * @param word - * The file to be counted. - * @return Returns the count of findings in the file. - */ - public final int getCount(String word) - { - if (word == null) - { - throw new IllegalArgumentException("Word must be a valid referece."); - } - if (word.isEmpty()) - { - throw new IllegalArgumentException("The word can't be empty."); - } - - Integer tmp = wordMap.get(word); - if (tmp == null) - { - return 0; - } - return tmp; - } - -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe9/WordCountTest.java b/src/de/fhswf/in/inf/java1/aufgabe9/WordCountTest.java deleted file mode 100644 index a1364c9..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe9/WordCountTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe9; - -import java.io.File; - -/** - * Tests the WordCount class. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public final class WordCountTest -{ - - /** - * Private constructor for utility class. - * - */ - private WordCountTest() - { - } - - /** - * Tests the WordCount class. - * - * @param args - * Command line arguments. - */ - public static void main(String[] args) - { - File file = new File( - "C:/eclipse/workspace/java1/misc/Blatt9TestFile2.txt"); - - WordCount tmp = new WordCount(); - tmp.readFile(file); - System.out.println(tmp); - System.out.println("fgh = " + tmp.getCount("fgh")); - - } - -} -- cgit v1.2.3-70-g09d2