├── src ├── pgdp16 │ ├── blatt11 │ │ ├── set │ │ │ ├── List.java │ │ │ └── Set.java │ │ └── exception │ │ │ ├── NotEnoughLetter.java │ │ │ ├── NotEnoughExc.java │ │ │ ├── NotLongEnoughExc.java │ │ │ ├── NotEnoughLower.java │ │ │ ├── NotEnoughNumber.java │ │ │ ├── NotEnoughUpper.java │ │ │ ├── NotEnoughSpecial.java │ │ │ ├── IllegalCharExc.java │ │ │ ├── Password.java │ │ │ ├── ExceptionTest.java │ │ │ └── PasswordTest.java │ ├── blatt12 │ │ ├── map │ │ │ ├── Fun.java │ │ │ └── Map.java │ │ ├── rock │ │ │ ├── Player.java │ │ │ ├── RockPaperScissors.java │ │ │ ├── RockPaperScissorsTest.java │ │ │ └── PlayerTest.java │ │ └── readMe.md │ ├── blatt09 │ │ ├── pgdp │ │ │ ├── PinkLady.java │ │ │ ├── GrannySmith.java │ │ │ ├── Apple.java │ │ │ ├── Banana.java │ │ │ ├── Pineapple.java │ │ │ ├── Fruit.java │ │ │ ├── FruitBasket.java │ │ │ └── FruitTest.java │ │ └── fight │ │ │ ├── Vegetarian.java │ │ │ ├── Readme.md │ │ │ ├── Main.java │ │ │ ├── Predator.java │ │ │ ├── Horse.java │ │ │ ├── Rabbit.java │ │ │ ├── Elephant.java │ │ │ ├── Snake.java │ │ │ ├── Leopard.java │ │ │ ├── Penguin.java │ │ │ ├── IO.java │ │ │ ├── Move.java │ │ │ ├── Game.java │ │ │ ├── Animal.java │ │ │ ├── MoveTest.java │ │ │ ├── VegetarianTest.java │ │ │ ├── MainTest.java │ │ │ ├── PredatorTest.java │ │ │ └── theWinnerTest_Marco.java │ ├── blatt06 │ │ ├── MatrixMultOptimization.java │ │ ├── Toolbox.java │ │ ├── MatrixMultOptimizationTest.java │ │ └── ToolboxTest.java │ ├── blatt08 │ │ ├── MatrixMultOptMemoization.java │ │ ├── pgdp │ │ │ ├── Money.java │ │ │ ├── Bank.java │ │ │ ├── BankAccount.java │ │ │ ├── BankAccountTest.java │ │ │ └── MoneyTest.java │ │ ├── SymmetricStack.java │ │ └── MatrixMultOptMemoizationTest.java │ └── blatt07 │ │ ├── Tailrec1.java │ │ ├── HeadList.java │ │ ├── MiniJava.java │ │ └── DameSpiel.java ├── gad17 │ ├── blatt06 │ │ ├── SortingBase.java │ │ ├── Mergesort.java │ │ ├── Quicksort.java │ │ └── SortTest.java │ ├── blatt05 │ │ ├── HashableFactory.java │ │ ├── IntHashableFactory.java │ │ ├── StringHashableFactory.java │ │ ├── DoubleHashable.java │ │ ├── Pair.java │ │ ├── DoubleHashInt.java │ │ ├── DoubleHashString.java │ │ ├── DoubleHashTable.java │ │ ├── DoubleHashIntTest.java │ │ ├── DoubleHashStringTest.java │ │ └── FactoryTest.java │ ├── blatt04 │ │ ├── IOExceptionConsumer.java │ │ ├── IResponse.java │ │ ├── Mutable.java │ │ ├── IRequest.java │ │ ├── StoreResponse.java │ │ ├── ReadRequest.java │ │ ├── StoreRequest.java │ │ ├── RequestVisitor.java │ │ ├── ResponseVisitor.java │ │ ├── ReadResponse.java │ │ ├── HashString.java │ │ ├── StoreTest.java │ │ ├── MasterTest.java │ │ ├── Client.java │ │ ├── Store.java │ │ └── ClientTest.java │ ├── blatt11 │ │ ├── ConnectedComponents.java │ │ ├── BFS.java │ │ ├── Graph.java │ │ └── ConnectedComponentsTest.java │ ├── blatt03 │ │ ├── EmptyIntervalTest.java │ │ ├── NonEmptyIntervalTest.java │ │ ├── DynamicStack.java │ │ ├── StackyQueue.java │ │ ├── StackyQueueTest.java │ │ ├── RingQueue.java │ │ ├── DynamicStackTest.java │ │ ├── DynamicaTests.java │ │ └── Interval.java │ ├── blatt07 │ │ └── RadixSort.java │ ├── blatt08 │ │ ├── BinomialHeap.java │ │ ├── BinomialTreeNode.java │ │ └── BinomialTreeNodeTest.java │ ├── blatt09 │ │ ├── AVLTreeNodeBuilder.java │ │ ├── AVLTreeTest.java │ │ ├── AVLTree.java │ │ └── AVLTreeNode.java │ └── blatt02 │ │ ├── Interval.java │ │ └── BinSea.java ├── grnvs18 │ └── assignment2 │ │ ├── Timeout.java │ │ ├── GRNVS_RAW_Mock.java │ │ ├── Arguments.java │ │ ├── Assignment2.java │ │ └── GRNVS_RAW.java └── eti18 │ ├── ha3 │ ├── AcceptNFA.java │ ├── Powerset.java │ ├── AcceptDFA.java │ ├── NFA.java │ ├── Transition.java │ ├── State.java │ ├── AcceptDFATest.java │ ├── AcceptNFATest.java │ ├── PowersetTest.java │ └── DFA.java │ └── ha5 │ ├── IsEmpty.java │ ├── IsFinite.java │ ├── NFA.java │ ├── Equivalent.java │ ├── Intersection.java │ ├── Transition.java │ ├── State.java │ ├── Operations.java │ ├── IsEmptyTest.java │ ├── IsFiniteTest.java │ └── DFA.java ├── .gitignore └── README.md /src/pgdp16/blatt11/set/List.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.set; 2 | 3 | public class List { 4 | } 5 | -------------------------------------------------------------------------------- /src/pgdp16/blatt12/map/Fun.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt12.map; 2 | 3 | public interface Fun { 4 | R apply(T x); 5 | } 6 | -------------------------------------------------------------------------------- /src/gad17/blatt06/SortingBase.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt06; 2 | 3 | interface SortingBase { 4 | void sort(int[] numbers); 5 | 6 | String getName(); 7 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt12/map/Map.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt12.map; 2 | 3 | public class Map { 4 | public static void map(Fun f, T[] a, R[] b, int n) { 5 | //TODO 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/pgdp/PinkLady.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.pgdp; 2 | 3 | public class PinkLady extends Apple { 4 | public int shelfLife() { 5 | //TODO 6 | return -1; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/pgdp/GrannySmith.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.pgdp; 2 | 3 | public class GrannySmith extends Apple { 4 | public int shelfLife() { 5 | //TODO 6 | return -1; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/NotEnoughLetter.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | public class NotEnoughLetter extends NotEnoughExc { 4 | public NotEnoughLetter(int should, int is) { 5 | //TODO 6 | super(-1, -1); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | /.idea/ 14 | /out/ 15 | -------------------------------------------------------------------------------- /src/gad17/blatt06/Mergesort.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt06; 2 | 3 | public class Mergesort implements SortingBase { 4 | public void sort(int[] numbers) { 5 | // TODO 6 | } 7 | 8 | public String getName() { 9 | return "Mergesort"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/gad17/blatt06/Quicksort.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt06; 2 | 3 | public class Quicksort implements SortingBase { 4 | public void sort(int[] numbers) { 5 | // TODO 6 | } 7 | 8 | public String getName() { 9 | return "Quicksort"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/gad17/blatt05/HashableFactory.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | /** 4 | * Dieses Interface beschreibt eine Fabrik, die aus 5 | * einer Größe ein Hashable-Objekt erzeugt. 6 | */ 7 | public interface HashableFactory { 8 | DoubleHashable create(int size); 9 | } 10 | -------------------------------------------------------------------------------- /src/gad17/blatt05/IntHashableFactory.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | public class IntHashableFactory implements HashableFactory { 4 | 5 | @Override 6 | public DoubleHashable create(int size) { 7 | return new DoubleHashInt(size); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/gad17/blatt05/StringHashableFactory.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | public class StringHashableFactory implements HashableFactory { 4 | 5 | @Override 6 | public DoubleHashable create(int size) { 7 | return new DoubleHashString(size); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/pgdp/Apple.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.pgdp; 2 | 3 | public class Apple extends Fruit { 4 | public boolean isApple() { 5 | //TODO 6 | return false; 7 | } 8 | 9 | public int shelfLife() { 10 | //TODO 11 | return -1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/pgdp/Banana.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.pgdp; 2 | 3 | public class Banana extends Fruit { 4 | public boolean isBanana() { 5 | //TODO 6 | return false; 7 | } 8 | 9 | public int shelfLife() { 10 | //TODO 11 | return -1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/NotEnoughExc.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | public class NotEnoughExc extends Exception { 4 | final int should, is; 5 | 6 | public NotEnoughExc(int should, int is) { 7 | //TODO 8 | this.is = -1; 9 | this.should = -1; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/pgdp/Pineapple.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.pgdp; 2 | 3 | public class Pineapple extends Fruit { 4 | public boolean isPineapple() { 5 | //TODO 6 | return false; 7 | } 8 | 9 | public int shelfLife() { 10 | //TODO 11 | return -1; 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /src/pgdp16/blatt12/rock/Player.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt12.rock; 2 | 3 | public class Player implements Runnable { 4 | @Override 5 | public void run() { 6 | //TODO 7 | } 8 | 9 | public int getChoice() throws InterruptedException { 10 | //TODO 11 | return -1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/NotLongEnoughExc.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | public class NotLongEnoughExc extends Exception { 4 | final private int should, is; 5 | 6 | public NotLongEnoughExc(int should, int is) { 7 | //TODO 8 | this.should = -1; 9 | this.is = -1; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/pgdp16/blatt12/rock/RockPaperScissors.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt12.rock; 2 | 3 | public class RockPaperScissors implements Runnable { 4 | public static int determineWinner(int p1, int p2) { 5 | //TODO 6 | return -1; 7 | } 8 | 9 | @Override 10 | public void run() { 11 | //TODO 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pgdp16/blatt06/MatrixMultOptimization.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt06; 2 | 3 | public class MatrixMultOptimization { 4 | 5 | public static int f(int[][] mm) { 6 | return f(mm, 0, mm.length - 1); 7 | } 8 | 9 | public static int f(int[][] mm, int i, int j) { 10 | // TODO 11 | return -1; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/grnvs18/assignment2/Timeout.java: -------------------------------------------------------------------------------- 1 | package grnvs18.assignment2; 2 | 3 | public class Timeout { 4 | private int timeout; 5 | 6 | public Timeout(int milliseconds) { 7 | this.timeout = milliseconds; 8 | } 9 | 10 | public int getTimeout() { return timeout; } 11 | public void setTimeout(int timeout) { this.timeout = timeout; } 12 | } 13 | -------------------------------------------------------------------------------- /src/pgdp16/blatt08/MatrixMultOptMemoization.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt08; 2 | 3 | public class MatrixMultOptMemoization { 4 | 5 | public /*static*/ int f(int[][] mm) { 6 | return f(mm, 0, mm.length - 1); 7 | } 8 | 9 | public /*static*/ int f(int[][] mm, int i, int j) { 10 | // TODO 11 | return -1; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Vegetarian.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | /** 4 | * Klasse der Vegetarier. 5 | */ 6 | public class Vegetarian extends Animal { 7 | 8 | /** 9 | * Dem Konstruktor wird das Geschlecht des Tiers uebergeben. 10 | */ 11 | public Vegetarian(boolean female) { 12 | //TODO 13 | super(false); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/NotEnoughLower.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | public class NotEnoughLower extends NotEnoughLetter { 4 | public NotEnoughLower(int should, int is) { 5 | //TODO 6 | super(-1, -1); 7 | } 8 | 9 | public String toString() { 10 | //TODO 11 | return this.getClass().getSimpleName(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/NotEnoughNumber.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | public class NotEnoughNumber extends NotEnoughExc { 4 | public NotEnoughNumber(int should, int is) { 5 | //TODO 6 | super(-1, -1); 7 | } 8 | 9 | public String toString() { 10 | //TODO 11 | return this.getClass().getSimpleName(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/NotEnoughUpper.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | public class NotEnoughUpper extends NotEnoughLetter { 4 | public NotEnoughUpper(int should, int is) { 5 | //TODO 6 | super(-1, -1); 7 | } 8 | 9 | public String toString() { 10 | //TODO 11 | return this.getClass().getSimpleName(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/NotEnoughSpecial.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | public class NotEnoughSpecial extends NotEnoughExc { 4 | public NotEnoughSpecial(int should, int is) { 5 | //TODO 6 | super(-1, -1); 7 | } 8 | 9 | 10 | public String toString() { 11 | //TODO 12 | return this.getClass().getSimpleName(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Readme.md: -------------------------------------------------------------------------------- 1 | #Change Notes: 2 | 3 | Beginning with the 9th Homework, a secondary set of unit tests shall be commited to master instead of to a separate side branch for visibility purposes. 4 | It would be advisable to use both tests, as they may differ slightly. 5 | 6 | ##Disclaimer 7 | As per any project, errors may occur. Do not *solely* rely on the unit tests for your checking. 8 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/IllegalCharExc.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | public class IllegalCharExc extends Exception { 4 | final private char used; 5 | 6 | public IllegalCharExc(char used) { 7 | //TODO 8 | this.used = 0; 9 | } 10 | 11 | public String toString() { 12 | //TODO 13 | return this.getClass().getSimpleName(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/gad17/blatt04/IOExceptionConsumer.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | import java.util.function.Consumer; 5 | 6 | /** 7 | * Dieses {@link FunctionalInterface} implementiert einen 8 | * {@link Consumer}, der eine {@link IOException} werfen kann. 9 | */ 10 | @FunctionalInterface 11 | public interface IOExceptionConsumer { 12 | void accept(T t) throws IOException; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/pgdp16/blatt12/readMe.md: -------------------------------------------------------------------------------- 1 | #Map 2 | - [x] Test to throw IllegalArgumentException 3 | - [x] Test with one Thread 4 | - [x] Test with multiple Threads 5 | - [ ] Test IntToString 6 | - [ ] Find n Fixing 7 | 8 | #Rock-Paper-Scissors 9 | - [x] Test Player getChoice() 10 | - [x] Test Player interrupt() 11 | - [x] Test RockPaperScissors determineWinner 12 | - [x] Test RockPaperScissors kill Threads 13 | - [ ] Find n Fixing 14 | -------------------------------------------------------------------------------- /src/gad17/blatt04/IResponse.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * Dieses Interface beschreibt alle gemeinsamen Methoden einer 8 | * Response. 9 | */ 10 | public interface IResponse extends Serializable { 11 | /** 12 | * Diese Methode entspricht der accept()-Methode des Visitor-Patterns. 13 | */ 14 | void accept(ResponseVisitor v) throws IOException; 15 | } 16 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/Password.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | public class Password { 4 | public Password(int nrUpperShould, int nrLowerShould, int nrSpecialShould, int nrNumbersShould, int lengthShould, char[] illegalChars) { 5 | //TODO 6 | } 7 | 8 | public void checkFormat(String pwd) { 9 | //TODO 10 | } 11 | 12 | public static void main(String args[]) { 13 | //TODO manual testing 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/gad17/blatt11/ConnectedComponents.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt11; 2 | 3 | public class ConnectedComponents { 4 | private BFS search; 5 | 6 | public ConnectedComponents() { 7 | search = new BFS(); 8 | } 9 | 10 | /** 11 | * Zählt alle Zusammenhangskomponenten im gegebenen Graphen g 12 | */ 13 | public int countConnectedComponents(Graph g) { 14 | // TODO 15 | throw new RuntimeException("Not yet implemented."); 16 | } 17 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt09/pgdp/Fruit.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.pgdp; 2 | 3 | public class Fruit { 4 | public boolean isApple() { 5 | //TODO 6 | return false; 7 | } 8 | 9 | public boolean isBanana() { 10 | //TODO 11 | return false; 12 | } 13 | 14 | public boolean isPineapple() { 15 | //TODO 16 | return false; 17 | } 18 | 19 | public int shelfLife() { 20 | //TODO 21 | return -2; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/pgdp/FruitBasket.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.pgdp; 2 | 3 | import java.util.LinkedList; 4 | 5 | public class FruitBasket { 6 | /*private*/ LinkedList fruits; //TODO make it private 7 | 8 | public void addFruit(Fruit f) { 9 | //TODO 10 | } 11 | 12 | public LinkedList getApples() { 13 | //TODO 14 | return null; 15 | } 16 | 17 | public LinkedList getEqualOrLongerShelfLife(int n) { 18 | //TODO 19 | return null; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Main.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | /** 4 | * Die Klasse Main enthaelt das Hauptprogramm. 5 | *

6 | * Im Hauptprogramm wird zuerst der Benutzer gefragt, 7 | * wer das Spiel beginnen soll. 8 | *

9 | * Dann wird das Spiel gestartet. 10 | */ 11 | public class Main { 12 | 13 | public static Game game;//TODO Please Rename; by CC 14 | public static IO io;//TODO Please Rename; by CC 15 | 16 | public static void main(String args[]) { 17 | //TODO 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/gad17/blatt05/DoubleHashable.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | public interface DoubleHashable { 4 | /** 5 | * Diese Methode implementiert h(key). 6 | * 7 | * @param key der Schlüssel, der gehasht werden soll 8 | * @return der generierte Hash 9 | */ 10 | long hash(K key); 11 | 12 | /** 13 | * Diese Methode implementiert h'(key). 14 | * 15 | * @param key der Schlüssel, der gehasht werden soll 16 | * @return der generierte Hash 17 | */ 18 | long hashTick(K key); 19 | } 20 | -------------------------------------------------------------------------------- /src/gad17/blatt05/Pair.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | public class Pair<_1, _2> { 4 | public final _1 _1; 5 | public final _2 _2; 6 | 7 | public Pair(_1 _1, _2 _2) { 8 | this._1 = _1; 9 | this._2 = _2; 10 | } 11 | 12 | public String toString() { 13 | return "(" + _1.toString() + "," + _2.toString() + ")"; 14 | } 15 | 16 | @Override 17 | public boolean equals(Object o) { 18 | return o instanceof Pair && ((Pair) o)._1.equals(_1) && ((Pair) o)._2.equals(_2); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/gad17/blatt03/EmptyIntervalTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt03; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class EmptyIntervalTest { 7 | @Test 8 | public void isEmpty_returnsFalse() throws Exception { 9 | EmptyInterval test = new EmptyInterval(); 10 | Assert.assertTrue(test.isEmpty()); 11 | } 12 | 13 | @Test 14 | public void getSize_returns0() throws Exception { 15 | EmptyInterval test = new EmptyInterval(); 16 | Assert.assertEquals(0, test.getSize(10)); 17 | } 18 | } -------------------------------------------------------------------------------- /src/gad17/blatt07/RadixSort.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt07; 2 | import java.lang.reflect.Array; 3 | import java.util.ArrayList; 4 | 5 | public class RadixSort { 6 | private int key(Integer element, int digit) throws IllegalArgumentException { 7 | // TODO 8 | return -1; 9 | } 10 | 11 | private void concatenate(ArrayList[] buckets, Integer[] elements) { 12 | // TODO 13 | } 14 | 15 | private void kSort(Integer[] elements, int digit) { 16 | // TODO 17 | } 18 | 19 | public void sort(Integer[] elements) { 20 | // TODO 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/pgdp16/blatt08/pgdp/Money.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt08.pgdp; //TODO move to package: pgdp 2 | 3 | public class Money { 4 | private int cent; 5 | 6 | public Money() { 7 | //TODO 8 | } 9 | 10 | public Money(int cent) { 11 | //TODO 12 | } 13 | 14 | public int getCent() { 15 | //TODO 16 | return -1; 17 | } 18 | 19 | public Money addMoney(Money m) { 20 | //TODO 21 | return null; 22 | } 23 | 24 | public String toString() { 25 | //TODO 26 | return null; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/gad17/blatt04/Mutable.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | /** 4 | * Diese Klasse implementiert einen simplen Wrapper für Objekte. 5 | * 6 | * @param der Typ der Objekte 7 | */ 8 | public class Mutable { 9 | private T wrapped; 10 | 11 | public T get() { 12 | return wrapped; 13 | } 14 | 15 | public void set(T wrapped) { 16 | this.wrapped = wrapped; 17 | } 18 | 19 | public Mutable(T wrapped) { 20 | this.wrapped = wrapped; 21 | } 22 | 23 | public Mutable() { 24 | this.wrapped = null; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Predator.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | /** 4 | * Klasse der Raubtiere. 5 | */ 6 | public class Predator extends Animal { 7 | 8 | int withoutFood;//TODO Please Rename; by CC 9 | 10 | /** 11 | * Dem Konstruktor wird das Geschlecht des Tiers uebergeben. 12 | */ 13 | public Predator(boolean female) { 14 | //TODO 15 | super(false); 16 | } 17 | 18 | public void eat() {//TODO Please Rename; by CC 19 | throw new RuntimeException("Method eat() shall be overridden! by children!"); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/gad17/blatt04/IRequest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * Dieses Interface beschreibt alle gemeinsamen Methoden eines 8 | * Requests. 9 | */ 10 | public interface IRequest extends Serializable { 11 | /** 12 | * Diese Methode entspricht der accept()-Methode des Visitor-Patterns. 13 | */ 14 | void accept(RequestVisitor v) throws IOException; 15 | 16 | /** 17 | * Diese Methode ermittelt des Schlüssel des Requests. 18 | * 19 | * @return der Schlüssel 20 | */ 21 | String getKey(); 22 | } 23 | -------------------------------------------------------------------------------- /src/gad17/blatt04/StoreResponse.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | * Die {@link StoreResponse} signalisiert Erfolg bei der 7 | * Durchführung des {@link StoreRequest}s. 8 | */ 9 | public class StoreResponse implements IResponse { 10 | private static final long serialVersionUID = 1L; 11 | 12 | public StoreResponse() { 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "Store() success"; 18 | } 19 | 20 | @Override 21 | public void accept(ResponseVisitor v) throws IOException { 22 | v.visit(this); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/pgdp16/blatt06/Toolbox.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt06; 2 | 3 | public class Toolbox { 4 | 5 | public static int evenSum(int n) { 6 | // TODO 7 | return -1; 8 | } 9 | 10 | public static int multiplication(int x, int y) { 11 | // TODO 12 | return -1; 13 | } 14 | 15 | public static void reverse(int[] m) { 16 | // TODO 17 | } 18 | 19 | public static int numberOfOddIntegers(int[] m) { 20 | // TODO 21 | return -1; 22 | } 23 | 24 | public static int[] filterOdd(int[] m) { 25 | // TODO 26 | return new int[0]; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Horse.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | public class Horse extends Vegetarian { 4 | 5 | /** 6 | * Dem Konstruktor wird das Geschlecht des Tiers uebergeben. 7 | */ 8 | public Horse(boolean female) { 9 | //TODO 10 | super(false); 11 | } 12 | 13 | 14 | @Override 15 | public String toString() { 16 | return this.female 17 | ? (Globals.darkSquare(this.square) ? Globals.ts_female_horse_dark : Globals.ts_female_horse_light) 18 | : (Globals.darkSquare(this.square) ? Globals.ts_male_horse_dark : Globals.ts_male_horse_light); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/gad17/blatt04/ReadRequest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | 5 | public class ReadRequest implements IRequest { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private String key; 9 | 10 | @Override 11 | public String getKey() { 12 | return key; 13 | } 14 | 15 | public ReadRequest(String key) { 16 | this.key = key; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "Read(" + key + ")"; 22 | } 23 | 24 | @Override 25 | public void accept(RequestVisitor v) throws IOException { 26 | v.visit(this); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/eti18/ha3/AcceptNFA.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class AcceptNFA { 6 | 7 | public static boolean accept(EpsilonNFA n, String w){ 8 | //TODO 9 | return false; 10 | } 11 | 12 | 13 | public static void main (String[] args){ 14 | Scanner scanner = new Scanner(System.in); 15 | EpsilonNFA e = Parser.parse(scanner); 16 | String word = scanner.nextLine(); 17 | while(!word.equals("DONE")) { 18 | System.out.println(word + ": " + accept(e, word)); 19 | word = scanner.nextLine(); 20 | } 21 | scanner.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Rabbit.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | public class Rabbit extends Vegetarian { 4 | 5 | /** 6 | * Dem Konstruktor wird das Geschlecht des Tiers uebergeben. 7 | */ 8 | public Rabbit(boolean female) { 9 | //TODO 10 | super(false); 11 | } 12 | 13 | 14 | @Override 15 | public String toString() { 16 | return this.female 17 | ? (Globals.darkSquare(this.square) ? Globals.ts_female_rabbit_dark : Globals.ts_female_rabbit_light) 18 | : (Globals.darkSquare(this.square) ? Globals.ts_male_rabbit_dark : Globals.ts_male_rabbit_light); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Elephant.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | public class Elephant extends Vegetarian { 4 | 5 | /** 6 | * Dem Konstruktor wird das Geschlecht des Tiers uebergeben. 7 | */ 8 | public Elephant(boolean female) { 9 | //TODO 10 | super(false); 11 | } 12 | 13 | 14 | @Override 15 | public String toString() { 16 | return this.female 17 | ? (Globals.darkSquare(this.square) ? Globals.ts_female_elephant_dark : Globals.ts_female_elephant_light) 18 | : (Globals.darkSquare(this.square) ? Globals.ts_male_elephant_dark : Globals.ts_male_elephant_light); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/eti18/ha5/IsEmpty.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class IsEmpty { 6 | 7 | public static boolean isEmpty(DFA n) { 8 | //TODO 9 | return false; 10 | } 11 | 12 | 13 | 14 | public static void main (String[] args){ 15 | Scanner scanner = new Scanner(System.in); 16 | EpsilonNFA e = Parser.parse(scanner); 17 | DFA d = null; 18 | if(e instanceof DFA){ 19 | d = (DFA) e; 20 | } 21 | else{ 22 | System.out.println("No DFA provided, aborting"); 23 | System.exit(3); 24 | } 25 | System.out.println(isEmpty(d)); 26 | scanner.close(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/eti18/ha5/IsFinite.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class IsFinite { 6 | 7 | public static boolean isFinite(DFA d){ 8 | //TODO 9 | return false; 10 | } 11 | 12 | 13 | public static void main (String[] args){ 14 | Scanner scanner = new Scanner(System.in); 15 | EpsilonNFA e = Parser.parse(scanner); 16 | DFA d = null; 17 | if(e instanceof DFA){ 18 | d = (DFA) e; 19 | } 20 | else{ 21 | System.out.println("No DFA provided, aborting"); 22 | System.exit(3); 23 | } 24 | System.out.println(isFinite(d)); 25 | scanner.close(); 26 | } 27 | 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/eti18/ha3/Powerset.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import java.util.*; 4 | 5 | public class Powerset { 6 | 7 | public static DFA powersetConstruction(NFA n) { 8 | //TODO 9 | return null; 10 | } 11 | 12 | public static void main(String[] args) { 13 | Scanner scanner = new Scanner(System.in); 14 | scanner.nextLine();//remove the number of lines in the beginning 15 | EpsilonNFA e = Parser.parse(scanner); 16 | scanner.close(); 17 | NFA n = null; 18 | if (e instanceof NFA) { 19 | n = (NFA) e; 20 | } else { 21 | System.out.println("No NFA provided, aborting"); 22 | System.exit(3); 23 | } 24 | System.out.println("Case #1:\n" + powersetConstruction(n)); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/gad17/blatt03/NonEmptyIntervalTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt03; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class NonEmptyIntervalTest { 7 | @Test 8 | public void isEmpty_returnsFalse() throws Exception { 9 | NonEmptyInterval test = new NonEmptyInterval(0, 0); 10 | Assert.assertFalse(test.isEmpty()); 11 | } 12 | 13 | @Test 14 | public void getSize_input10_givenFrom1To4_returns4() throws Exception { 15 | NonEmptyInterval test = new NonEmptyInterval(1, 4); 16 | Assert.assertEquals(4, test.getSize(10)); 17 | } 18 | 19 | @Test 20 | public void getSize_input10_givenFrom4To1_returns4() throws Exception { 21 | NonEmptyInterval test = new NonEmptyInterval(4, 1); 22 | Assert.assertEquals(8, test.getSize(10)); 23 | } 24 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt11/set/Set.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.set; 2 | 3 | import java.util.Iterator; 4 | 5 | public class Set implements Iterable { 6 | final private List list; 7 | 8 | public Set() { 9 | list = null; 10 | } 11 | 12 | public Set add(T e) { 13 | return null; 14 | } 15 | 16 | public Set remove(Object o) { 17 | return null; 18 | } 19 | 20 | public boolean contains(Object o) { 21 | return false; 22 | } 23 | 24 | public int size() { 25 | return -1; 26 | } 27 | 28 | public boolean equals(Object o) { 29 | return false; 30 | } 31 | 32 | public String toString() { 33 | return super.toString(); 34 | } 35 | 36 | public Iterator iterator() { 37 | return null; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/gad17/blatt04/StoreRequest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | 5 | public class StoreRequest implements IRequest { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private String key; 9 | 10 | @Override 11 | public String getKey() { 12 | return key; 13 | } 14 | 15 | private int value; 16 | 17 | public int getValue() { 18 | return value; 19 | } 20 | 21 | public StoreRequest(String key, int value) { 22 | this.key = key; 23 | this.value = value; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "Store(" + key + " <- " + value + ")"; 29 | } 30 | 31 | @Override 32 | public void accept(RequestVisitor v) throws IOException { 33 | v.visit(this); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/eti18/ha3/AcceptDFA.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class AcceptDFA { 6 | 7 | public static boolean accept(DFA d, String w) { 8 | //TODO 9 | return false; 10 | } 11 | 12 | public static void main(String[] args) { 13 | Scanner scanner = new Scanner(System.in); 14 | EpsilonNFA e = Parser.parse(scanner); 15 | DFA d = null; 16 | if (e instanceof DFA) { 17 | d = (DFA) e; 18 | } else { 19 | System.out.println("No DFA provided, aborting"); 20 | System.exit(3); 21 | } 22 | String word = scanner.nextLine(); 23 | while (!word.equals("DONE")) { 24 | System.out.println(word + ": " + accept(d, word)); 25 | word = scanner.nextLine(); 26 | } 27 | scanner.close(); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Snake.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | public class Snake extends Predator { 4 | 5 | // Eine Schlange kann 9 Tage bzw. Spielrunden ohne Essen auskommen. 6 | // Die Deklaration darf entfernt (und der Wert z. B. direkt im Code 7 | // verwendet) werden. 8 | private static int withoutFood = 9; 9 | 10 | 11 | /** 12 | * Dem Konstruktor wird das Geschlecht des Tiers uebergeben. 13 | */ 14 | public Snake(boolean female) { 15 | //TODO 16 | super(false); 17 | } 18 | 19 | 20 | @Override 21 | public String toString() { 22 | return this.female 23 | ? (Globals.darkSquare(this.square) ? Globals.ts_female_snake_dark : Globals.ts_female_snake_light) 24 | : (Globals.darkSquare(this.square) ? Globals.ts_male_snake_dark : Globals.ts_male_snake_light); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Leopard.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | public class Leopard extends Predator { 4 | 5 | // Ein Leopard kann nur 5 Tage bzw. Spielrunden ohne Essen auskommen. 6 | // Die Deklaration darf entfernt (und der Wert z. B. direkt im Code 7 | // verwendet) werden. 8 | private static int withoutFood = 5; 9 | 10 | 11 | /** 12 | * Dem Konstruktor wird das Geschlecht des Tiers uebergeben. 13 | */ 14 | public Leopard(boolean female) { 15 | //TODO 16 | super(false); 17 | } 18 | 19 | 20 | @Override 21 | public String toString() { 22 | return this.female 23 | ? (Globals.darkSquare(this.square) ? Globals.ts_female_leopard_dark : Globals.ts_female_leopard_light) 24 | : (Globals.darkSquare(this.square) ? Globals.ts_male_leopard_dark : Globals.ts_male_leopard_light); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Penguin.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | public class Penguin extends Predator { 4 | 5 | // Ein Pinguin kann 12 Tage bzw. Spielrunden ohne Essen auskommen. 6 | // Die Deklaration darf entfernt (und der Wert z. B. direkt im Code 7 | // verwendet) werden. 8 | private static int withoutFood = 12; 9 | 10 | 11 | /** 12 | * Dem Konstruktor wird das Geschlecht des Tiers uebergeben. 13 | */ 14 | public Penguin(boolean female) { 15 | //TODO 16 | super(false); 17 | } 18 | 19 | 20 | @Override 21 | public String toString() { 22 | return this.female 23 | ? (Globals.darkSquare(this.square) ? Globals.ts_female_penguin_dark : Globals.ts_female_penguin_light) 24 | : (Globals.darkSquare(this.square) ? Globals.ts_male_penguin_dark : Globals.ts_male_penguin_light); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/IO.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | /** 3 | * Die Klasse IO enthaelt Hilfsprogramme zum Einlesen. 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class IO { 9 | 10 | public /*static*/ String readString(String msg) { 11 | System.out.print(msg); 12 | return (new Scanner(System.in)).nextLine(); 13 | } 14 | 15 | public /*static*/ int readInt(String msg, int low, int high) { 16 | int result; 17 | do { 18 | System.out.print(msg); 19 | result = (new Scanner(System.in)).nextInt(); 20 | } while (result < low || result > high); 21 | return result; 22 | } 23 | 24 | public /*static*/ int readInt() { 25 | return (new Scanner(System.in)).nextInt(); 26 | } 27 | 28 | public /*static*/ int readInt(String msg) { 29 | System.out.print(msg); 30 | return readInt(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pgdp16/blatt08/pgdp/Bank.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt08.pgdp; //TODO move to package: pgdp 2 | 3 | public class Bank { 4 | /*private*/ BankAccountList accounts; //TODO make private 5 | 6 | public int newAccount(String firstname, String lastname) { 7 | //TODO 8 | return -1; 9 | } 10 | 11 | public void removeAccount(int accountnumber) { 12 | //TODO 13 | } 14 | 15 | public Money getBalance(int accountnumber) { 16 | //TODO 17 | return null; 18 | } 19 | 20 | public boolean depositOrWithdraw(int accountnumber, Money m) { 21 | //TODO 22 | return false; 23 | } 24 | 25 | public boolean transfer(int from, int to, Money m) { 26 | //TODO 27 | return false; 28 | } 29 | 30 | /*private*/ class BankAccountList {//TODO make private 31 | public BankAccount info; 32 | public BankAccountList next; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/pgdp16/blatt07/Tailrec1.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt07; 2 | 3 | public class Tailrec1 { 4 | 5 | // x^y 6 | public static int pow(int x, int y) { 7 | return java.math.BigInteger.valueOf(x).pow(y).intValueExact(); 8 | } 9 | 10 | // function f recursive 11 | public static int frec(int x) { 12 | return grec(x, 0); 13 | } 14 | 15 | // helper function g recursive 16 | public static int grec(int x, int pos) { 17 | if (x / 10 == 0) { 18 | return pow(x, pos); 19 | } 20 | return pow(x % 10, pos) + grec(x / 10, ++pos); 21 | } 22 | 23 | // function f tail recursive 24 | public static int ftailrec(int x) { 25 | // TODO 26 | return -1; 27 | } 28 | 29 | public static void main(String[] args) { 30 | int n = 12345; 31 | 32 | System.out.println("f recursive: " + frec(n)); 33 | System.out.println("f tailrec: " + ftailrec(n)); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/eti18/ha3/NFA.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import java.util.Set; 4 | 5 | 6 | public class NFA extends EpsilonNFA { 7 | public NFA(Set states, Set transitions, Set alphabet, State startState, Set finalStates) { 8 | super(states, transitions, alphabet, startState, finalStates); 9 | checkValidNFA(); 10 | } 11 | 12 | //No epsilon transitions, rest is done by the checkValidEpsilonNFA method 13 | private void checkValidNFA() throws IllegalArgumentException{ 14 | for (State s : states){ 15 | for (Transition t : getTransitionsFromState(s)) { 16 | if (t.getLabel() == Transition.EPSILON) { 17 | throw new IllegalArgumentException("State " + s + " has an epsilon transition"); 18 | } 19 | } 20 | } 21 | } 22 | 23 | @Override 24 | public String toString(){ 25 | return "NFA\n" + toStringHelper(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/eti18/ha5/NFA.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import java.util.Set; 4 | 5 | 6 | public class NFA extends EpsilonNFA { 7 | public NFA(Set states, Set transitions, Set alphabet, State startState, Set finalStates) { 8 | super(states, transitions, alphabet, startState, finalStates); 9 | checkValidNFA(); 10 | } 11 | 12 | //No epsilon transitions, rest is done by the checkValidEpsilonNFA method 13 | private void checkValidNFA() throws IllegalArgumentException{ 14 | for (State s : states){ 15 | for (Transition t : getTransitionsFromState(s)) { 16 | if (t.getLabel() == Transition.EPSILON) { 17 | throw new IllegalArgumentException("State " + s + " has an epsilon transition"); 18 | } 19 | } 20 | } 21 | } 22 | 23 | @Override 24 | public String toString(){ 25 | return "NFA\n" + toStringHelper(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Move.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | /** 4 | * Die Klasse Move repraesentiert einen einzelnen Zug. 5 | *

6 | * Es gibt zwei Konstruktoren. Einer bekommt 7 | * Ausgangsfeld und Zielfeld uebergeben, der andere 8 | * bekommt nur den eingegebenen Zug in der Form 9 | * als String uebergeben, 10 | * also z. B. "a7c5" fuer den Zug von "a7" nach "c5". 11 | */ 12 | public class Move { 13 | 14 | public Move(String from, String to) { 15 | //TODO 16 | } 17 | 18 | public Move(String move) { 19 | //TODO 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | //TODO 25 | // Rueckgabe exakt in der Form als String, 26 | // also z. B. "b2b3" fuer den Zug eines Tiers von "b2" nach "b3". 27 | return null; 28 | } 29 | 30 | public boolean equals(Object other) { 31 | //TODO 32 | return false; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/pgdp16/blatt08/pgdp/BankAccount.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt08.pgdp; //TODO move to package: pgdp 2 | 3 | public class BankAccount { 4 | private int bankaccount; 5 | private String firstname; 6 | private String surname; 7 | private Money balance; 8 | 9 | public BankAccount(int accountnumber, String fname, String sname) { 10 | this.bankaccount = accountnumber; 11 | this.firstname = fname; 12 | this.surname = sname; 13 | //TODO 14 | } 15 | 16 | public int getAccountnumber() { 17 | return bankaccount; 18 | } 19 | 20 | public String getFirstname() { 21 | return firstname; 22 | } 23 | 24 | public String getSurname() { 25 | return surname; 26 | } 27 | 28 | public Money getBalance() { 29 | return balance; 30 | } 31 | 32 | public void addMoney(Money m) { 33 | //TODO 34 | } 35 | 36 | public String toString() { 37 | //TODO 38 | return null; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/gad17/blatt04/RequestVisitor.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | * Diese Klasse implementiert den Visitor des Visitor-Patterns 7 | * für Requests. 8 | */ 9 | public class RequestVisitor { 10 | /** 11 | * Der Callback, der für {@link ReadRequest}-Objekte aufgerufen wird. 12 | */ 13 | private IOExceptionConsumer readCallback; 14 | 15 | /** 16 | * Der Callback, der für {@link StoreRequest}-Objekte aufgerufen wird. 17 | */ 18 | private IOExceptionConsumer storeCallback; 19 | 20 | public void __(IOExceptionConsumer readCallback, IOExceptionConsumer storeCallback) { 21 | this.readCallback = readCallback; 22 | this.storeCallback = storeCallback; 23 | } 24 | 25 | public void visit(ReadRequest rr) throws IOException { 26 | readCallback.accept(rr); 27 | } 28 | 29 | public void visit(StoreRequest sr) throws IOException { 30 | storeCallback.accept(sr); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/eti18/ha5/Equivalent.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Equivalent { 6 | 7 | public static boolean equivalent(DFA d1, DFA d2) { 8 | //TODO 9 | return false; 10 | } 11 | 12 | public static void main(String[] args){ 13 | 14 | Scanner scanner = new Scanner(System.in); 15 | EpsilonNFA e = Parser.parse(scanner); 16 | DFA d1 = null; 17 | if(e instanceof DFA){ 18 | d1 = (DFA) e; 19 | } 20 | else{ 21 | System.out.println("First automaton is no DFA, aborting"); 22 | System.exit(3); 23 | } 24 | 25 | e = Parser.parse(scanner); 26 | DFA d2 = null; 27 | if(e instanceof DFA){ 28 | d2 = (DFA) e; 29 | } 30 | else{ 31 | System.out.println("Second automaton is no DFA, aborting"); 32 | System.exit(3); 33 | } 34 | 35 | System.out.println(equivalent(d1,d2)); 36 | 37 | scanner.close(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/gad17/blatt04/ResponseVisitor.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | * Diese Klasse implementiert den Visitor des Visitor-Patterns 7 | * für Responses. 8 | */ 9 | public class ResponseVisitor { 10 | /** 11 | * Der Callback, der für {@link ReadResponse}-Objekte aufgerufen wird. 12 | */ 13 | private IOExceptionConsumer readCallback; 14 | 15 | /** 16 | * Der Callback, der für {@link StoreResponse}-Objekte aufgerufen wird. 17 | */ 18 | private IOExceptionConsumer storeCallback; 19 | 20 | public void __(IOExceptionConsumer readCallback, IOExceptionConsumer storeCallback) { 21 | this.readCallback = readCallback; 22 | this.storeCallback = storeCallback; 23 | } 24 | 25 | public void visit(ReadResponse rr) throws IOException { 26 | readCallback.accept(rr); 27 | } 28 | 29 | public void visit(StoreResponse sr) throws IOException { 30 | storeCallback.accept(sr); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/gad17/blatt08/BinomialHeap.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt08; 2 | import java.util.ArrayList; 3 | 4 | public class BinomialHeap { 5 | public ArrayList heap;// TODO Rename after testing 6 | 7 | /** 8 | * Dieser Konstruktor baut einen leeren Haufen. 9 | */ 10 | public BinomialHeap() { 11 | // TODO 12 | } 13 | 14 | /** 15 | * Diese Methode ermittelt das minimale Element im binomialen Haufen. 16 | * Wenn es dieses nicht gibt (bei leerem Haufen), soll eine RuntimeException geworfen werden. 17 | * 18 | * @return das minimale Element 19 | */ 20 | public int min() { 21 | // TODO 22 | return -1; 23 | } 24 | 25 | /** 26 | * Diese Methode fügt einen Schlüssel in den Haufen ein. 27 | * 28 | * @param key der einzufügende Schlüssel 29 | */ 30 | public void insert(int key) { 31 | // TODO 32 | } 33 | 34 | /** 35 | * Diese Methode entfernt das minimale Element aus dem binomialen 36 | * Haufen und gibt es zurück. 37 | * 38 | * @return das minimale Element 39 | */ 40 | public int deleteMin() { 41 | // TODO 42 | return -1; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/gad17/blatt04/ReadResponse.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | 5 | public class ReadResponse implements IResponse { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private SerializableOptional value; 9 | 10 | /** 11 | * Diese Methode gibt den gelesenen Wert zurück. Gibt 12 | * es zum gesuchten Schlüssel keinen Wert, so wird 13 | * {@link SerializableOptional}.empty() zurückgeliefert. 14 | * 15 | * @return der gelesene Wert 16 | */ 17 | public SerializableOptional getValue() { 18 | return value; 19 | } 20 | 21 | public ReadResponse(int value) { 22 | this.value = SerializableOptional.of(value); 23 | } 24 | 25 | public ReadResponse() { 26 | this.value = SerializableOptional.empty(); 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "Read() = " + value; 32 | } 33 | 34 | @Override 35 | public void accept(ResponseVisitor v) throws IOException { 36 | v.visit(this); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/eti18/ha5/Intersection.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Intersection { 6 | 7 | public static DFA intersection(DFA d1, DFA d2) { 8 | //TODO 9 | return null; 10 | } 11 | 12 | public static void main(String[] args){ 13 | 14 | Scanner scanner = new Scanner(System.in); 15 | scanner.nextLine();//remove the number of lines in the beginning 16 | EpsilonNFA e = Parser.parse(scanner); 17 | DFA d1 = null; 18 | if(e instanceof DFA){ 19 | d1 = (DFA) e; 20 | } 21 | else{ 22 | System.out.println("First automaton is no DFA, aborting"); 23 | System.exit(3); 24 | } 25 | 26 | e = Parser.parse(scanner); 27 | DFA d2 = null; 28 | if(e instanceof DFA){ 29 | d2 = (DFA) e; 30 | } 31 | else{ 32 | System.out.println("Second automaton is no DFA, aborting"); 33 | System.exit(3); 34 | } 35 | 36 | System.out.println("Case #1:\n" + intersection(d1,d2)); 37 | scanner.close(); 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/gad17/blatt03/DynamicStack.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt03; 2 | 3 | /** 4 | * Die Klasse DynamicStack soll einen Stapel auf 5 | * Basis der Klasse {@link DynamicArray} implementieren. 6 | */ 7 | public class DynamicStack { 8 | private DynamicArray dynArr; 9 | 10 | /** 11 | * Dieses Feld speichert die Anzahl der Elemente auf dem Stapel. 12 | */ 13 | private int length; 14 | 15 | public int getLength() { 16 | return length; 17 | } 18 | 19 | public DynamicStack(int growthFactor, int maxOverhead) { 20 | dynArr = new DynamicArray(growthFactor, maxOverhead); 21 | length = 0; 22 | } 23 | 24 | /** 25 | * Diese Methode legt ein Element auf den Stapel. 26 | * 27 | * @param value das Element, das auf den Stapel gelegt werden soll 28 | */ 29 | public void pushBack(int value) { 30 | /* 31 | * Todo 32 | */ 33 | } 34 | 35 | /** 36 | * Diese Methode nimmt ein Element vom Stapel. 37 | * 38 | * @return das entfernte Element 39 | */ 40 | public int popBack() { 41 | /* 42 | * Todo 43 | */ 44 | return -2; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/gad17/blatt11/BFS.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt11; 2 | 3 | public class BFS { 4 | public BFS() { 5 | // TODO 6 | // Sie dürfen die Signatur dieser Funktion verändern 7 | // (z.B. im Parameter im Konstruktor zu übergeben), 8 | // müssen das dann aber in Graphilia.java entsprechend übernehmen. 9 | } 10 | 11 | /** 12 | * Führt eine Breitensuche vom Startknoten "start" aus, um das SSSP-Problem zu lösen. 13 | */ 14 | public void sssp(Graph.Node start) { 15 | // TODO 16 | } 17 | 18 | /** 19 | * Nachdem SSSP ausgeführt wurde, kann mit dieser Funktion die Tiefe des Knotens n 20 | * vom Startknoten überprüft werden. 21 | */ 22 | public Integer getDepth(Graph.Node n) { 23 | // TODO 24 | throw new RuntimeException("Not yet implemented."); 25 | } 26 | 27 | /** 28 | * Nachdem SSSP ausgeführt wurde, kann mit dieser Funktion der Vaterknoten 29 | * des Knotens n in Richtung Startknoten abgefragt werden. 30 | */ 31 | public Graph.Node getParent(Graph.Node n) { 32 | // TODO 33 | throw new RuntimeException("Not yet implemented."); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/grnvs18/assignment2/GRNVS_RAW_Mock.java: -------------------------------------------------------------------------------- 1 | package grnvs18.assignment2; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class GRNVS_RAW_Mock { 6 | private byte[] mac; 7 | private ArrayList frames; 8 | 9 | public GRNVS_RAW_Mock(byte[] mac, byte[]... frames) { 10 | this.frames = new ArrayList<>(); 11 | for (byte[] frame : frames) 12 | this.frames.add(frame); 13 | this.mac = mac; 14 | } 15 | 16 | public GRNVS_RAW_Mock() { 17 | this.mac = new byte[6]; 18 | this.frames = new ArrayList<>(); 19 | } 20 | 21 | public byte[] getMac() { 22 | return mac; 23 | } 24 | 25 | /** 26 | * @param recbuffer writes into 27 | * @param timeout 28 | * @return length of recbuffer (1514); nothing to receive: returns 0 29 | */ 30 | public int read(byte[] recbuffer, Timeout timeout) { 31 | try { 32 | byte[] f = frames.remove(0); 33 | for (int i = 0; i < f.length; i++) 34 | recbuffer[i] = f[i]; 35 | 36 | return f.length; 37 | } catch (Exception e) { 38 | return 1234; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/gad17/blatt04/HashString.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.math.BigInteger; 4 | import java.util.ArrayList; 5 | 6 | /** 7 | * Die Klasse {@link HashString} kann dazu verwendet werden, 8 | * Strings zu hashen. 9 | */ 10 | public class HashString { 11 | /* 12 | * Todo 13 | */ 14 | public int size; //TODO rename after Testing! 15 | public ArrayList vectors; //TODO rename after Testing! 16 | 17 | /** 18 | * Dieser Konstruktor initialisiert ein {@link HashString} 19 | * Objekt für einen gegebenen Maximalwert (size - 1) der gehashten 20 | * Werte. 21 | * 22 | * @param size die Größe der Hashtabelle 23 | */ 24 | public HashString(int size) { 25 | /* 26 | * Todo 27 | */ 28 | } 29 | 30 | private boolean isInvalidSize(int size) { 31 | return size <= 1 || !new BigInteger("" + size).isProbablePrime(666); 32 | } 33 | 34 | /** 35 | * Diese Methode berechnet den Hashwert für einen String. 36 | * 37 | * @param key der Schlüssel, der gehasht werden sollen 38 | * @return der Hashwert des Schlüssels 39 | */ 40 | public int hash(String key) { 41 | /* 42 | * Todo 43 | */ 44 | return -1; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/gad17/blatt11/Graph.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt11; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | 6 | public class Graph { 7 | public ArrayList nodes; 8 | 9 | public class Node { 10 | // TODO rename after Testing! 11 | public int index; 12 | public Collection connected; 13 | } 14 | 15 | public Graph() { 16 | // TODO 17 | } 18 | 19 | /** 20 | * Erstellt einen neuen Knoten, und gibt den Index dieses Knotens zurück. 21 | * Der erste erstellte Knoten sollte Index 0 haben. 22 | * Knoten, die direkt nacheinander erstellt werden, sollten aufsteigende Indices haben. 23 | */ 24 | public Integer addNode() { 25 | // TODO 26 | throw new RuntimeException("Not yet implemented."); 27 | } 28 | 29 | /** 30 | * Liefert den Knoten zum angegebenen Index zurück 31 | */ 32 | public Node getNode(Integer id) { 33 | // TODO 34 | throw new RuntimeException("Not yet implemented."); 35 | } 36 | 37 | /** 38 | * Fügt zwischen den beiden angegebenen Knoten eine (ungerichtete) Kante hinzu. 39 | */ 40 | public void addEdge(Node a, Node b) { 41 | // TODO 42 | throw new RuntimeException("Not yet implemented."); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/gad17/blatt05/DoubleHashInt.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | /** 4 | * Die Klasse {@link DoubleHashInt} kann dazu verwendet werden, 5 | * Integer zu hashen. 6 | */ 7 | public class DoubleHashInt implements DoubleHashable { 8 | /* 9 | * Todo 10 | */ 11 | private int size, a, b; //TODO Rename after Testing 12 | 13 | /** 14 | * Dieser Konstruktor initialisiert ein {@link DoubleHashInt} 15 | * Objekt für einen gegebenen Maximalwert (size - 1) der gehashten 16 | * Werte. 17 | * 18 | * @param size die Größe der Hashtabelle 19 | */ 20 | public DoubleHashInt(int size) { 21 | /* 22 | * Todo 23 | */ 24 | } 25 | 26 | /** 27 | * Diese Methode berechnet h(key) für einen Integer. 28 | * 29 | * @param key der Schlüssel, der gehasht werden soll 30 | * @return der Hashwert des Schlüssels 31 | */ 32 | @Override 33 | public long hash(Integer key) { 34 | /* 35 | * Todo 36 | */ 37 | return -2; 38 | } 39 | 40 | /** 41 | * Diese Methode berechnet h'(key) für einen Integer. 42 | * 43 | * @param key der Schlüssel, der gehasht werden soll 44 | * @return der Hashwert des Schlüssels 45 | */ 46 | @Override 47 | public long hashTick(Integer key) { 48 | /* 49 | * Todo 50 | */ 51 | return -2; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/gad17/blatt08/BinomialTreeNode.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt08; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class BinomialTreeNode { 6 | 7 | public int key; // TODO Rename after testing 8 | public ArrayList tree; // TODO Rename after testing 9 | 10 | public BinomialTreeNode(int key) { 11 | // TODO 12 | } 13 | 14 | /** 15 | * Ermittelt das minimale Element im Teilbaum. 16 | * 17 | * @return das minimale Element 18 | */ 19 | public int min() { 20 | // TODO 21 | return -1; 22 | } 23 | 24 | /** 25 | * Gibt den Rang des Teilbaumes zurück. 26 | * 27 | * @return der Rang des Teilbaumes 28 | */ 29 | public int rank() { 30 | // TODO 31 | return -1; 32 | } 33 | 34 | /** 35 | * Entfernt den minimalen Knoten (= Wurzel) und gibt eine Menge von 36 | * Teilbäumen zurück, in die der aktuelle Baum zerfällt, wenn man 37 | * den Knoten entfernt. 38 | * 39 | * @return die Menge von Teilbäumen 40 | */ 41 | public BinomialTreeNode[] deleteMin() { 42 | // TODO 43 | return null; 44 | } 45 | 46 | /** 47 | * Diese Methode vereint zwei Bäume des gleichen Ranges. 48 | * 49 | * @param a der erste Baum 50 | * @param b der zweite Baum 51 | * @return denjenigen der beiden Bäume, an den der andere angehängt wurde 52 | */ 53 | public static BinomialTreeNode merge(BinomialTreeNode a, BinomialTreeNode b) { 54 | // TODO 55 | return null; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/gad17/blatt03/StackyQueue.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt03; 2 | 3 | /** 4 | * Die Klasse StackyQueue soll eine Warteschlange auf 5 | * Basis der Klasse {@link DynamicStack} implementieren. Es 6 | * soll ausschließlich die Klasse {@link DynamicStack} zur 7 | * Datenspeicherung verwendet werden. 8 | */ 9 | public class StackyQueue { 10 | /* 11 | * Todo 12 | */ 13 | 14 | /** 15 | * Diese Methode ermittelt die Länge der Warteschlange. 16 | * 17 | * @return die Länge der Warteschlange 18 | */ 19 | public int getLength() { 20 | /* 21 | * Todo 22 | */ 23 | return -2; 24 | } 25 | 26 | /** 27 | * Dieser Kontruktor initialisiert eine neue Schlange. 28 | * 29 | * @param growthFactor 30 | * @param maxOverhead 31 | */ 32 | public StackyQueue(int growthFactor, int maxOverhead) { 33 | /* 34 | * Todo 35 | */ 36 | } 37 | 38 | /** 39 | * Diese Methode reiht ein Element in die Schlange ein. 40 | * 41 | * @param value der einzufügende Wert 42 | */ 43 | public void enqueue(int value) { 44 | /* 45 | * Todo 46 | */ 47 | } 48 | 49 | /** 50 | * Diese Methode entfernt ein Element aus der Warteschlange. 51 | * 52 | * @return das entfernte Element 53 | */ 54 | public int dequeue() { 55 | /* 56 | * Todo 57 | */ 58 | return -2; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/gad17/blatt03/StackyQueueTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt03; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | public class StackyQueueTest { 8 | StackyQueue test; 9 | 10 | @Before 11 | public void setUp() throws Exception { 12 | test = new StackyQueue(2, 4); 13 | System.out.println(test.getClass().getFields()); 14 | } 15 | 16 | private void assertLength(int expected) { 17 | Assert.assertEquals(expected, test.getLength()); 18 | } 19 | 20 | @Test 21 | public void getLength_givenInitialState_returns0() throws Exception { 22 | assertLength(0); 23 | } 24 | 25 | @Test(expected = Exception.class) 26 | public void dequeue_givenInitialState_throwsException() throws Exception { 27 | test.dequeue(); 28 | } 29 | 30 | @Test 31 | public void enqueue_givenInitialState_thenLengthIs1() throws Exception { 32 | test.enqueue(10); 33 | assertLength(1); 34 | } 35 | 36 | @Test 37 | public void dequeue_given12345_thenDequeue12345() throws Exception { 38 | enqueue(1, 2, 3, 4, 5); 39 | 40 | assertDequeue(1, 2, 3, 4, 5); 41 | } 42 | 43 | private void enqueue(int... values) { 44 | for (int value : values) test.enqueue(value); 45 | } 46 | 47 | private void assertDequeue(int... ints) { 48 | for (int i : ints) Assert.assertEquals(i, test.dequeue()); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /src/gad17/blatt05/DoubleHashString.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * Die Klasse {@link DoubleHashString} kann dazu verwendet werden, 7 | * Strings zu hashen. 8 | */ 9 | public class DoubleHashString implements DoubleHashable { 10 | /* 11 | * Todo 12 | */ 13 | private int size; //TODO Rename after Testing 14 | private ArrayList a, b; //TODO Rename after Testing 15 | 16 | /** 17 | * Dieser Konstruktor initialisiert ein {@link DoubleHashString} 18 | * Objekt für einen gegebenen Maximalwert (size - 1) der gehashten 19 | * Werte. 20 | * 21 | * @param size die Größe der Hashtabelle 22 | */ 23 | public DoubleHashString(int size) { 24 | /* 25 | * Todo 26 | */ 27 | } 28 | 29 | /** 30 | * Diese Methode berechnet h(key) für einen String. 31 | * 32 | * @param key der Schlüssel, der gehasht werden soll 33 | * @return der Hashwert des Schlüssels 34 | */ 35 | public long hash(String key) { 36 | /* 37 | * Todo 38 | */ 39 | return -2; 40 | } 41 | 42 | /** 43 | * Diese Methode berechnet h'(key) für einen String. 44 | * 45 | * @param key der Schlüssel, der gehasht werden soll 46 | * @return der Hashwert des Schlüssels 47 | */ 48 | public long hashTick(String key) { 49 | /* 50 | * Todo 51 | */ 52 | return -2; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Game.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | /** 4 | * Die Klasse Game fuehrt die Benutzerinteraktion durch. 5 | */ 6 | 7 | public class Game { 8 | public IO io; //TODO Please Rename; by CC 9 | /*private*/ Position pos; 10 | 11 | 12 | /** 13 | * Startet ein neues Spiel. 14 | * Der Benutzer wird ueber das Spielgeschehen informiert. 15 | *

16 | * Dazu gehoert auch die Information, wie lange die 17 | * einzelnen Raubtiere noch ohne Essen auskommen koennen. 18 | * Diese Information soll auf Anfrage oder immer angezeigt werden. 19 | *

20 | * Es soll ausserdem eine Moeglichkeit geben, sich alle Zuege 21 | * anzeigen zu lassen, die in der Spielsituation moeglich sind. 22 | *

23 | * Bei fehlerhaften Eingaben wird die Eingabe natuerlich wiederholt. 24 | *

25 | * Der Parameter spezifiziert, wer das Spiel beginnen darf. 26 | */ 27 | public void startGame(boolean ladiesFirst) { 28 | pos = new Position(); 29 | pos.reset(ladiesFirst ? 'W' : 'M'); 30 | //TODO 31 | loop(); 32 | } 33 | 34 | public void loop() { //TODO Please Rename; by CC 35 | 36 | } 37 | 38 | public void printBoard() {//TODO Please Rename; by CC 39 | 40 | } 41 | 42 | public void printPredatorWithoutFood() {//TODO Please Rename; by CC 43 | } 44 | 45 | public void printMoves() {//TODO Please Rename; by CC 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/eti18/ha3/Transition.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import java.util.Objects; 4 | 5 | public class Transition { 6 | 7 | private final State start; 8 | private final State end; 9 | private final char label; 10 | public static final char EPSILON = Character.MIN_VALUE; 11 | 12 | public Transition(State start, State end, char label) { 13 | this.start = start; 14 | this.end = end; 15 | this.label = label; 16 | } 17 | 18 | public State getStart() { 19 | return start; 20 | } 21 | 22 | public State getEnd() { 23 | return end; 24 | } 25 | 26 | public char getLabel() { 27 | return label; 28 | } 29 | 30 | @Override 31 | public String toString(){ 32 | String letter = label==EPSILON ? "\u03B5" : label+""; 33 | return start + ";" + letter + ";" + end; 34 | } 35 | 36 | public String toStringWithoutEpsilon(){ 37 | return start + ";" + label + ";" + end; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Transition that = (Transition) o; 45 | return Objects.equals(start, that.start) && 46 | Objects.equals(end, that.end) && 47 | Objects.equals(label, that.label); 48 | } 49 | 50 | @Override 51 | public int hashCode() { 52 | 53 | return Objects.hash(start, end, label); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/eti18/ha5/Transition.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import java.util.Objects; 4 | 5 | public class Transition { 6 | 7 | private final State start; 8 | private final State end; 9 | private final char label; 10 | public static final char EPSILON = Character.MIN_VALUE; 11 | 12 | public Transition(State start, State end, char label) { 13 | this.start = start; 14 | this.end = end; 15 | this.label = label; 16 | } 17 | 18 | public State getStart() { 19 | return start; 20 | } 21 | 22 | public State getEnd() { 23 | return end; 24 | } 25 | 26 | public char getLabel() { 27 | return label; 28 | } 29 | 30 | @Override 31 | public String toString(){ 32 | String letter = label==EPSILON ? "\u03B5" : label+""; 33 | return start + ";" + letter + ";" + end; 34 | } 35 | 36 | public String toStringWithoutEpsilon(){ 37 | return start + ";" + label + ";" + end; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Transition that = (Transition) o; 45 | return Objects.equals(start, that.start) && 46 | Objects.equals(end, that.end) && 47 | Objects.equals(label, that.label); 48 | } 49 | 50 | @Override 51 | public int hashCode() { 52 | 53 | return Objects.hash(start, end, label); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/pgdp16/blatt12/rock/RockPaperScissorsTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt12.rock; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Ignore; 5 | import org.junit.Test; 6 | 7 | import static pgdp16.blatt12.rock.RockPaperScissors.determineWinner; 8 | 9 | 10 | public class RockPaperScissorsTest { 11 | @Test 12 | public void determineWinner_givenSame_return0() throws Exception { 13 | Assert.assertEquals(0, determineWinner(0, 0)); 14 | Assert.assertEquals(0, determineWinner(1, 1)); 15 | Assert.assertEquals(0, determineWinner(2, 2)); 16 | } 17 | 18 | @Test 19 | public void determineWinner_given1Wins_return1() throws Exception { 20 | Assert.assertEquals(1, determineWinner(1, 0)); 21 | Assert.assertEquals(1, determineWinner(2, 1)); 22 | Assert.assertEquals(1, determineWinner(0, 2)); 23 | } 24 | 25 | @Test 26 | public void determineWinner_given2Wins_return2() throws Exception { 27 | Assert.assertEquals(2, determineWinner(0, 1)); 28 | Assert.assertEquals(2, determineWinner(1, 2)); 29 | Assert.assertEquals(2, determineWinner(2, 0)); 30 | } 31 | 32 | @Test(timeout = 1500) 33 | public void run_1Game() throws Exception { 34 | Thread game = new Thread(new RockPaperScissors()); 35 | game.start(); 36 | game.join(); 37 | Assert.assertFalse(game.isAlive()); 38 | } 39 | 40 | @Test(timeout = 15000) 41 | @Ignore 42 | public void run_10Games() throws Exception { 43 | for (int i = 0; i <= 10; i++) run_1Game(); 44 | } 45 | 46 | 47 | } -------------------------------------------------------------------------------- /src/grnvs18/assignment2/Arguments.java: -------------------------------------------------------------------------------- 1 | package grnvs18.assignment2; 2 | 3 | class Arguments { 4 | public int frames = 10; 5 | public String iface = "eth0"; 6 | public int timeout = 10; 7 | 8 | private void printHelp() { 9 | System.out.println("Analyze and count raw Ethernet frames"); 10 | System.out.println("Usage: ethstats [-i|--interface interface] NUM"); 11 | System.out.println("-i/--interface: The interface on which the frames should be read from"); 12 | System.out.println("NUM: The number of frames to read before printing summary"); 13 | System.out.println("-?/-h/--help Print this help message"); 14 | } 15 | 16 | Arguments(String[] argv) { 17 | //For_each would be nice, but we may have to skip/access next 18 | int i, j = 0; 19 | String[] fargs = new String[1]; 20 | for(i = 0; i < argv.length; ++i) { 21 | String arg = argv[i]; 22 | switch(arg) { 23 | case "-h": 24 | case "-?": 25 | case "--help": 26 | printHelp(); 27 | System.exit(0); 28 | break; 29 | case "-i": 30 | case "--interface": 31 | iface = argv[++i]; 32 | break; 33 | default: 34 | if(j == fargs.length) { 35 | System.out.println("Encountered an unexpected number of positional arguments"); 36 | System.exit(1); 37 | } 38 | fargs[j++] = arg; 39 | break; 40 | } 41 | } 42 | if(fargs[0] != null) { 43 | try { 44 | this.frames = Integer.parseInt(fargs[0]); 45 | } catch (NumberFormatException e) { 46 | System.err.format("\"%s\" is not a valid number.\n", fargs[0]); 47 | this.printHelp(); 48 | System.exit(1); 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/Animal.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | /** 4 | * Oberklasse fuer Tiere. 5 | */ 6 | public class Animal { 7 | 8 | // Attribute fuer den allen Tieren gemeinen Teil des Tierzustands 9 | public boolean female; // Weibchen? 10 | public boolean alive; // Lebendig? 11 | public String square; // Auf welchem Feld? (genau zwei Zeichen, z. B. "e4") 12 | public Position position; // Auf welchem Spielbrett? 13 | 14 | 15 | /** 16 | * Dem Konstruktor wird das Geschlecht des Tiers uebergeben. 17 | */ 18 | public Animal(boolean female) { 19 | //TODO 20 | } 21 | 22 | 23 | /** 24 | * Ermittelt die moeglichen Zuege gemaess den Spielregeln, 25 | * die das Tier von seinem Feld aus in der aktuellen Position 26 | * ausfuehren kann. 27 | *

28 | * Muss von jeder einzelnen Tierklasse ueberschrieben werden. 29 | */ 30 | public Move[] possibleMoves() { 31 | return null; 32 | } 33 | 34 | 35 | /** 36 | * Wird aufgerufen nach jeder Spielrunde 37 | * (quasi am Ende vom Tag - jede Spielrunde zaehlt als ein Tag). 38 | *

39 | * Muss in jeder einzelnen Tierklasse ueberschrieben sein! 40 | */ 41 | public void sunset() { 42 | // Methode (und Klasse Animal) sollten eigentlich als abstract deklariert sein. 43 | // Kommt spaeter in der Vorlesung noch dran... 44 | // Zum Verstaendnis reicht es, dass diese Methode ueberschrieben werden muss. 45 | // (Die folgende Zeile wird dann auch nie ausgefuehrt.) 46 | throw new RuntimeException("Method sunset should have been overridden"); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/eti18/ha3/State.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import java.util.HashSet; 4 | import java.util.Objects; 5 | import java.util.Set; 6 | 7 | public class State { 8 | 9 | private static int id_counter = 0; 10 | private final int id; 11 | private final String name; //may be useful for thinking or debugging 12 | 13 | public State(){ 14 | id = id_counter++; 15 | this.name = ""; 16 | } 17 | 18 | public State(String name){ 19 | id = id_counter++; 20 | this.name = name; 21 | } 22 | 23 | /** 24 | * Returns a set of states with size given by the parameter size. 25 | * The name of each state is the empty String. 26 | * @param size Gives the size of the set 27 | * @return A set of states with size given by the parameter size. 28 | */ 29 | public static Set getStateSet(int size){ 30 | Set result = new HashSet(); 31 | for (; size>0; size--){ 32 | result.add(new State()); 33 | } 34 | return result; 35 | } 36 | 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | @Override 43 | public String toString(){ 44 | if (name!=""){ 45 | return name; 46 | } 47 | else{ 48 | return "s"+id; 49 | } 50 | } 51 | 52 | @Override 53 | public boolean equals(Object o) { 54 | if (this == o) return true; 55 | if (o == null || getClass() != o.getClass()) return false; 56 | State state = (State) o; 57 | return id == state.id; 58 | } 59 | 60 | @Override 61 | public int hashCode() { 62 | return Objects.hash(id); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/eti18/ha5/State.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import java.util.HashSet; 4 | import java.util.Objects; 5 | import java.util.Set; 6 | 7 | public class State { 8 | 9 | private static int id_counter = 0; 10 | private final int id; 11 | private final String name; //may be useful for thinking or debugging 12 | 13 | public State(){ 14 | id = id_counter++; 15 | this.name = ""; 16 | } 17 | 18 | public State(String name){ 19 | id = id_counter++; 20 | this.name = name; 21 | } 22 | 23 | /** 24 | * Returns a set of states with size given by the parameter size. 25 | * The name of each state is the empty String. 26 | * @param size Gives the size of the set 27 | * @return A set of states with size given by the parameter size. 28 | */ 29 | public static Set getStateSet(int size){ 30 | Set result = new HashSet(); 31 | for (; size>0; size--){ 32 | result.add(new State()); 33 | } 34 | return result; 35 | } 36 | 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | @Override 43 | public String toString(){ 44 | if (name!=""){ 45 | return name; 46 | } 47 | else{ 48 | return "s"+id; 49 | } 50 | } 51 | 52 | @Override 53 | public boolean equals(Object o) { 54 | if (this == o) return true; 55 | if (o == null || getClass() != o.getClass()) return false; 56 | State state = (State) o; 57 | return id == state.id; 58 | } 59 | 60 | @Override 61 | public int hashCode() { 62 | return Objects.hash(id); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/gad17/blatt03/RingQueue.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt03; 2 | 3 | /** 4 | * Die Klasse RingQueue soll eine zirkuläre Warteschlange auf 5 | * Basis der Klasse {@link DynamicArray} implementieren. 6 | */ 7 | public class RingQueue { 8 | private DynamicArray dynArr; 9 | 10 | private int size; 11 | 12 | private int from; 13 | 14 | private int to; 15 | 16 | public int getSize() { 17 | return size; 18 | } 19 | 20 | public boolean isEmpty() { 21 | /* 22 | * Todo 23 | */ 24 | return false; 25 | } 26 | 27 | /** 28 | * Dieser Konstruktor erzeugt eine neue Ringschlange. Ein leere 29 | * Ringschlange habe stets eine Größe von 0, sowie auf 0 30 | * gesetzte Objektvariablen to und from. 31 | * 32 | * @param growthFactor der Wachstumsfaktor des zugrundeliegenden 33 | * dynamischen Feldes 34 | * @param maxOverhead der maximale Overhead des zugrundeliegenden 35 | * dynamischen Feldes 36 | */ 37 | public RingQueue(int growthFactor, int maxOverhead) { 38 | dynArr = new DynamicArray(growthFactor, maxOverhead); 39 | size = 0; 40 | from = 0; 41 | to = 0; 42 | } 43 | 44 | /** 45 | * Diese Methode reiht ein Element in die Schlange ein. 46 | * 47 | * @param value der einzufügende Wert 48 | */ 49 | public void enqueue(int value) { 50 | /* 51 | * Todo 52 | */ 53 | } 54 | 55 | /** 56 | * Diese Methode entfernt ein Element aus der Warteschlange. 57 | * 58 | * @return das entfernte Element 59 | */ 60 | public int dequeue() { 61 | /* 62 | * Todo 63 | */ 64 | return -2; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/pgdp16/blatt06/MatrixMultOptimizationTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt06; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | 11 | @RunWith(Parameterized.class) 12 | public class MatrixMultOptimizationTest { 13 | private final int expected; 14 | private final int[][] input; 15 | 16 | public MatrixMultOptimizationTest(int expected, int[][] input) { 17 | this.expected = expected; 18 | this.input = input; 19 | } 20 | 21 | @Parameterized.Parameters(name = "expected: {0}") 22 | public static Collection data() { 23 | return Arrays.asList(new Object[][]{ 24 | {0, new int[0][0]}, 25 | {0, new int[][]{{}}}, 26 | {0, new int[][]{{10, 30}}}, 27 | {0, new int[][]{{30, 5}}}, 28 | {0, new int[][]{{5, 60}}}, 29 | {1, new int[][]{{1, 1}, {1, 1}}}, 30 | {8, new int[][]{{2, 2}, {2, 2}}}, 31 | {4500, new int[][]{{10, 30}, {30, 5}, {5, 60}}}, 32 | {286110, new int[][]{{85, 42}, {42, 33}, {33, 60}}}, 33 | {500610, new int[][]{{85, 42}, {42, 33}, {33, 60} , {60 , 80}}}, 34 | {13345101, new int[][]{{251,99},{99,666},{666,147}}}, 35 | {798200504, new int[][]{{420,69},{69,1337},{1337,9001}}}, 36 | {428, new int[][]{{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10},{10,10}}}, 37 | }); 38 | } 39 | 40 | @Test 41 | public void f() throws Exception { 42 | Assert.assertEquals(expected, MatrixMultOptimization.f(input)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/eti18/ha3/AcceptDFATest.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.Scanner; 11 | 12 | @RunWith(Parameterized.class) 13 | public class AcceptDFATest { 14 | private final DFA d; 15 | private final String w; 16 | private final boolean expected; 17 | 18 | @Parameterized.Parameters(name = "{index}: {1} -> {2}") 19 | public static Collection data() { 20 | return Arrays.asList(new Object[][]{ 21 | {"abababc", false}, 22 | {"cc", true}, 23 | {"ccc", true}, 24 | {"caaabbcabaabc", true}, 25 | {"x", false}, 26 | {"ccx", false}, 27 | }); 28 | } 29 | 30 | 31 | public AcceptDFATest(String w, boolean expected) { 32 | String d = "DFA\n" + 33 | "Alphabet: a;b;c\n" + 34 | "States: p;q;r\n" + 35 | "Init: p\n" + 36 | "Final: r\n" + 37 | "Transitions:\n" + 38 | "p;a;p\n" + 39 | "p;b;p\n" + 40 | "p;c;q\n" + 41 | "q;a;q\n" + 42 | "q;b;q\n" + 43 | "q;c;r\n" + 44 | "r;a;r\n" + 45 | "r;b;r\n" + 46 | "r;c;r\n" + 47 | "END"; 48 | this.d = (DFA) Parser.parse(new Scanner(d)); 49 | this.w = w; 50 | this.expected = expected; 51 | } 52 | 53 | 54 | @Test 55 | public void test_accept() { 56 | Assert.assertEquals("\nd: " + d + "\nw: " + w + expected, expected, AcceptDFA.accept(d, w)); 57 | } 58 | } -------------------------------------------------------------------------------- /src/gad17/blatt09/AVLTreeNodeBuilder.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt09; 2 | 3 | import java.util.Arrays; 4 | import java.util.TreeSet; 5 | 6 | public class AVLTreeNodeBuilder extends AVLTreeNode { 7 | int validAVLCounter; 8 | int insertInput; 9 | int findInput; 10 | private int height; 11 | private TreeSet tree = new TreeSet(); 12 | 13 | private AVLTreeNodeBuilder(int key) { 14 | super(key); 15 | tree.add(key); 16 | } 17 | 18 | static AVLTreeNodeBuilder node() { 19 | return new AVLTreeNodeBuilder(0); 20 | } 21 | 22 | public AVLTreeNodeBuilder withHeight(int height) { 23 | this.height = height; 24 | return this; 25 | } 26 | 27 | public AVLTreeNodeBuilder withKey(int key) { 28 | AVLTreeNodeBuilder out = new AVLTreeNodeBuilder(key); 29 | out.height = height; 30 | tree.remove(tree.first()); 31 | out.tree = tree; 32 | out.tree.add(key); 33 | return out; 34 | } 35 | 36 | public AVLTreeNodeBuilder withTree(Integer... key) { 37 | tree.addAll(Arrays.asList(key)); 38 | return this; 39 | } 40 | 41 | public AVLTreeNodeBuilder insert(int... keys) { 42 | for (int key : keys) insert(key); 43 | return this; 44 | } 45 | 46 | @Override 47 | public int height() { 48 | return height; 49 | } 50 | 51 | @Override 52 | public boolean find(int key) { 53 | findInput = key; 54 | return tree.contains(key); 55 | } 56 | 57 | @Override 58 | public boolean validAVL() { 59 | validAVLCounter++; 60 | return super.validAVL(); 61 | } 62 | 63 | @Override 64 | public AVLTreeNode insert(int key) { 65 | insertInput = key; 66 | return super.insert(key); 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /src/eti18/ha3/AcceptNFATest.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.Scanner; 11 | 12 | import static org.junit.Assert.*; 13 | 14 | @RunWith(Parameterized.class) 15 | public class AcceptNFATest { 16 | private final EpsilonNFA n; 17 | private final String w; 18 | private final boolean expected; 19 | 20 | @Parameterized.Parameters(name = "{index}: {1} -> {2}") 21 | public static Collection data() { 22 | return Arrays.asList(new Object[][]{ 23 | {"abababc", false}, 24 | {"cc", true}, 25 | {"ccc", true}, 26 | {"caaabbcabaabc", true}, 27 | {"x", false}, 28 | {"ccx", false}, 29 | }); 30 | } 31 | 32 | 33 | public AcceptNFATest(String w, boolean expected) { 34 | this.n = Parser.parse(new Scanner("EpsilonNFA\n" + 35 | "Alphabet: a;b;c\n" + 36 | "States: p;q;r\n" + 37 | "Init: p\n" + 38 | "Final: r\n" + 39 | "Transitions:\n" + 40 | "p;a;p\n" + 41 | "p;b;p\n" + 42 | "p;c;q\n" + 43 | "q;a;q\n" + 44 | "q;b;q\n" + 45 | "q;c;r\n" + 46 | "r;a;r\n" + 47 | "r;b;r\n" + 48 | "r;c;r\n" + 49 | "END")); 50 | this.w = w; 51 | this.expected = expected; 52 | } 53 | 54 | 55 | @Test 56 | public void test_accept() { 57 | Assert.assertEquals("\nn: "+ n + "\nw: "+ w + expected ,expected, AcceptNFA.accept(n,w)); 58 | } 59 | } -------------------------------------------------------------------------------- /src/eti18/ha5/Operations.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Operations { 6 | 7 | public static DFA union(DFA d1, DFA d2) { 8 | //TODO 9 | return null; 10 | } 11 | 12 | public static DFA setminus(DFA d1, DFA d2) { 13 | //TODO 14 | return null; 15 | } 16 | 17 | public static DFA xor(DFA d1, DFA d2) { 18 | //TODO 19 | return null; 20 | } 21 | 22 | public static void main(String[] args){ 23 | 24 | Scanner scanner = new Scanner(System.in); 25 | scanner.nextLine();//remove the number of lines in the beginning 26 | EpsilonNFA e = Parser.parse(scanner); 27 | DFA d1 = null; 28 | if(e instanceof DFA){ 29 | d1 = (DFA) e; 30 | } 31 | else{ 32 | System.out.println("First automaton is no DFA, aborting"); 33 | System.exit(3); 34 | } 35 | 36 | e = Parser.parse(scanner); 37 | DFA d2 = null; 38 | if(e instanceof DFA){ 39 | d2 = (DFA) e; 40 | } 41 | else{ 42 | System.out.println("Second automaton is no DFA, aborting"); 43 | System.exit(3); 44 | } 45 | 46 | switch (scanner.nextLine()){ 47 | case "UNION": 48 | System.out.println("Case #1:\n" + union(d1,d2)); 49 | break; 50 | case "SETMINUS": 51 | System.out.println("Case #1:\n" + setminus(d1,d2)); 52 | break; 53 | case "XOR": 54 | System.out.println("Case #1:\n" + xor(d1,d2)); 55 | break; 56 | default: 57 | System.err.println("Testcase does not specify one of the allowed operations."); 58 | } 59 | 60 | scanner.close(); 61 | } 62 | 63 | 64 | } 65 | 66 | -------------------------------------------------------------------------------- /src/eti18/ha3/PowersetTest.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.Scanner; 11 | 12 | import static org.junit.Assert.*; 13 | 14 | @RunWith(Parameterized.class) 15 | public class PowersetTest { 16 | private final NFA n; 17 | private final DFA expected; 18 | 19 | @Parameterized.Parameters(name = "{index}: {0} -> {1}") 20 | public static Collection data() { 21 | return Arrays.asList(new Object[][]{ 22 | {"NFA\n" + 23 | "Alphabet: a\n" + 24 | "States: p;q\n" + 25 | "Init: p\n" + 26 | "Final: q\n" + 27 | "Transitions:\n" + 28 | "p;a;p\n" + 29 | "p;a;q\n" + 30 | "q;a;q\n" + 31 | "END", 32 | "DFA\n" + 33 | "Alphabet: a\n" + 34 | "States: [p];[q, p]\n" + 35 | "Init: [p]\n" + 36 | "Final: [q, p]\n" + 37 | "Transitions:\n" + 38 | "[q, p];a;[q, p]\n" + 39 | "[p];a;[q, p]\n" + 40 | "END"}, 41 | }); 42 | } 43 | 44 | public PowersetTest(String d, String expected) { 45 | this.expected = (DFA) Parser.parse(new Scanner(expected)); 46 | this.n = (NFA) Parser.parse(new Scanner(d)); 47 | } 48 | 49 | @Test 50 | public void test_accept() { 51 | Assert.assertEquals(expected, Powerset.powersetConstruction(n)); 52 | } 53 | } -------------------------------------------------------------------------------- /src/gad17/blatt09/AVLTreeTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt09; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import static gad17.blatt09.AVLTreeNodeBuilder.node; 8 | 9 | public class AVLTreeTest { 10 | private AVLTree test; 11 | 12 | @Before 13 | public void setUp() throws Exception { 14 | test = new AVLTree(); 15 | } 16 | 17 | @Test 18 | public void validALL_givenEmpty_returnsFalse() throws Exception { 19 | Assert.assertFalse(test.validAVL()); 20 | } 21 | 22 | @Test 23 | public void validALL_givenNotEmpty_thenCalls_validAVL() throws Exception { 24 | AVLTreeNodeBuilder node = node(); 25 | test.root = node; 26 | 27 | Assert.assertTrue(test.validAVL()); 28 | Assert.assertEquals(1, node.validAVLCounter); 29 | } 30 | 31 | @Test 32 | public void insert_givenEmpty_thenRootIsNotNull() throws Exception { 33 | test.insert(1); 34 | Assert.assertNotNull(test.root); 35 | } 36 | 37 | @Test 38 | public void insert_givenEmpty_input10_thenRootHas10() throws Exception { 39 | test.insert(10); 40 | Assert.assertTrue(test.root.find(10)); 41 | } 42 | 43 | @Test 44 | public void insert_givenNotEmpty_thenCalls_insert() throws Exception { 45 | AVLTreeNodeBuilder node = node(); 46 | test.root = node; 47 | 48 | test.insert(10); 49 | Assert.assertEquals(10, node.insertInput); 50 | } 51 | 52 | @Test 53 | public void find_givenEmpty_returnsFalse() throws Exception { 54 | Assert.assertFalse(test.find(10)); 55 | } 56 | 57 | @Test 58 | public void find_givenNotEmpty_thenCalls_find() throws Exception { 59 | AVLTreeNodeBuilder node = node(); 60 | test.root = node; 61 | 62 | Assert.assertTrue(test.find(0)); 63 | Assert.assertEquals(0, node.findInput); 64 | } 65 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/MoveTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class MoveTest { 7 | private Move test; 8 | 9 | @Test 10 | public void constructor_given_a1a2() throws Exception { 11 | String input = "a1a2"; 12 | test = new Move(input); 13 | assertToString(input); 14 | } 15 | 16 | @Test 17 | public void constructor_given_a7c5() throws Exception { 18 | String input = "a7c5"; 19 | test = new Move(input); 20 | assertToString(input); 21 | } 22 | 23 | @Test 24 | public void constructor_given_a1_given_a2() throws Exception { 25 | test = new Move("a1", "a2"); 26 | assertToString("a1a2"); 27 | } 28 | 29 | @Test 30 | public void constructor_given_a7_given_c5() throws Exception { 31 | test = new Move("a7", "c5"); 32 | assertToString("a7c5"); 33 | } 34 | 35 | private void assertToString(String input) { 36 | Assert.assertEquals("ToString() is wrong:\n", input, test.toString()); 37 | } 38 | 39 | @Test 40 | public void equals_given_a1a2_givenNull_returnsFalse() throws Exception { 41 | test = new Move("a1a2"); 42 | 43 | assertEquals(false, null); 44 | } 45 | 46 | @Test 47 | public void equals_given_a1a2_given_a1a2_returnsTrue() throws Exception { 48 | test = new Move("a1a2"); 49 | 50 | assertEquals(true, new Move("a1a2")); 51 | } 52 | 53 | @Test 54 | public void equals_given_a1a2_given_a7c5_returnsFalse() throws Exception { 55 | test = new Move("a1a2"); 56 | 57 | assertEquals(false, new Move("a7c5")); 58 | } 59 | 60 | private void assertEquals(boolean expected, Move other) { 61 | Assert.assertEquals("equals() is wrong:\n" + 62 | "this: " + test + "\n" + 63 | "other: " + other, expected, test.equals(other)); 64 | } 65 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt07/HeadList.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt07; 2 | 3 | public class HeadList { 4 | 5 | Entry head; 6 | 7 | /** 8 | * constructor empty HeadList 9 | */ 10 | HeadList() { 11 | head = null; 12 | } 13 | 14 | /** 15 | * Appends a new element with value info to the end of this list 16 | * @param info value of the new element 17 | */ 18 | public void add(int info) { 19 | 20 | //TODO 21 | 22 | } 23 | 24 | /** 25 | * Removes and returns the element at position index from this list. 26 | * @param index position of the element that is removed 27 | * @return value of the removed element 28 | */ 29 | public int remove(int index) { 30 | 31 | //TODO 32 | return -1; 33 | } 34 | 35 | /** 36 | * sets the head of each list element to newHead 37 | * @param newHead reference to the new head 38 | */ 39 | private void setHead(Entry newHead) { 40 | 41 | //TODO 42 | 43 | } 44 | 45 | /** 46 | * reverse the list 47 | * example: [1,2,3,4,5] --> [5,4,3,2,1], [] --> [], [1] --> [1] 48 | */ 49 | public void reverse() { 50 | 51 | //TODO 52 | 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | String out = "["; 58 | if (head != null) { 59 | out += head.elem; 60 | Entry tmp = head.next; 61 | while (tmp != null) { 62 | out = out + "," + tmp.elem; 63 | tmp = tmp.next; 64 | } 65 | } 66 | out += "]"; 67 | return out; 68 | } 69 | 70 | public static void main(String[] args) { 71 | HeadList l = new HeadList(); 72 | System.out.println("empty list: " + l); 73 | // Test implementation 74 | 75 | } 76 | 77 | class Entry { 78 | 79 | Entry first; 80 | Entry next; 81 | int elem; 82 | 83 | public Entry(Entry first, Entry next, int elem) { 84 | this.first = first; 85 | this.next = next; 86 | this.elem = elem; 87 | } 88 | 89 | } 90 | 91 | } 92 | 93 | -------------------------------------------------------------------------------- /src/gad17/blatt11/ConnectedComponentsTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt11; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import java.util.Random; 8 | import java.util.stream.LongStream; 9 | 10 | public class ConnectedComponentsTest { 11 | private ConnectedComponents test; 12 | private Graph g; 13 | 14 | private void addNodes(int times) { 15 | for (int i = 0; i < times; i++) g.addNode(); 16 | } 17 | 18 | private void connect(int node, int... connected) { 19 | for (int i : connected) g.addEdge(g.getNode(node), g.getNode(i)); 20 | } 21 | 22 | private void buildWay(int start, int end) { 23 | for (int i = start; i < end - 1; i++) connect(i, i + 1); 24 | } 25 | 26 | @Before 27 | public void setUp() throws Exception { 28 | test = new ConnectedComponents(); 29 | g = new Graph(); 30 | } 31 | 32 | @Test 33 | public void count_givenGrapthEmpty_returns0() throws Exception { 34 | Assert.assertEquals(0, test.countConnectedComponents(g)); 35 | } 36 | 37 | @Test 38 | public void count_givenGraphWithoutConnections_returns10() throws Exception { 39 | addNodes(10); 40 | Assert.assertEquals(10, test.countConnectedComponents(g)); 41 | } 42 | 43 | @Test 44 | public void count_givenGraphWithWayLength10_returns1() throws Exception { 45 | addNodes(10); 46 | buildWay(0, 10); 47 | Assert.assertEquals(1, test.countConnectedComponents(g)); 48 | } 49 | 50 | @Test 51 | public void count_given15Nodes_givenGraphWithWayLength11_returns5() throws Exception { 52 | addNodes(15); 53 | buildWay(2, 13); 54 | Assert.assertEquals(5, test.countConnectedComponents(g)); 55 | } 56 | 57 | @Test 58 | public void count_givenRandomNodesSize_givenEverySecondConnected_returnsHalf() throws Exception { 59 | int times = new Random().nextInt(333) * 2; 60 | addNodes(times); 61 | LongStream.range(0, times).filter(i -> i % 2 == 0) 62 | .forEach(i -> connect((int) i, (int) i + 1)); 63 | Assert.assertEquals(times / 2, test.countConnectedComponents(g)); 64 | } 65 | 66 | 67 | } -------------------------------------------------------------------------------- /src/gad17/blatt04/StoreTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import junit.framework.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import java.util.Hashtable; 8 | import java.util.Map; 9 | 10 | public class StoreTest { 11 | 12 | private Map getData() { 13 | return Store.data; 14 | } 15 | 16 | private ReadResponse onReadRequest(ReadRequest readRequest) { 17 | return Store.onReadRequest(readRequest); 18 | } 19 | 20 | private void onStoreRequest(StoreRequest storeRequest) { 21 | Store.onStoreRequest(storeRequest); 22 | } 23 | 24 | @Before 25 | public void setup() { 26 | Store.data = new Hashtable<>(); 27 | } 28 | 29 | @Test 30 | public void table_givenMain_thenIsNotNull() throws Exception { 31 | Store.data = null; 32 | Store.init(); 33 | Assert.assertNotNull(getData()); 34 | } 35 | 36 | @Test(expected = Exception.class) 37 | public void eventStore_inputNull_throwsException() throws Exception { 38 | onStoreRequest(null); 39 | } 40 | 41 | @Test 42 | public void eventStore_input123_123_thenSaves123At123() { 43 | onStoreRequest(new StoreRequest("123", 123)); 44 | Assert.assertNotNull(getData()); 45 | Assert.assertNotNull(getData().get("123")); 46 | Assert.assertEquals(123, (int) getData().get("123")); 47 | } 48 | 49 | @Test(expected = Exception.class) 50 | public void eventRead_inputNull_throwsException() throws Exception { 51 | onReadRequest(null); 52 | } 53 | 54 | @Test 55 | public void eventRead_input123_valueNotFound_returnSerializableOptionalEmpty() { 56 | assertEventRead("123", new ReadResponse()); 57 | } 58 | 59 | private void assertEventRead(String key, ReadResponse expected) { 60 | ReadResponse actual = onReadRequest(new ReadRequest(key)); 61 | Assert.assertNotNull(actual); 62 | Assert.assertEquals(expected.getValue(), actual.getValue()); 63 | } 64 | 65 | @Test 66 | public void eventRead_input123_valueFound_return123() { 67 | getData().put("123", 123); 68 | assertEventRead("123", new ReadResponse(123)); 69 | } 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/pgdp16/blatt08/SymmetricStack.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt08; 2 | 3 | public class SymmetricStack { 4 | 5 | private int[] data; 6 | private int first; 7 | private int last; 8 | 9 | public SymmetricStack() { 10 | // TODO 11 | } 12 | 13 | public int getFirst() { 14 | return first; 15 | } 16 | 17 | public void setFirst(int first) { 18 | this.first = first; 19 | } 20 | 21 | public int getLast() { 22 | return last; 23 | } 24 | 25 | public void setLast(int last) { 26 | this.last = last; 27 | } 28 | 29 | public int[] getData() { 30 | return data; 31 | } 32 | 33 | public void setData(int[] data) { 34 | this.data = data; 35 | } 36 | 37 | public int getNumberOfElements() { 38 | // TODO 39 | return -1; 40 | } 41 | 42 | public void increase() { 43 | // TODO 44 | } 45 | 46 | public void decrease() { 47 | // TODO 48 | } 49 | 50 | public boolean isEmpty() { 51 | // TODO 52 | return false; 53 | } 54 | 55 | public boolean isFull() { 56 | // TODO 57 | return false; 58 | } 59 | 60 | public void prepend(int x) { 61 | // TODO 62 | } 63 | 64 | public void append(int x) { 65 | // TODO 66 | } 67 | 68 | public void removeFirst() { 69 | // TODO 70 | } 71 | 72 | public void removeLast() { 73 | // TODO 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | String out = ""; 79 | for (int i = 0; i < data.length; i++) { 80 | if (first <= last && (i < first || i > last)) 81 | out += "* "; 82 | if (first <= last && i > first && i < last) 83 | out += " " + data[i]; 84 | if (first > last && i > last && i < first) 85 | out += "* "; 86 | if (first > last && (i > first || i < last)) 87 | out += " " + data[i]; 88 | if (i == first) 89 | out = out + "(" + data[i]; 90 | if (i == last) 91 | out += (first == last ? "" : " " + data[i]) + ")"; 92 | } 93 | return out; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/gad17/blatt04/MasterTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.io.IOException; 7 | import java.net.ServerSocket; 8 | import java.net.Socket; 9 | import java.net.SocketException; 10 | 11 | public class MasterTest { 12 | 13 | private Socket getStore(IRequest request) { 14 | return Master.getStore(request); 15 | } 16 | 17 | private void runInit() { 18 | Master.init(2); 19 | } 20 | 21 | private void runInitStores() throws IOException { 22 | Master.initStores(2, new ServerSocket() { 23 | @Override 24 | public Socket accept() throws IOException { 25 | return new Socket(); 26 | } 27 | }); 28 | } 29 | 30 | @Test(expected = Exception.class) 31 | public void getStore_inputNull_throwsException() { 32 | getStore(null); 33 | } 34 | 35 | @Test 36 | public void init_givenStoreCount2_thenStoreArrayIsSize2() throws Exception { 37 | runInit(); 38 | Assert.assertNotNull(Master.storeSockets); 39 | Assert.assertEquals(2, Master.storeSockets.length); 40 | } 41 | 42 | @Test 43 | public void init_givenStoreCount2_thenInitializeHashStringWithSize2() throws Exception { 44 | runInit(); 45 | Assert.assertNotNull(Master.hashString); 46 | Assert.assertEquals(2, Master.hashString.size); 47 | } 48 | 49 | @Test 50 | public void initStores_givenStoreCount2_thenInitializeStoresInArray() throws Exception { 51 | runInit(); 52 | runInitStores(); 53 | Assert.assertNotNull(Master.storeSockets); 54 | for (Socket store : Master.storeSockets) Assert.assertNotNull(store); 55 | } 56 | 57 | @Test 58 | public void getStore_inputIRequestWithKey123_returnsStore() throws SocketException { 59 | runInit(); 60 | Assert.assertNotNull(Master.storeSockets); 61 | Master.storeSockets[0] = new Socket(); 62 | Master.storeSockets[1] = new Socket(); 63 | Master.storeSockets[0].setSendBufferSize(1); 64 | Master.storeSockets[1].setSendBufferSize(2); 65 | Socket actual = getStore(new ReadRequest(((char) 0) + "")); 66 | Assert.assertEquals(1, actual.getSendBufferSize()); 67 | } 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unit Tests for TUM Informatic's Homework 2 | 3 | ## **Disclaimer**: Do not solely rely on these Unit Tests for correction. Mistakes happen and sometimes things may slip past the tests. 4 | 5 | ### Availible Subjects 6 | * EIDI 1 WS16 7 | * GAD SS17 8 | 9 | ### Contact 10 | * [Announcement / News Channel](https://telegram.me/TUMUnitTests) 11 | * [Chat](https://telegram.me/joinchat/CBbdCQtAOI9Qalx6JlYdcw) 12 | 13 | **You can also notify us of issues and bugs by submitting a GitHub issue.** 14 | 15 | ### Purpose of a Unit Test 16 | 17 | The Unit Test's purpose, is to help identify errors in the programs written for PGDP and GAD. It does this by creating test cases, in which inputs are entered into the classes, methods being run and finally checking to see if the output is what is to be expected. 18 | 19 | ### Setup 20 | 21 | The setup of the UnitTests vary depending on the IDE. If your IDE is not listed here, post it as an issue. 22 | 23 | ##### Eclipse 24 | 25 | 1. Select the Project 26 | 2. Click: Project > Properties 27 | 3. Click: Java Build Path > Libraries > Add Library... 28 | 4. Click: Junit > Next > Select Junit 4 (or higher) > Finish 29 | 30 | >https://youtu.be/CD5JJ5bDAfY?t=2m33s 31 | 32 | ##### NetBeans 33 | 34 | 1. Select the Project 35 | 2. Right click on Libaries 36 | 3. Add Libary 37 | 4. Select "Junit 4.12" (or higher) and add libary 38 | 5. Right Click on Project > New > Other > Junit Test 39 | 6. Move your method and the method's test class into the Junit Test 40 | 7. Right Click on MethodNameTest > Test File 41 | 42 | >https://www.youtube.com/watch?v=C6oQqbqPBB0 43 | 44 | ##### IntelliJ 45 | 46 | 1. Either put the Test and Production Code (your code) into the TestFolder 47 | 2. Or be sure to fix your imports and scope (it is just a bit cleaner). 48 | 49 | >https://www.youtube.com/watch?v=Bld3644bIAo 50 | 51 | ##### BlueJ 52 | BlueJ automatically comes with JUnit, thus you will not need to import it into your project. Be aware that BLUEJ uses an OUTDATED version of JUNIT, thus may not accept all methods used in the test. It is advisable to have eclipse of another IDE for testing purposes. 53 | 54 | 1. Copy the Test Files into your Project 55 | 2. Restart BlueJ 56 | 3. Compile. The Unit Test should turn Green 57 | 4. Right Click on the Unit Test and hit Test All (or the specific Test you require) 58 | -------------------------------------------------------------------------------- /src/eti18/ha5/IsEmptyTest.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.Scanner; 11 | 12 | @RunWith(Parameterized.class) 13 | public class IsEmptyTest { 14 | 15 | 16 | private final DFA dfa; 17 | private final boolean expected; 18 | 19 | @Parameterized.Parameters(name = "{index}: {0} -> {1}") 20 | public static Collection data() { 21 | return Arrays.asList(new Object[][]{ 22 | {"DFA\n" + 23 | "Alphabet: a;b;c\n" + 24 | "States: p;q;r\n" + 25 | "Init: p\n" + 26 | "Final: r\n" + 27 | "Transitions:\n" + 28 | "p;a;p\n" + 29 | "p;b;p\n" + 30 | "p;c;q\n" + 31 | "q;a;q\n" + 32 | "q;b;q\n" + 33 | "q;c;r\n" + 34 | "r;a;r\n" + 35 | "r;b;r\n" + 36 | "r;c;r\n" + 37 | "END", false}, 38 | {"DFA\n" + 39 | "Alphabet: a\n" + 40 | "States: p;q\n" + 41 | "Init: p\n" + 42 | "Final: q\n" + 43 | "Transitions:\n" + 44 | "p;a;q\n" + 45 | "q;a;q\n" + 46 | "END", false}, 47 | {"DFA\n" + 48 | "Alphabet: a\n" + 49 | "States: p;q;f\n" + 50 | "Init: p\n" + 51 | "Final: q\n" + 52 | "Transitions:\n" + 53 | "p;a;q\n" + 54 | "q;a;f\n" + 55 | "f;a;f\n" + 56 | "END", false}, 57 | }); 58 | } 59 | 60 | public IsEmptyTest(String dfa, boolean expected) { 61 | this.dfa = (DFA) Parser.parse(new Scanner(dfa)); 62 | this.expected = expected; 63 | } 64 | 65 | @Test 66 | public void isEmpty() { 67 | Assert.assertEquals("\nd: " + dfa, 68 | expected, IsEmpty.isEmpty(dfa)); 69 | } 70 | } -------------------------------------------------------------------------------- /src/eti18/ha5/IsFiniteTest.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.Scanner; 11 | 12 | 13 | @RunWith(Parameterized.class) 14 | public class IsFiniteTest { 15 | 16 | private final DFA dfa; 17 | private final boolean expected; 18 | 19 | @Parameterized.Parameters(name = "{index}: {0} -> {1}") 20 | public static Collection data() { 21 | return Arrays.asList(new Object[][]{ 22 | {"DFA\n" + 23 | "Alphabet: a;b;c\n" + 24 | "States: p;q;r\n" + 25 | "Init: p\n" + 26 | "Final: r\n" + 27 | "Transitions:\n" + 28 | "p;a;p\n" + 29 | "p;b;p\n" + 30 | "p;c;q\n" + 31 | "q;a;q\n" + 32 | "q;b;q\n" + 33 | "q;c;r\n" + 34 | "r;a;r\n" + 35 | "r;b;r\n" + 36 | "r;c;r\n" + 37 | "END", false}, 38 | {"DFA\n" + 39 | "Alphabet: a\n" + 40 | "States: p;q\n" + 41 | "Init: p\n" + 42 | "Final: q\n" + 43 | "Transitions:\n" + 44 | "p;a;q\n" + 45 | "q;a;q\n" + 46 | "END", false}, 47 | {"DFA\n" + 48 | "Alphabet: a\n" + 49 | "States: p;q;f\n" + 50 | "Init: p\n" + 51 | "Final: q\n" + 52 | "Transitions:\n" + 53 | "p;a;q\n" + 54 | "q;a;f\n" + 55 | "f;a;f\n" + 56 | "END", true}, 57 | }); 58 | } 59 | 60 | public IsFiniteTest(String dfa, boolean expected) { 61 | this.dfa = (DFA) Parser.parse(new Scanner(dfa)); 62 | this.expected = expected; 63 | } 64 | 65 | @Test 66 | public void isFinite() { 67 | Assert.assertEquals("\nd: " + dfa, 68 | expected, IsFinite.isFinite(dfa)); 69 | } 70 | } -------------------------------------------------------------------------------- /src/gad17/blatt09/AVLTree.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt09; 2 | 3 | /** 4 | * Diese Klasse implementiert einen AVL-Baum. 5 | */ 6 | public class AVLTree { 7 | /** 8 | * Diese Variable speichert die Wurzel des Baumes. 9 | */ 10 | AVLTreeNode root = null; 11 | 12 | public AVLTree() { 13 | } 14 | 15 | /** 16 | * Diese Methode ist zum Debuggen gedacht und prüft, ob es sich um einen 17 | * validen AVL-Baum handelt. Dabei werden die folgenden Eigenschaften geprüft: 18 | *

19 | * - Die Höhe des linken Teilbaumes eines Knotens unterscheidet sich von der 20 | * Höhe des rechten Teilbaumes um höchstens eins. - Die Schlüssel im linken 21 | * Teilbaum eines Knotens sind kleiner als der oder gleich dem Schlüssel des 22 | * Knotens. - Die Schlüssel im rechten Teilbaum eines Knotens sind größer als 23 | * der Schlüssel des Knotens. - Die Balancierung jedes Knoten entspricht der 24 | * Höhendifferenz der Teilbäume entsprechend der Erklärung in der Vorlesung. 25 | * 26 | * @return 'true' falls der Baum ein valider AVL-Baum ist, 'false' sonst 27 | */ 28 | public boolean validAVL() { 29 | // TODO... 30 | return true; 31 | } 32 | 33 | /** 34 | * Diese Methode fügt einen neuen Schlüssel in den AVL-Baum ein. 35 | * 36 | * @param key der einzufügende Schlüssel 37 | */ 38 | public void insert(int key) { 39 | // TODO... 40 | } 41 | 42 | /** 43 | * Diese Methode sucht einen Schlüssel im AVL-Baum. 44 | * 45 | * @param key der Schlüssel, der gesucht werden soll 46 | * @return 'true', falls der Schlüssel gefunden wurde, 'false' sonst 47 | */ 48 | public boolean find(int key) { 49 | // TODO... 50 | return true; 51 | } 52 | 53 | /** 54 | * Diese Methode wandelt den Baum in das Graphviz-Format um. 55 | * 56 | * @return der Baum im Graphiz-Format 57 | */ 58 | public String dot() { 59 | StringBuilder sb = new StringBuilder(); 60 | sb.append("digraph {\n"); 61 | if (root != null) 62 | root.dot(sb); 63 | sb.append("}"); 64 | return sb.toString(); 65 | } 66 | 67 | /** 68 | * Diese Methode wandelt den Baum in das Graphviz-Format um. 69 | * 70 | * @return der Baum im Graphiz-Format 71 | */ 72 | @Override 73 | public String toString() { 74 | return dot(); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/pgdp/FruitTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.pgdp; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | 11 | @RunWith(Parameterized.class) 12 | public class FruitTest { 13 | private final String testClass; 14 | private final Fruit test; 15 | private final boolean expIsApple; 16 | private final boolean expIsBanana; 17 | private final boolean expIsPineapple; 18 | private final int expShelfLife; 19 | 20 | public FruitTest(String testClass, Fruit test, boolean expIsApple, boolean expIsBanana, boolean expIsPineapple, int expShelfLife) { 21 | this.testClass = testClass; 22 | this.test = test; 23 | this.expIsApple = expIsApple; 24 | this.expIsBanana = expIsBanana; 25 | this.expIsPineapple = expIsPineapple; 26 | this.expShelfLife = expShelfLife; 27 | } 28 | 29 | @Parameterized.Parameters(name = "{0}") 30 | public static Collection data() { 31 | return Arrays.asList(new Object[][]{ 32 | {"Fruit", new Fruit(), false, false, false, -1}, 33 | {"Apple", new Apple(), true, false, false, 30}, 34 | {"GrannySmith", new GrannySmith(), true, false, false, 50}, 35 | {"PinkLady", new PinkLady(), true, false, false, 20}, 36 | {"Banana", new Banana(), false, true, false, 7}, 37 | {"Pineapple", new Pineapple(), false, false, true, 20}, 38 | }); 39 | } 40 | 41 | @Test 42 | public void isApple() throws Exception { 43 | Assert.assertEquals(messageMethod("isApple()"), expIsApple, test.isApple()); 44 | } 45 | 46 | @Test 47 | public void isBanana() throws Exception { 48 | Assert.assertEquals(messageMethod("isBanana()"), expIsBanana, test.isBanana()); 49 | } 50 | 51 | @Test 52 | public void isPineapple() throws Exception { 53 | Assert.assertEquals(messageMethod("isPineapple()"), expIsPineapple, test.isPineapple()); 54 | } 55 | 56 | @Test 57 | public void shelfLife() throws Exception { 58 | Assert.assertEquals(messageMethod("shelfLife()"), expShelfLife, test.shelfLife()); 59 | } 60 | 61 | private String messageMethod(String method) { 62 | return "There is something wrong with " + testClass + "." + method + ":"; 63 | } 64 | 65 | 66 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/ExceptionTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | 11 | @RunWith(Parameterized.class) 12 | public class ExceptionTest { 13 | public static final int RANGE = Character.MAX_VALUE; 14 | private final Exception test; 15 | private final Object[] params; 16 | 17 | public ExceptionTest(String name, Exception test, Object... params) { 18 | this.test = test; 19 | this.params = params; 20 | } 21 | 22 | @Parameterized.Parameters(name = "{0}") 23 | public static Collection data() { 24 | int is = randomInt(RANGE); 25 | int should = randomInt(RANGE) + 1 + is; 26 | return Arrays.asList(new Object[][]{ 27 | {"IllegalCharExc", new IllegalCharExc((char) is), new Object[]{(char) is}}, 28 | {"IllegalCharExc: \\n", new IllegalCharExc('\n'), new Object[]{"\\n"}}, 29 | {"IllegalCharExc: \\t", new IllegalCharExc('\t'), new Object[]{"\\t"}}, 30 | {"IllegalCharExc: \\r", new IllegalCharExc('\r'), new Object[]{"\\r"}}, 31 | {"IllegalCharExc: \\b", new IllegalCharExc('\b'), new Object[]{"\\b"}}, 32 | {"IllegalCharExc: \\f", new IllegalCharExc('\f'), new Object[]{"\\f"}}, 33 | 34 | {"NotEnoughLower", new NotEnoughLower(should, is), new Object[]{should, is}}, 35 | {"NotEnoughNumber", new NotEnoughNumber(should, is), new Object[]{should, is}}, 36 | {"NotEnoughSpecial", new NotEnoughSpecial(should, is), new Object[]{should, is}}, 37 | {"NotEnoughUpper", new NotEnoughUpper(should, is), new Object[]{should, is}}, 38 | {"NotLongEnoughExc", new NotLongEnoughExc(should, is), new Object[]{should, is}}, 39 | }); 40 | } 41 | 42 | private static int randomInt(int range) { 43 | return (char) (Math.random() * range); 44 | } 45 | 46 | @Test 47 | public void toString_givenParams() throws Exception { 48 | try { 49 | throw test; 50 | } catch (Exception e) { 51 | System.out.println(e); 52 | Arrays.stream(params).forEach(param -> 53 | Assert.assertTrue(e.toString().contains( 54 | String.valueOf(param)))); 55 | } 56 | } 57 | 58 | 59 | } -------------------------------------------------------------------------------- /src/eti18/ha3/DFA.java: -------------------------------------------------------------------------------- 1 | package eti18.ha3; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class DFA extends NFA { 7 | 8 | public DFA(Set states, Set transitions, Set alphabet, State startState, Set finalStates) { 9 | super(states, transitions, alphabet, startState, finalStates); 10 | checkValidDFA(); 11 | } 12 | 13 | /** 14 | * Returns the successor of a state-letter pair. 15 | * It surely exists, because the transition relation was checked to be total in checkValidDFA. 16 | * There surely is only one, because that was also checked in checkValidDFA 17 | * If the given char is not in the alphabet, returns null 18 | * @param s The state from which to start. 19 | * @param a The letter to read 20 | * @return The successor of the transition starting in s reading a. 21 | */ 22 | public State getSuccessor(State s, char a){ 23 | for (Transition t : transitions){ 24 | if(t.getStart().equals(s) && t.getLabel()==a){ 25 | return t.getEnd(); 26 | } 27 | } 28 | return null; 29 | } 30 | 31 | 32 | /** 33 | * Checks whether the given parameters indeed describe a DFA. 34 | * Throws an exception describing the problem if some parameter is invalid. 35 | * Checks firstly that each state has a transition for each label at most once, after that at least once. 36 | * We do not need to check that the transition labels are a subset of the alphabet, that is done in checkValidEpsilonNFA 37 | */ 38 | private void checkValidDFA() throws IllegalArgumentException{ 39 | for (State s : states){ 40 | Set transLabels = new HashSet<>(); 41 | for (Transition t : getTransitionsFromState(s)){ 42 | if(!transLabels.add(t.getLabel())){ 43 | throw new IllegalArgumentException("State " + s + " has more than one transition for label " + t.getLabel()); 44 | } 45 | } 46 | if(!transLabels.containsAll(alphabet)){ 47 | Set missingSet = new HashSet<>(alphabet); 48 | missingSet.removeAll(transLabels); 49 | throw new IllegalArgumentException("State " + s + " is missing a transition for label(s): " + missingSet); 50 | } 51 | } 52 | } 53 | 54 | @Override 55 | public String toString(){ 56 | return "DFA\n" + toStringHelper(); 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /src/eti18/ha5/DFA.java: -------------------------------------------------------------------------------- 1 | package eti18.ha5; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class DFA extends NFA { 7 | 8 | public DFA(Set states, Set transitions, Set alphabet, State startState, Set finalStates) { 9 | super(states, transitions, alphabet, startState, finalStates); 10 | checkValidDFA(); 11 | } 12 | 13 | /** 14 | * Returns the successor of a state-letter pair. 15 | * It surely exists, because the transition relation was checked to be total in checkValidDFA. 16 | * There surely is only one, because that was also checked in checkValidDFA 17 | * If the given char is not in the alphabet, returns null 18 | * @param s The state from which to start. 19 | * @param a The letter to read 20 | * @return The successor of the transition starting in s reading a. 21 | */ 22 | public State getSuccessor(State s, char a){ 23 | for (Transition t : transitions){ 24 | if(t.getStart().equals(s) && t.getLabel()==a){ 25 | return t.getEnd(); 26 | } 27 | } 28 | return null; 29 | } 30 | 31 | 32 | /** 33 | * Checks whether the given parameters indeed describe a DFA. 34 | * Throws an exception describing the problem if some parameter is invalid. 35 | * Checks firstly that each state has a transition for each label at most once, after that at least once. 36 | * We do not need to check that the transition labels are a subset of the alphabet, that is done in checkValidEpsilonNFA 37 | */ 38 | private void checkValidDFA() throws IllegalArgumentException{ 39 | for (State s : states){ 40 | Set transLabels = new HashSet<>(); 41 | for (Transition t : getTransitionsFromState(s)){ 42 | if(!transLabels.add(t.getLabel())){ 43 | throw new IllegalArgumentException("State " + s + " has more than one transition for label " + t.getLabel()); 44 | } 45 | } 46 | if(!transLabels.containsAll(alphabet)){ 47 | Set missingSet = new HashSet<>(alphabet); 48 | missingSet.removeAll(transLabels); 49 | throw new IllegalArgumentException("State " + s + " is missing a transition for label(s): " + missingSet); 50 | } 51 | } 52 | } 53 | 54 | @Override 55 | public String toString(){ 56 | return "DFA\n" + toStringHelper(); 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /src/gad17/blatt09/AVLTreeNode.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt09; 2 | 3 | /** 4 | * Diese Klasse implementiert einen Knoten in einem AVL-Baum. 5 | */ 6 | public class AVLTreeNode { 7 | /** 8 | * Diese Variable enthält den Schlüssel, den der Knoten speichert. 9 | */ 10 | private int key; 11 | 12 | /** 13 | * Diese Variable speichert die Balancierung des Knotens - wie in der 14 | * Vorlesung erklärt - ab. Ein Wert von -1 bedeutet, dass der linke Teilbaum 15 | * um eins tiefer ist als der rechte Teilbaum. Ein Wert von 0 bedeutet, dass 16 | * die beiden Teilbäume gleich hoch sind. Ein Wert von 1 bedeutet, dass der 17 | * rechte Teilbaum tiefer ist. 18 | */ 19 | private int balance = 0; 20 | 21 | private AVLTreeNode left = null; 22 | private AVLTreeNode right = null; 23 | 24 | public AVLTreeNode(int key) { 25 | // TODO... 26 | } 27 | 28 | /** 29 | * Diese Methode ermittelt die Höhe des Teilbaums unter diesem Knoten. 30 | * 31 | * @return die ermittelte Höhe 32 | */ 33 | public int height() { 34 | // TODO... 35 | return -3; 36 | } 37 | 38 | public boolean validAVL() { 39 | // TODO... 40 | return false; 41 | } 42 | 43 | /** 44 | * Diese Methode sucht einen Schlüssel im Baum. 45 | * 46 | * @param key der zu suchende Schlüssel 47 | * @return 'true', falls der Schlüssel gefunden wurde, 'false' sonst 48 | */ 49 | public boolean find(int key) { 50 | // TODO... 51 | return false; 52 | } 53 | 54 | public AVLTreeNode insert(int key) { 55 | // TODO... 56 | return null; 57 | } 58 | 59 | /** 60 | * Diese Methode wandelt den Baum in das Graphviz-Format um. 61 | * 62 | * @param sb der StringBuilder für die Ausgabe 63 | */ 64 | public void dot(StringBuilder sb) { 65 | dotNode(sb, 0); 66 | } 67 | 68 | private int dotNode(StringBuilder sb, int idx) { 69 | sb.append(String.format("\t%d [label=\"%d, b=%d\"];\n", idx, key, balance)); 70 | int next = idx + 1; 71 | if (left != null) 72 | next = left.dotLink(sb, idx, next, "l"); 73 | if (right != null) 74 | next = right.dotLink(sb, idx, next, "r"); 75 | return next; 76 | } 77 | 78 | private int dotLink(StringBuilder sb, int idx, int next, String label) { 79 | sb.append(String.format("\t%d -> %d [label=\"%s\"];\n", idx, next, label)); 80 | return dotNode(sb, next); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/gad17/blatt04/Client.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | import java.io.ObjectInputStream; 5 | import java.io.ObjectOutputStream; 6 | import java.net.Socket; 7 | import java.util.Scanner; 8 | 9 | public class Client { 10 | 11 | public static void main(String[] args) throws IOException { 12 | // Configuration 13 | String masterHost = "127.0.0.1"; 14 | int masterClientPort = 5555; 15 | 16 | System.out.println("Examples: read key"); 17 | System.out.println(" store key 42"); 18 | System.out.println("Please type your request:"); 19 | 20 | Scanner scanner = new Scanner(System.in); 21 | String line = scanner.nextLine(); 22 | scanner.close(); 23 | String[] lineParts = line.split(" "); 24 | IRequest request = makeRequest(lineParts); //TODO inline after Testing! 25 | 26 | System.out.println("Sending: " + request); 27 | Socket master = null; 28 | master = new Socket(masterHost, masterClientPort); 29 | try { 30 | ObjectOutputStream out = new ObjectOutputStream(master.getOutputStream()); 31 | out.writeObject(request); 32 | out.flush(); 33 | 34 | ObjectInputStream in = new ObjectInputStream(master.getInputStream()); 35 | IResponse response = (IResponse) in.readObject(); 36 | ResponseVisitor rv = new ResponseVisitor(); 37 | rv.__((readResponse) -> { 38 | SerializableOptional result = readResponse.getValue(); 39 | if (result.isPresent()) 40 | System.out.println("Read response with value " + result.get() + "."); 41 | else 42 | System.out.println("Read response: Unknown key!"); 43 | }, (storeResponse) -> { 44 | System.out.println("Store successful!"); 45 | }); 46 | response.accept(rv); 47 | } catch (ClassNotFoundException e) { 48 | e.printStackTrace(); 49 | } finally { 50 | master.close(); 51 | } 52 | 53 | } 54 | 55 | public static IRequest makeRequest(String[] lineParts) { 56 | // ***************************************** 57 | // TODO: Werten Sie das Array lineParts aus und interpretieren die Eingabe entsprechend. 58 | // Eine passende Anfrage sollte danach in der Variable request gespeichert sein. 59 | // ***************************************** 60 | return null; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/pgdp16/blatt08/MatrixMultOptMemoizationTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt08; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.ArrayList; 9 | import java.util.Arrays; 10 | import java.util.Collection; 11 | 12 | @RunWith(Parameterized.class) 13 | public class MatrixMultOptMemoizationTest extends MatrixMultOptMemoization { 14 | private final int expected; 15 | private final int[][] input; 16 | private ArrayList fInputs; 17 | 18 | public MatrixMultOptMemoizationTest(int expected, int[][] input) { 19 | this.expected = expected; 20 | this.input = input; 21 | fInputs = new ArrayList<>(); 22 | } 23 | 24 | //TODO invalid Input 25 | @Parameterized.Parameters(name = "expected: {0}") 26 | public static Collection data() { 27 | return Arrays.asList(new Object[][]{ 28 | {0, new int[0][0]}, 29 | {0, new int[][]{{}}}, 30 | {0, new int[][]{{10, 30}}}, 31 | {0, new int[][]{{30, 5}}}, 32 | {0, new int[][]{{5, 60}}}, 33 | {1, new int[][]{{1, 1}, {1, 1}}}, 34 | {8, new int[][]{{2, 2}, {2, 2}}}, 35 | {4500, new int[][]{{10, 30}, {30, 5}, {5, 60}}}, 36 | {286110, new int[][]{{85, 42}, {42, 33}, {33, 60}}}, 37 | {500610, new int[][]{{85, 42}, {42, 33}, {33, 60}, {60, 80}}}, 38 | {13345101, new int[][]{{251, 99}, {99, 666}, {666, 147}}}, 39 | {798200504, new int[][]{{420, 69}, {69, 1337}, {1337, 9001}}}, 40 | {428, new int[][]{{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 10}, {10, 10}}}, 41 | {3325056, new int[][]{{198,116}, {116,322}, {322,74}, {74,492}, {492,257}, {257,227}, {227,491}, {491,7}, {7,294}}} 42 | }); 43 | } 44 | 45 | @Test 46 | public void f() throws Exception { 47 | Assert.assertEquals(expected, this.f(input)); 48 | } 49 | 50 | @Override 51 | public int f(int[][] mm, int i, int j) { 52 | Integer[] array = {i, j}; 53 | assertDuplicates(i, j); 54 | fInputs.add(array); 55 | return super.f(mm, i, j); 56 | } 57 | 58 | private void assertDuplicates(int i, int j) { 59 | for (Integer[] fInput : fInputs) { 60 | if (fInput[0] == i && fInput[1] == j) 61 | Assert.fail("i: " + i + " and j: " + j + 62 | " combined were already calculated "); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/grnvs18/assignment2/Assignment2.java: -------------------------------------------------------------------------------- 1 | package grnvs18.assignment2; 2 | 3 | /* DO NOT!!! put a package statement here, that would break the build system */ 4 | import java.net.InetAddress; 5 | import java.net.UnknownHostException; 6 | 7 | public class Assignment2 { 8 | 9 | /*====================================TODO===================================*/ 10 | /* Put your required state variables here */ 11 | static byte[] mymac; 12 | 13 | /*===========================================================================*/ 14 | 15 | public static void run(GRNVS_RAW_Mock sock, int frames) { 16 | byte[] recbuffer = new byte[1514]; 17 | int length; 18 | Timeout timeout = new Timeout(10000); 19 | 20 | /*====================================TODO===================================*/ 21 | /* If you want to set up any data/counters before the receive loop, 22 | * this is the right location 23 | */ 24 | mymac = sock.getMac(); 25 | 26 | /*===========================================================================*/ 27 | 28 | /* keep this AS IS! the tester uses this to avoid races */ 29 | System.out.format("I am ready!\n"); 30 | 31 | /*====================================TODO===================================*/ 32 | /* Update loop condition */ 33 | for (; true; ) { 34 | /*===========================================================================*/ 35 | length = sock.read(recbuffer, timeout); 36 | if (length == 0) { 37 | System.err.format("Timed out, this means there was nothing to receive. Do you have a sender set up?\n"); 38 | break; 39 | } 40 | /*====================================TODO===================================*/ 41 | /* This is the receive loop, 'recbuffer' will contain the received 42 | * frame. 'length' tells you the length of what you received. 43 | * Anything that should be done with every frame that's received 44 | * should be done here. 45 | */ 46 | 47 | /*===========================================================================*/ 48 | } 49 | /*====================================TODO===================================*/ 50 | /* Print your summary here */ 51 | 52 | /*===========================================================================*/ 53 | } 54 | 55 | 56 | public static void main(String[] argv) { 57 | Arguments args = new Arguments(argv); 58 | GRNVS_RAW sock = null; 59 | try{ 60 | sock = new GRNVS_RAW(args.iface, 3); 61 | run(sock, args.frames); 62 | sock.close(); 63 | } 64 | catch(Exception e) { 65 | e.printStackTrace(); 66 | System.out.println(e.getMessage()); 67 | System.exit(1); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/pgdp16/blatt12/rock/PlayerTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt12.rock; 2 | 3 | import org.junit.*; 4 | import pgdp16.blatt12.map.MapTest; 5 | 6 | import java.util.ArrayList; 7 | 8 | public class PlayerTest { 9 | 10 | private static final long WAIT_MS = 1000; 11 | ArrayList choices; 12 | private Player test; 13 | 14 | @Before 15 | public void setUp() throws Exception { 16 | choices = new ArrayList<>(); 17 | test = new Player() { 18 | @Override 19 | public void run() { 20 | MapTest.sleepFor(WAIT_MS); 21 | super.run(); 22 | } 23 | }; 24 | } 25 | 26 | @After 27 | public void tearDown() throws Exception { 28 | choices.clear(); 29 | } 30 | 31 | @Test 32 | public void run_generatesRandom_between0And2() throws Exception { 33 | new Thread(test).start(); 34 | assertChoiceRange(test.getChoice()); 35 | } 36 | 37 | private void assertChoiceRange(int choice) { 38 | Assert.assertTrue(choice <= 2 && choice >= 0); 39 | 40 | choices.add(choice); 41 | System.err.println("\nchoices: " + choices); 42 | } 43 | 44 | 45 | @Test(timeout = 10000) 46 | public void getChoice_called2Times() throws Exception { 47 | new Thread(test).start(); 48 | assertChoiceRange(test.getChoice()); 49 | assertChoiceRange(test.getChoice()); 50 | } 51 | 52 | 53 | @Test(timeout = 10000) 54 | @Ignore //optional 55 | public void interrupt_withoutGetChoice_givenRunning() throws Exception { 56 | Thread thread = arrangeNewGame(); 57 | 58 | assertInterrupt(thread); 59 | } 60 | 61 | private Thread arrangeNewGame() { 62 | Thread thread = new Thread(test); 63 | thread.start(); 64 | return thread; 65 | } 66 | 67 | private void assertInterrupt(Thread thread) throws InterruptedException { 68 | thread.interrupt(); 69 | thread.join(); 70 | 71 | Assert.assertFalse(thread.isAlive()); 72 | } 73 | 74 | @Test(timeout = 10000) 75 | public void interrupt_givenRunning() throws Exception { 76 | Thread thread = arrangeNewGame(); 77 | 78 | test.getChoice(); 79 | assertInterrupt(thread); 80 | } 81 | 82 | @Test(timeout = 10000) 83 | public void getChoice_beforeStart() throws Exception { 84 | new Thread(() -> { 85 | try { 86 | assertChoiceRange(test.getChoice()); 87 | } catch (InterruptedException e) { 88 | e.printStackTrace(); 89 | } 90 | }).start(); 91 | new Thread(test).start(); 92 | } 93 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt08/pgdp/BankAccountTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt08.pgdp; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | public class BankAccountTest { 8 | private static final int NUMBER = 007; 9 | private static final String FNAME = "James B."; 10 | private static final String SNAME = "Ond"; 11 | private BankAccount test; 12 | private String underTest; 13 | 14 | @Before 15 | public void setUp() throws Exception { 16 | test = new BankAccount(NUMBER, FNAME, SNAME); 17 | } 18 | 19 | @Test 20 | public void constructor() throws Exception { 21 | assertCent(0); 22 | } 23 | 24 | private void assertCent(int expected) { 25 | assertAccount(expected, NUMBER, FNAME, SNAME); 26 | } 27 | 28 | private void assertAccount(int cent, int number, String fname, String sname) { 29 | Money balance = test.getBalance(); 30 | Assert.assertEquals(messageFunction("getAccountnumber()"), number, test.getAccountnumber()); 31 | Assert.assertEquals(messageFunction("getFirstname()"), fname, test.getFirstname()); 32 | Assert.assertEquals(messageFunction("getSurname()"), sname, test.getSurname()); 33 | Assert.assertNotNull("getBalance() shall not return null!", balance); 34 | Assert.assertEquals(messageFunction(underTest != null ? underTest : "getCent()"), cent, balance.getCent()); 35 | } 36 | 37 | private String messageFunction(String function) { 38 | return "Check the Function: " + function; 39 | } 40 | 41 | @Test 42 | public void addMoney_givenEmpty_given10_thenCentIs10() throws Exception { 43 | assertAddMoney(10); 44 | } 45 | 46 | @Test 47 | public void addMoney_given10_given30_thenCentIs40() throws Exception { 48 | assertAddMoney(10, 30); 49 | } 50 | 51 | private void assertAddMoney(int... input) { 52 | underTest = "addMoney()"; 53 | int sum = 0; 54 | for (int i : input) { 55 | sum += i; 56 | test.addMoney(new Money(i)); 57 | } 58 | assertCent(sum); 59 | } 60 | 61 | @Test 62 | public void toString_returnsSomething() throws Exception { 63 | Assert.assertNotNull("toString() shall return something!",test.toString()); 64 | 65 | assertToStringContains(NUMBER); 66 | assertToStringContains(FNAME); 67 | assertToStringContains(SNAME); 68 | assertToStringContains(new Money(0)); 69 | } 70 | 71 | private void assertToStringContains(Object contains) { 72 | Assert.assertTrue("toString() shall contain: " + contains,test.toString().contains(contains +"")); 73 | } 74 | } -------------------------------------------------------------------------------- /src/gad17/blatt02/Interval.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt02; 2 | 3 | /** 4 | * Objekte der Klasse Interval stellen einen Bereich von Zahlen dar. 5 | */ 6 | abstract class Interval { 7 | /** 8 | * Abfragen der unteren, inklusiven Intervallgrenze 9 | * 10 | * @return die untere, inklusive Grenze 11 | */ 12 | public abstract int getFrom(); 13 | 14 | /** 15 | * Abfragen der oberen, inklusiven Intervallgrenze 16 | * 17 | * @return die obere, inklusive Grenze 18 | */ 19 | public abstract int getTo(); 20 | 21 | /** 22 | * Diese Methode erzeugt ein Intervall aus Array-Indices. Ist die 23 | * untere Intervallberenzung größer als die obere Intervallbegrenzung 24 | * oder ist einer der Begrenzungen negativ, so wird ein leeres Intervall 25 | * zurückgegeben. 26 | * 27 | * @param from die untere, inklusive Intervallbegrenzung 28 | * @param to die obere, inklusive Intervallbegrenzung 29 | * @return ein Intervallobjekt, das den Indexbereich repräsentiert 30 | */ 31 | static Interval fromArrayIndices(int from, int to) { 32 | if (to < from) 33 | return new EmptyInterval(); 34 | else if (to < 0 || from < 0) 35 | return new EmptyInterval(); 36 | else 37 | return new NonEmptyInterval(from, to); 38 | } 39 | } 40 | 41 | /** 42 | * Objekte der Klasse NonEmptyInterval repräsentieren ein nicht-leeres 43 | * Intervall. 44 | */ 45 | class NonEmptyInterval extends Interval { 46 | private int from; 47 | private int to; 48 | 49 | @Override 50 | public int getFrom() { 51 | return from; 52 | } 53 | 54 | @Override 55 | public int getTo() { 56 | return to; 57 | } 58 | 59 | public NonEmptyInterval(int from, int to) { 60 | if (to >= from) { 61 | this.from = from; 62 | this.to = to; 63 | } else 64 | throw new RuntimeException("Invalid interval boundary"); 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return "[" + from + ";" + to + "]"; 70 | } 71 | } 72 | 73 | /** 74 | * Objekte der Klasse EmptyInterval repräsentieren ein leeres Intervall. 75 | */ 76 | class EmptyInterval extends Interval { 77 | @Override 78 | public int getFrom() { 79 | throw new RuntimeException("No lower boundary in empty interval"); 80 | } 81 | 82 | @Override 83 | public int getTo() { 84 | throw new RuntimeException("No upper boundary in empty interval"); 85 | } 86 | 87 | public EmptyInterval() { 88 | } 89 | 90 | @Override 91 | public String toString() { 92 | return "[]"; 93 | } 94 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt07/MiniJava.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt07; 2 | 3 | import javax.swing.*; 4 | 5 | public class MiniJava { 6 | public /*static*/ String readString(String text) { 7 | JFrame frame = new JFrame(); 8 | String s = JOptionPane.showInputDialog(frame, text); 9 | frame.dispose(); 10 | 11 | if (s == null) 12 | System.exit(0); 13 | return s; 14 | } 15 | 16 | public /*static*/ String readString() { 17 | return readString("Eingabe:"); 18 | } 19 | 20 | public /*static*/ int readInt(String text) { 21 | JFrame frame = new JFrame(); 22 | String s = JOptionPane.showInputDialog(frame, text); 23 | frame.dispose(); 24 | 25 | int x = 0; 26 | if (s == null) 27 | System.exit(0); 28 | try { 29 | x = Integer.parseInt(s.trim()); 30 | } catch (NumberFormatException e) { 31 | x = readInt(text); 32 | } 33 | return x; 34 | } 35 | 36 | public /*static*/ int readInt() { 37 | return readInt("Geben Sie eine ganze Zahl ein:"); 38 | } 39 | 40 | public /*static*/ int read(String text) { 41 | return readInt(text); 42 | } 43 | 44 | public /*static*/ int read() { 45 | return readInt(); 46 | } 47 | 48 | public /*static*/ double readDouble(String text) { 49 | JFrame frame = new JFrame(); 50 | String s = JOptionPane.showInputDialog(frame, text); 51 | frame.dispose(); 52 | 53 | double x = 0; 54 | if (s == null) 55 | System.exit(0); 56 | try { 57 | x = Double.parseDouble(s.trim()); 58 | } catch (NumberFormatException e) { 59 | x = readDouble(text); 60 | } 61 | return x; 62 | } 63 | 64 | public /*static*/ double readDouble() { 65 | return readDouble("Geben Sie eine Zahl ein:"); 66 | } 67 | 68 | public /*static*/ void write(String output) { 69 | JFrame frame = new JFrame(); 70 | JOptionPane.showMessageDialog(frame, output, "Ausgabe", JOptionPane.PLAIN_MESSAGE); 71 | frame.dispose(); 72 | } 73 | 74 | public /*static*/ void write(int output) { 75 | write("" + output); 76 | } 77 | 78 | public /*static*/ void write(double output) { 79 | write("" + output); 80 | } 81 | 82 | public /*static*/ int drawCard(int minval, int maxval){ 83 | return minval + (new java.util.Random()).nextInt(maxval-minval+1); 84 | } 85 | public /*static*/ int drawCard(){ 86 | return drawCard(2,11); 87 | } 88 | 89 | public /*static*/ int dice(){ 90 | return drawCard(1,6); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/gad17/blatt02/BinSea.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt02; 2 | 3 | /** 4 | * Die Klasse BinSea stellt Methoden bereit, in sortierten Feldern binär 5 | * nach Wertebereichen zu suchen. 6 | */ 7 | class BinSea { 8 | /** 9 | * Diese Methode sucht nach einem Wert in einem einem sortierten Feld. Sie 10 | * soll dabei dazu verwendet werden können, den ersten bzw. letzten Index in 11 | * einem Intervall zu finden. Wird kein passender Index gefunden, wird 12 | * -1 zurückgegeben. 13 | * 14 | * Beispiel: 15 | * 16 | * 0 <-----------------------> 5 17 | * sortedData: [-10, 33, 50, 99, 123, 4242 ] 18 | * value: 80 ^ ^ 19 | * | | 20 | * | `- Ergebnis (3) für ersten Index im Intervall, da 99 21 | * | als erster Wert im Feld größer als 80 ist 22 | * | 23 | * `- Ergebnis (2) für letzten Index im Intervall, da 50 24 | * als letzter Wert kleiner als 80 ist 25 | * 26 | * @param sortedData das sortierte Feld, in dem gesucht wird 27 | * @param value der Wert der Intervallbegrenzung, der dem gesucht wird 28 | * @param lower true für untere Intervallbegrenzung, false für obere Intervallbegrenzung 29 | * @return der passende Index, -1 wenn dieser nicht gefunden werden kann 30 | */ 31 | private static int search (int[] sortedData, int value, boolean lower) { 32 | /** 33 | * Todo: Aufgabe a) 34 | */ 35 | return -2; 36 | } 37 | 38 | /** 39 | * Diese Methode sucht ein Intervall von Indices eines sortierten Feldes, deren Werte 40 | * in einem Wertebereich liegen. Es soll dabei binäre Suche verwendet werden. 41 | * 42 | * Beispiel: 43 | * 0 <-----------------------> 5 44 | * sortedData: [-10, 33, 50, 99, 123, 4242 ] 45 | * valueRange: [80;700] ^ ^ 46 | * | | 47 | * | `- Ergebnis (4) für obere Intervallbegrenzung, 48 | * | da 123 als letzter Wert kleiner oder gleich 700 ist 49 | * | 50 | * `- Ergebnis (3) für untere Intervallbegrenzung, 51 | * da 99 als erster Wert größer oder gleich 80 ist 52 | * 53 | * @param sortedData das sortierte Feld, in dem gesucht wird 54 | * @param valueRange der Wertebereich, zu dem Indices gesucht werden 55 | */ 56 | public static Interval search (int[] sortedData, Interval valueRange) { 57 | /** 58 | * Todo: Aufgabe b) 59 | */ 60 | return null; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/gad17/blatt03/DynamicStackTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt03; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import java.lang.reflect.Field; 8 | 9 | public class DynamicStackTest { 10 | DynamicStack test; 11 | 12 | @Before 13 | public void setUp() throws Exception { 14 | test = new DynamicStack(2, 4); 15 | } 16 | 17 | private void setLength(int l) throws Exception { 18 | Field length = test.getClass().getDeclaredField("length"); 19 | length.setAccessible(true); 20 | length.set(test, l); 21 | } 22 | 23 | private DynamicArray getArray() throws Exception { 24 | Field array = test.getClass().getDeclaredField("dynArr"); 25 | array.setAccessible(true); 26 | return (DynamicArray) array.get(test); 27 | } 28 | 29 | @Test(expected = Exception.class) 30 | public void popBack_givenInitialState_throwsException() throws Exception { 31 | test.popBack(); 32 | } 33 | 34 | @Test 35 | public void getLength_returns0() throws Exception { 36 | assertLength(0); 37 | } 38 | 39 | private void assertLength(int expected) { 40 | Assert.assertEquals(expected, test.getLength()); 41 | } 42 | 43 | @Test 44 | public void pushBack_givenInitialState_thenLengthIs1() throws Exception { 45 | test.pushBack(10); 46 | assertLength(1); 47 | } 48 | 49 | @Test 50 | public void popBack_givenLengthIs10_thenLengthIs9() throws Exception { 51 | push(1, 2, 3); 52 | test.popBack(); 53 | assertLength(2); 54 | } 55 | 56 | @Test 57 | public void pushBack_givenInitialState_addsToArray() throws Exception { 58 | test.pushBack(10); 59 | Assert.assertEquals(10, getArray().get(0)); 60 | } 61 | 62 | @Test 63 | public void pushBack_push3Times_addsToArray() throws Exception { 64 | push(10, 11, 12); 65 | assertPush(10, 11, 12); 66 | } 67 | 68 | private void push(int... ints) { 69 | for (int anInt : ints) test.pushBack(anInt); 70 | } 71 | 72 | private void assertPush(int... ints) throws Exception { 73 | Assert.assertEquals(3, test.getLength()); 74 | for (int i = 0; i < ints.length; i++) 75 | Assert.assertEquals(ints[i], getArray().get(i)); 76 | } 77 | 78 | @Test 79 | public void popBack_givenPushed12345_popAll() throws Exception { 80 | push(1, 2, 3, 4, 5); 81 | assertPop(5, 4, 3, 2, 1); 82 | } 83 | 84 | private void assertPop(int... ints) { 85 | for (int anInt : ints) 86 | Assert.assertEquals(anInt, test.popBack()); 87 | } 88 | 89 | @Test 90 | public void integration() throws Exception { 91 | //TODO 92 | 93 | } 94 | } -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/VegetarianTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.junit.runners.Parameterized; 8 | 9 | import java.util.Arrays; 10 | import java.util.Collection; 11 | 12 | 13 | @RunWith(Parameterized.class) 14 | public class VegetarianTest { 15 | public static String[] moves; 16 | private Vegetarian test; 17 | 18 | public VegetarianTest(String toTest, Vegetarian test, String[] moves) { 19 | this.test = test; 20 | VegetarianTest.moves = moves; 21 | } 22 | 23 | @Parameterized.Parameters(name = "{0}") 24 | public static Collection data() { 25 | 26 | return Arrays.asList(new Object[][]{ 27 | {"Elephant", new Elephant(false), new String[0]}, 28 | {"Horse", new Horse(false), new String[]{"f6", "e5"}}, 29 | {"Rabbit", new Rabbit(false), new String[0]}, 30 | }); 31 | } 32 | 33 | @Before 34 | public void setUp() throws Exception { 35 | test.position = new Position(); 36 | test.position.myAnimals = new Animal[]{test}; 37 | } 38 | 39 | @Test 40 | public void possibleMoves_givenSurrounded_givenH8() throws Exception { 41 | Move[] exp = buildKillerMove(); 42 | 43 | Move[] actual = test.possibleMoves(); 44 | 45 | assertMoves(exp, actual); 46 | } 47 | 48 | private Move[] buildKillerMove() { 49 | Predator predatorW_G8 = new Penguin(true); 50 | Vegetarian vegetarianW_G7 = new Vegetarian(true); 51 | Vegetarian vegetarianM_H7 = new Vegetarian(false); 52 | predatorW_G8.square = "g8"; 53 | vegetarianW_G7.square = "g7"; 54 | vegetarianM_H7.square = "h7"; 55 | 56 | test.position.myAnimals = new Animal[]{test, predatorW_G8, vegetarianW_G7, vegetarianM_H7}; 57 | return buildMoves("h8", moves); 58 | } 59 | 60 | private Move[] buildMoves(String start, String... targets) { 61 | test.square = start; 62 | Move[] out = new Move[targets.length]; 63 | for (int i = 0; i < out.length; i++) out[i] = new Move(start + targets[i]); 64 | return out; 65 | } 66 | 67 | 68 | private void assertMoves(Move[] exp, Move[] actual) { 69 | Assert.assertNotNull("possibleMoves() shall not return null!", actual); 70 | Assert.assertEquals("possibleMoves() shall have another length\n" 71 | + Arrays.toString(actual), exp.length, actual.length); 72 | for (Move move : exp) 73 | Assert.assertTrue("possibleMoves() does not contain: " 74 | + move + "\n" + Arrays.toString(actual), 75 | Arrays.asList(actual).contains(move)); 76 | } 77 | } -------------------------------------------------------------------------------- /src/gad17/blatt04/Store.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import java.io.IOException; 4 | import java.io.ObjectInputStream; 5 | import java.io.ObjectOutputStream; 6 | import java.net.Socket; 7 | import java.util.Map; 8 | 9 | public class Store { 10 | 11 | public static Map data; //TODO rename after Testing! 12 | 13 | public static void main(String[] args) throws IOException, ClassNotFoundException { 14 | // Configuration 15 | String masterHost = "127.0.0.1"; 16 | int masterStorePort = 5000; 17 | 18 | init(); 19 | 20 | Socket master = new Socket(masterHost, masterStorePort); 21 | try { 22 | while (true) { 23 | ObjectInputStream masterIn = new ObjectInputStream(master.getInputStream()); 24 | ObjectOutputStream masterOut = new ObjectOutputStream(master.getOutputStream()); 25 | IRequest request = (IRequest) masterIn.readObject(); 26 | System.out.println(request); 27 | RequestVisitor rv = new RequestVisitor(); 28 | Mutable response = new Mutable(); 29 | rv.__((ReadRequest readRequest) -> { 30 | ReadResponse readResponse = onReadRequest(readRequest); 31 | response.set(readResponse); 32 | 33 | }, (StoreRequest storeRequest) -> { 34 | onStoreRequest(storeRequest); 35 | response.set(new StoreResponse()); 36 | }); 37 | request.accept(rv); 38 | System.out.println(response.get()); 39 | masterOut.writeObject(response.get()); 40 | masterOut.flush(); 41 | } 42 | } catch (IOException e) { 43 | e.printStackTrace(); 44 | } finally { 45 | master.close(); 46 | } 47 | } 48 | 49 | public static void init() { 50 | // ***************************************** 51 | // TODO: Bereiten Sie eine Struktur vor, um die �bergebenen und angefragten Werte zu speichern. 52 | // ***************************************** 53 | } 54 | 55 | public static void onStoreRequest(StoreRequest storeRequest) { 56 | // ***************************************** 57 | // TODO: Speichern Sie den �bergebenen Wert. 58 | // ***************************************** 59 | } 60 | 61 | public static ReadResponse onReadRequest(ReadRequest readRequest) { 62 | // ***************************************** 63 | // TODO: Lesen Sie den angefragten Wert aus (sofern vorhanden!), 64 | // bereiten Sie eine passende ReadResponse zur Anfrage vor, 65 | // und �bergeben Sie sie an das response Objekt. 66 | // ***************************************** 67 | return null; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/gad17/blatt04/ClientTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt04; 2 | 3 | import junit.framework.Assert; 4 | import org.junit.Test; 5 | 6 | public class ClientTest { 7 | 8 | private IRequest makeRequest(String[] lineParts) { 9 | return Client.makeRequest(lineParts); 10 | } 11 | 12 | @Test(expected = RuntimeException.class) 13 | public void buildRequest_inputNull_throwsException() throws Exception { 14 | makeRequest(null); 15 | } 16 | 17 | @Test(expected = Exception.class) 18 | public void buildRequest_inputEmptyArray_throwsException() throws Exception { 19 | makeRequest(new String[0]); 20 | } 21 | 22 | @Test(expected = Exception.class) 23 | public void buildRequest_inputLenght1_throwsException() throws Exception { 24 | makeRequest(new String[1]); 25 | } 26 | 27 | @Test(expected = Exception.class) 28 | public void buildRequest_inputReadLengthToBig_throwsException() throws Exception { 29 | makeRequest(new String[]{"read", "123", "1"}); 30 | } 31 | 32 | @Test(expected = Exception.class) 33 | public void buildRequest_inputStoreLengthToBig_throwsException() throws Exception { 34 | makeRequest(new String[]{"store", "123", "1", "Uebung"}); 35 | } 36 | 37 | @Test(expected = Exception.class) 38 | public void buildRequest_inputReadEmptyKey_throwsException() throws Exception { 39 | makeRequest(new String[]{"read", ""}); 40 | } 41 | 42 | @Test(expected = Exception.class) 43 | public void buildRequest_inputStoreEmptyKey_throwsException() throws Exception { 44 | makeRequest(new String[]{"store", "", "4"}); 45 | } 46 | 47 | @Test(expected = Exception.class) 48 | public void buildRequest_inputReadNoKey_throwsException() throws Exception { 49 | makeRequest(new String[]{"read"}); 50 | } 51 | 52 | @Test(expected = Exception.class) 53 | public void buildRequest_inputStoreNoKey_throwsException() throws Exception { 54 | makeRequest(new String[]{"store", "4"}); 55 | } 56 | 57 | @Test(expected = Exception.class) 58 | public void buildRequest_inputStoreEmptyValue_throwsException() throws Exception { 59 | makeRequest(new String[]{"store", "123", ""}); 60 | } 61 | 62 | @Test 63 | public void buildRequest_inputReadValid_returnsReadRequest() throws Exception { 64 | IRequest actual = makeRequest(new String[]{"read", "1"}); 65 | Assert.assertNotNull(actual); 66 | Assert.assertTrue(actual instanceof ReadRequest); 67 | Assert.assertEquals("1", actual.getKey()); 68 | } 69 | 70 | @Test 71 | public void buildRequest_inputStoreValid_returnsStoreRequest() throws Exception { 72 | IRequest actual = makeRequest(new String[]{"store", "123", "1"}); 73 | Assert.assertNotNull(actual); 74 | Assert.assertTrue(actual instanceof StoreRequest); 75 | Assert.assertEquals("123", actual.getKey()); 76 | Assert.assertEquals(1, ((StoreRequest) actual).getValue()); 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/MainTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import java.util.ArrayList; 8 | 9 | public class MainTest { 10 | 11 | private ArrayList startGameInput; 12 | private ArrayList readStringOutput; 13 | private ArrayList readIntOutput; 14 | 15 | @Before 16 | public void setUp() throws Exception { 17 | startGameInput = new ArrayList<>(); 18 | readStringOutput = new ArrayList<>(); 19 | readIntOutput = new ArrayList<>(); 20 | 21 | Main.game = new Game() { 22 | @Override 23 | public void startGame(boolean ladiesFirst) { 24 | startGameInput.add(ladiesFirst); 25 | } 26 | }; 27 | Main.io = new IO() { 28 | @Override 29 | public String readString(String msg) { 30 | return readStringOutput.remove(0); 31 | } 32 | 33 | @Override 34 | public int readInt(String msg, int low, int high) { 35 | return readIntOutput.remove(0); 36 | } 37 | 38 | @Override 39 | public int readInt(String msg) { 40 | return readIntOutput.remove(0); 41 | } 42 | 43 | @Override 44 | public int readInt() { 45 | return readIntOutput.remove(0); 46 | } 47 | }; 48 | } 49 | 50 | @Test 51 | public void main_givenTrue_thenLadiesFirst() throws Exception { 52 | readStringOutput.add("true"); 53 | readIntOutput.add(1); 54 | 55 | Main.main(null); 56 | 57 | Assert.assertEquals(1, startGameInput.size()); 58 | Assert.assertEquals(true, startGameInput.get(0)); 59 | } 60 | 61 | 62 | @Test 63 | public void main_givenFalse_thenNotLadiesFirst() throws Exception { 64 | readStringOutput.add("false"); 65 | readIntOutput.add(0); 66 | 67 | Main.main(null); 68 | 69 | Assert.assertEquals(1, startGameInput.size()); 70 | Assert.assertEquals(false, startGameInput.get(0)); 71 | } 72 | 73 | @Test 74 | public void main_givenInvalid_givenFalse_thenAskAgain() throws Exception { 75 | readStringOutput.add("no"); 76 | readIntOutput.add(-1); 77 | readStringOutput.add("false"); 78 | readIntOutput.add(0); 79 | 80 | Main.main(null); 81 | 82 | Assert.assertEquals(1, startGameInput.size()); 83 | Assert.assertEquals(false, startGameInput.get(0)); 84 | } 85 | 86 | @Test 87 | public void main_givenInvalid_givenTrue_thenAskAgain() throws Exception { 88 | readStringOutput.add("yes"); 89 | readIntOutput.add(2); 90 | readStringOutput.add("true"); 91 | readIntOutput.add(1); 92 | 93 | Main.main(null); 94 | 95 | Assert.assertEquals(1, startGameInput.size()); 96 | Assert.assertEquals(true, startGameInput.get(0)); 97 | } 98 | 99 | 100 | } -------------------------------------------------------------------------------- /src/grnvs18/assignment2/GRNVS_RAW.java: -------------------------------------------------------------------------------- 1 | package grnvs18.assignment2; 2 | 3 | public class GRNVS_RAW extends GRNVS_RAW_Mock{ 4 | static { 5 | System.loadLibrary("GRNVS"); 6 | } 7 | 8 | private final int handle; 9 | 10 | private final native int getSocket(String dev, int level); 11 | private final native int write_(int handle, byte[] buffer, int size); 12 | private final native int read_(int handle, byte[] buffer, Timeout time); 13 | private final native int close_(int handle); 14 | private final native byte[] mac_(int handle); 15 | private final native byte[] ip_(int handle); 16 | private final native byte[] ip6_(int handle); 17 | 18 | private static final native void hexdump_(byte[] buffer, int size); 19 | private static final native byte[] checksum_(byte[] hdr, int hdrOffset, 20 | byte[] payload, int payloadOffset, int payloadLength); 21 | private static final int SOCK_RAW = 3; 22 | private static final int SOCK_DGRAM = 2; 23 | 24 | public GRNVS_RAW(String dev, int level) { 25 | handle = getSocket(dev, level); 26 | } 27 | 28 | /* Write to the network device 29 | * @param buffer A bytebuffer containing the frame/packet 30 | * @size the first size byte will be sent 31 | * 32 | * @result The number of bytes sent 33 | */ 34 | public final int write(byte[] buffer, int size) { 35 | return write_(handle, buffer, size); 36 | } 37 | 38 | /* Read from the network device 39 | * @param buffer The buffer to read into 40 | * @param Timeout The timeout that should be used for the read 41 | * it will be updated 42 | * 43 | * @result The number of bytes sent 44 | */ 45 | public final int read(byte[] buffer, Timeout time) { 46 | return read_(handle, buffer, time); 47 | } 48 | 49 | /* Get the ip address of the device for this socket */ 50 | public final byte[] getIP() { 51 | return ip_(handle); 52 | } 53 | 54 | /* Get an ipv6 address of the device for this socket */ 55 | public final byte[] getIPv6() { 56 | return ip6_(handle); 57 | } 58 | 59 | /* Get the mac address of the device for this socket */ 60 | public final byte[] getMac() { 61 | return mac_(handle); 62 | } 63 | 64 | public final int close() { 65 | return close_(handle); 66 | } 67 | 68 | /* Dump a hex representation of the buffers content to stderr */ 69 | public static final void hexdump(byte[] buffer, int size) { 70 | GRNVS_RAW.hexdump_(buffer, size); 71 | } 72 | 73 | /* Compute the icmpv6 checksum for the packet. 74 | * @param hdr The buffer containing the ipv6 header 75 | * @param hdrOffset the offset of the header in the buffer 76 | * @param payload The buffer containing the icmp data (ip payload) 77 | * @param payloadOffset the offset of the payload in the buffer 78 | * @param payloadLength The length of the ip payload 79 | * 80 | * @return A byte array containing the checksum, in the same order it 81 | * should be in the output buffer 82 | */ 83 | public static final byte[] checksum(byte[] hdr, int hdrOffset, 84 | byte[] payload, int payloadOffset, int payloadLength) { 85 | return GRNVS_RAW.checksum_(hdr, hdrOffset, payload, 86 | payloadOffset, payloadLength); 87 | } 88 | 89 | protected void finalize() { 90 | close_(handle); 91 | } 92 | 93 | 94 | 95 | } 96 | -------------------------------------------------------------------------------- /src/pgdp16/blatt07/DameSpiel.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt07; 2 | 3 | public class DameSpiel extends MiniJava { 4 | 5 | /*private*/ int nrRows, nrColumns; // Board dimensions 6 | /*private*/ boolean[][] board; // true = queen, false = empty 7 | /*private*/ boolean whiteToMove; // Whose turn it is 8 | /*private*/ String white, black; // Players' names 9 | 10 | //TODO Mehr Attribute? 11 | 12 | //TODO Weitere Methoden? 13 | 14 | 15 | /** 16 | * Der Konstruktor registriert Spielernamen fuer Weiss und Schwarz. 17 | * 18 | * @param white Name des als 'Weiss' bezeichneten Spielers 19 | * @param black Name des als 'Schwarz' bezeichneten Spielers 20 | */ 21 | public DameSpiel(String white, String black){ 22 | //TODO 23 | } 24 | 25 | 26 | /** 27 | * Gibt das Spielbrett aus. 28 | */ 29 | /*private*/ void printBoard(){ 30 | for (int j = board[0].length - 1; j >= 0; j--) { 31 | System.out.print("\n " + (1 + j)); 32 | for (int i = 0; i < board.length; i++) { 33 | System.out.print(board[i][j] ? " X" : " -"); 34 | } 35 | } 36 | System.out.print("\n "); 37 | for (int i = 1; i <= board.length; i++) { 38 | System.out.print(" " + i); 39 | } 40 | System.out.println("\n" + (whiteToMove ? white : black) + " ist am Zug."); 41 | } 42 | 43 | 44 | /** 45 | * Initialisiert das Spielbrett ueberall mit false. 46 | * Dazu wird (ggf. neuer) Speicher allokiert. 47 | */ 48 | /*private*/ void initBoard(){ 49 | //TODO 50 | } 51 | 52 | 53 | /** 54 | * Ermittelt die Groesse des Spielbretts gemaess den Spielregeln. 55 | * Das Ergebnis der Abfrage wird in den Attributen nrRows und nrColumns abgelegt. 56 | */ 57 | /*private*/ void determineBoardSize(){ 58 | //TODO 59 | } 60 | 61 | 62 | /** 63 | * Ermittelt, wer anfaengt zu ziehen. 64 | * Das Ergebnis der Abfrage wird im Attribut whiteToMove abgelegt. 65 | */ 66 | /*private*/ void determineFirstPlayer(){ 67 | //TODO 68 | } 69 | 70 | 71 | /** 72 | * Fuehrt den Zug aus. 73 | * 74 | * @param move der auszufuehrende Zug! 75 | */ 76 | /*private*/ void applyMove(int move){ 77 | //TODO 78 | } 79 | 80 | 81 | /** 82 | * Startet die Hauptschleife des Spiels 83 | * mit der Abfrage nach Zuegen. 84 | * Die Schleife wird durch Eingabe von -1 beendet. 85 | */ 86 | /*private*/ void mainLoop(){ 87 | //TODO 88 | } 89 | 90 | 91 | /** 92 | * Informiert die Benutzerin ueber den Ausgang des Spiels. 93 | * Speziell: Wer hat gewonnen (Weiss oder Schwarz)? 94 | */ 95 | /*private*/ void reportWinner(){ 96 | //TODO 97 | } 98 | 99 | 100 | /** 101 | * Startet das Spiel. 102 | */ 103 | public void startGame(){ 104 | determineBoardSize(); 105 | initBoard(); 106 | determineFirstPlayer(); 107 | printBoard(); 108 | mainLoop(); 109 | reportWinner(); 110 | } 111 | 112 | 113 | public static void main(String[] args) { 114 | DameSpiel ds = new DameSpiel("Weiß", "Schwarz"); 115 | ds.startGame(); 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /src/gad17/blatt03/DynamicaTests.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt03; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.function.Consumer; 6 | import java.util.function.Function; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | 10 | public class DynamicaTests { 11 | private void testQueue(Consumer enqueue, Function dequeue, Function size) { 12 | int enqueueCount = 55; 13 | int dequeueCount = 55; 14 | 15 | // System.out.println("Up..."); 16 | enqueue.accept(enqueueCount++); 17 | enqueue.accept(enqueueCount++); 18 | enqueue.accept(enqueueCount++); 19 | enqueue.accept(enqueueCount++); 20 | enqueue.accept(enqueueCount++); 21 | enqueue.accept(enqueueCount++); 22 | enqueue.accept(enqueueCount++); 23 | enqueue.accept(enqueueCount++); 24 | enqueue.accept(enqueueCount++); 25 | 26 | // System.out.println("Down..."); 27 | for (int i = 0; i < 6; i++) 28 | assertEquals(dequeueCount++, (int) dequeue.apply(0)); 29 | 30 | // System.out.println("Up..."); 31 | enqueue.accept(enqueueCount++); 32 | enqueue.accept(enqueueCount++); 33 | enqueue.accept(enqueueCount++); 34 | enqueue.accept(enqueueCount++); 35 | 36 | // System.out.println("Down..."); 37 | for (int i = 0; i < 4; i++) 38 | assertEquals(dequeueCount++, (int) dequeue.apply(0)); 39 | 40 | // System.out.println("Up..."); 41 | enqueue.accept(enqueueCount++); 42 | enqueue.accept(enqueueCount++); 43 | enqueue.accept(enqueueCount++); 44 | enqueue.accept(enqueueCount++); 45 | enqueue.accept(enqueueCount++); 46 | enqueue.accept(enqueueCount++); 47 | enqueue.accept(enqueueCount++); 48 | enqueue.accept(enqueueCount++); 49 | enqueue.accept(enqueueCount++); 50 | enqueue.accept(enqueueCount++); 51 | enqueue.accept(enqueueCount++); 52 | enqueue.accept(enqueueCount++); 53 | 54 | // System.out.println("Down..."); 55 | while (size.apply(0) > 0) 56 | assertEquals(dequeueCount++, (int) dequeue.apply(0)); 57 | 58 | // System.out.println("Up..."); 59 | for (int i = 0; i < 1024; i++) 60 | enqueue.accept(enqueueCount++); 61 | // System.out.println("Down..."); 62 | for (int i = 0; i < 1024; i++) 63 | assertEquals(dequeueCount++, (int) dequeue.apply(0)); 64 | } 65 | 66 | @Test 67 | public void testRingQueue() { 68 | RingQueue rq = new RingQueue(2, 4); 69 | testQueue(x -> rq.enqueue(x), __ -> rq.dequeue(), __ -> rq.getSize()); 70 | } 71 | 72 | @Test 73 | public void testStackyQueue() { 74 | StackyQueue sq = new StackyQueue(2, 4); 75 | testQueue(x -> sq.enqueue(x), __ -> sq.dequeue(), __ -> sq.getLength()); 76 | } 77 | 78 | @Test 79 | public void testDynamicStack() { 80 | DynamicStack stack = new DynamicStack(2, 4); 81 | 82 | int count = 0; 83 | for (int i = 0; i < 20; i++) 84 | stack.pushBack(count++); 85 | 86 | for (int i = 0; i < 10; i++) 87 | assertEquals(--count, stack.popBack()); 88 | 89 | for (int i = 0; i < 30; i++) 90 | stack.pushBack(count++); 91 | 92 | for (int i = 0; i < 30 + 10; i++) 93 | assertEquals(--count, stack.popBack()); 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/pgdp16/blatt11/exception/PasswordTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt11.exception; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | 11 | @RunWith(Parameterized.class) 12 | public class PasswordTest extends Password { 13 | private static final int RANGE = 10; 14 | private static int nrUpperShould; 15 | private static int nrLowerShould; 16 | private static int nrSpecialShould; 17 | private static int nrNumbersShould; 18 | private static int lengthShould; 19 | private final String pwd; 20 | private final Exception expected; 21 | 22 | public PasswordTest(String pwd, Exception expected, int nrUpperShould, int nrLowerShould, int nrSpecialShould, 23 | int nrNumbersShould, int lengthShould, char[] illegalChars) { 24 | super(nrUpperShould, nrLowerShould, nrSpecialShould, 25 | nrNumbersShould, lengthShould, illegalChars); 26 | this.pwd = pwd; 27 | this.expected = expected; 28 | } 29 | 30 | @Parameterized.Parameters(name = "{1}") 31 | public static Collection data() { 32 | nrUpperShould = randomInt(RANGE); 33 | nrLowerShould = randomInt(RANGE); 34 | nrSpecialShould = randomInt(RANGE); 35 | nrNumbersShould = randomInt(RANGE); 36 | lengthShould = nrUpperShould + nrLowerShould + nrSpecialShould + nrNumbersShould; 37 | return Arrays.asList(new Object[][]{ 38 | {"", null, 0, 0, 0, 0, 0, new char[]{}}, 39 | {"", new NotEnoughUpper(1, 0), 1, 0, 0, 0, 0, new char[]{}}, 40 | {"", new NotEnoughLower(1, 0), 0, 1, 0, 0, 0, new char[]{}}, 41 | {"", new NotEnoughSpecial(1, 0), 0, 0, 1, 0, 0, new char[]{}}, 42 | {"", new NotEnoughNumber(1, 0), 0, 0, 0, 1, 0, new char[]{}}, 43 | {"", new NotLongEnoughExc(1, 0), 0, 0, 0, 0, 1, new char[]{}}, 44 | {"\n", new IllegalCharExc('\n'), 0, 0, 0, 0, 0, new char[]{'\n'}}, 45 | 46 | {"A", new NotEnoughUpper(2, 1), 2, 0, 0, 0, 0, new char[]{}}, 47 | {"!", new NotEnoughSpecial(3, 1), 0, 0, 3, 0, 0, new char[]{}}, 48 | {null, null, nrUpperShould, nrLowerShould, nrSpecialShould, nrNumbersShould, lengthShould, new char[]{}}, 49 | }); 50 | } 51 | 52 | private static int randomInt(int range) { 53 | return (char) (Math.random() * range); 54 | } 55 | 56 | private String buildPassword() { 57 | if (pwd != null) return pwd; 58 | return add(nrLowerShould, "l") + add(nrNumbersShould, "9") 59 | + add(nrSpecialShould, "*") + add(nrUpperShould, "U"); 60 | } 61 | 62 | private String add(int times, String add) { 63 | String out = ""; 64 | for (int i = times; i > 0; i--) 65 | out += add; 66 | return out; 67 | } 68 | 69 | @Test 70 | public void checkFormatTest() throws Exception { 71 | String actual = null + ""; 72 | try { 73 | String pwd = buildPassword(); 74 | System.out.println("PW: " + pwd); 75 | checkFormat(pwd); 76 | } catch (Exception e) { 77 | actual = e.toString(); 78 | } 79 | Assert.assertEquals(expected + "", actual); 80 | } 81 | 82 | 83 | } -------------------------------------------------------------------------------- /src/gad17/blatt05/DoubleHashTable.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | import java.util.Optional; 4 | 5 | /** 6 | * Die Klasse DoubleHashTable implementiert eine Hashtabelle, die doppeltes 7 | * Hashing verwendet. 8 | * 9 | * @param der Typ der Schlüssel, die in der Hashtabelle gespeichert werden 10 | * @param der Typ der Werte, die in der Hashtabelle gespeichert werden 11 | */ 12 | public class DoubleHashTable { 13 | /* 14 | * Todo 15 | */ 16 | private Pair[] hashTable; //TODO Rename after Testing 17 | 18 | /** 19 | * Diese Methode implementiert h(x, i). 20 | * 21 | * @param key der Schlüssel, der gehasht werden soll 22 | * @param i der Index, der angibt, der wievielte Hash für den gegebenen Schlüssel 23 | * berechnet werden soll 24 | * @return der generierte Hash 25 | */ 26 | private int hash(K key, int i) { 27 | /* 28 | * Todo 29 | */ 30 | return -2; 31 | } 32 | 33 | /** 34 | * Dieser Konstruktor initialisiert die Hashtabelle. 35 | * 36 | * @param primeSize die Größe 'm' der Hashtabelle; es kann davon ausgegangen 37 | * werden, dass es sich um eine Primzahl handelt. 38 | * @param hashableFactory Fabrik, die aus einer Größe ein DoubleHashable-Objekt erzeugt. 39 | */ 40 | public DoubleHashTable(int primeSize, HashableFactory hashableFactory) { 41 | /* 42 | * Todo 43 | */ 44 | } 45 | 46 | /** 47 | * Diese Methode fügt entsprechend des doppelten Hashens ein Element 48 | * in die Hashtabelle ein. 49 | * 50 | * @param k der Schlüssel des Elements, das eingefügt wird 51 | * @param v der Wert des Elements, das eingefügt wird 52 | * @return 'true' falls das einfügen erfolgreich war, 'false' falls die 53 | * Hashtabelle voll ist. 54 | */ 55 | public boolean insert(K k, V v) { 56 | /* 57 | * Todo 58 | */ 59 | return false; 60 | } 61 | 62 | /** 63 | * Diese Methode sucht ein Element anhand seines Schlüssels in der Hashtabelle 64 | * und gibt den zugehörigen Wert zurück, falls der Schlüssel gefunden wurde. 65 | * 66 | * @param k der Schlüssel des Elements, nach dem gesucht wird 67 | * @return der Wert des zugehörigen Elements, sonfern es gefunden wurde 68 | */ 69 | public Optional find(K k) { 70 | /* 71 | * Todo 72 | */ 73 | return null; 74 | } 75 | 76 | /** 77 | * Diese Methode ermittelt die Anzahl der Kollisionen, also die Anzahl 78 | * der Elemente, nicht an der 'optimalen' Position in die Hashtabelle eingefügt 79 | * werden konnten. Die optimale Position ist diejenige Position, die der 80 | * erste Aufruf der Hashfunktion (i = 0) bestimmt. 81 | * 82 | * @return die Anzahl der Kollisionen 83 | */ 84 | public int collisions() { 85 | /* 86 | * Todo 87 | */ 88 | return -2; 89 | } 90 | 91 | /** 92 | * Diese Methode berechnet die maximale Anzahl von Aufrufen der Hashfunktion, 93 | * die nötig waren, um ein einziges Element in die Hashtabelle einzufügen. 94 | * 95 | * @return die berechnete Maximalzahl von Aufrufen 96 | */ 97 | public int maxRehashes() { 98 | /* 99 | * Todo 100 | */ 101 | return -2; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/gad17/blatt03/Interval.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt03; 2 | 3 | /** 4 | * Objekte der Klasse Interval stellen einen Bereich von Zahlen dar. Die 5 | * Intervallgrenzen sind dabei jeweils eingeschlossen. 6 | */ 7 | abstract class Interval { 8 | /** 9 | * Abfragen der unteren Intervallgrenze 10 | * 11 | * @return die untere Grenze 12 | */ 13 | public abstract int getFrom(); 14 | 15 | /** 16 | * Abfragen der oberen Intervallgrenze 17 | * 18 | * @return die obere Grenze 19 | */ 20 | public abstract int getTo(); 21 | 22 | /** 23 | * Diese Methode gibt die Größe des Intervalls zurück. Zu 24 | * beachten ist dabei, dass falls getFrom() > getTo() gilt, 25 | * das Intervall die Elemente in den Teilintervallen 26 | * [usage.getFrom(); baseSize - 1] und [0; usage.getTo()] abdeckt. 27 | * 28 | * @param baseSize die Basisgröße, anhand derer ein Umbruch 29 | * innerhalb des Intervalls festgestellt wird 30 | * @return die Menge an abgedeckten Elementen 31 | */ 32 | public abstract int getSize(int baseSize); 33 | 34 | /** 35 | * Gibt zurück, ob das Intervall leer ist. 36 | * 37 | * @return 'true' falls das Intervall leer ist, 'false' sonst 38 | */ 39 | public abstract boolean isEmpty(); 40 | } 41 | 42 | /** 43 | * Objekte der Klasse NonEmptyInterval repräsentieren ein nicht-leeres 44 | * Intervall. 45 | */ 46 | class NonEmptyInterval extends Interval { 47 | private int from; 48 | 49 | @Override 50 | public int getFrom() { 51 | return from; 52 | } 53 | 54 | private int to; 55 | 56 | @Override 57 | public int getTo() { 58 | return to; 59 | } 60 | 61 | public NonEmptyInterval(int from, int to) { 62 | if (from < 0 || to < 0) 63 | throw new RuntimeException("Negative interval boundaries are invalid!"); 64 | this.from = from; 65 | this.to = to; 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return "[" + from + ";" + to + "]"; 71 | } 72 | 73 | @Override 74 | public int getSize(int baseSize) { 75 | if (to >= baseSize || from >= baseSize) 76 | throw new RuntimeException("Invalid interval for this base size!"); 77 | /* 78 | * Todo 79 | */ 80 | return -2; 81 | } 82 | 83 | @Override 84 | public boolean isEmpty() { 85 | /* 86 | * Todo 87 | */ 88 | return false; 89 | } 90 | } 91 | 92 | /** 93 | * Objekte der Klasse EmptyInterval repräsentieren ein leeres Intervall. 94 | */ 95 | class EmptyInterval extends Interval { 96 | @Override 97 | public int getFrom() { 98 | throw new RuntimeException("No lower boundary in empty interval"); 99 | } 100 | 101 | @Override 102 | public int getTo() { 103 | throw new RuntimeException("No upper boundary in empty interval"); 104 | } 105 | 106 | public EmptyInterval() { 107 | } 108 | 109 | @Override 110 | public String toString() { 111 | return "[]"; 112 | } 113 | 114 | @Override 115 | public int getSize(int baseSize) { 116 | /* 117 | * Todo 118 | */ 119 | return -2; 120 | } 121 | 122 | @Override 123 | public boolean isEmpty() { 124 | /* 125 | * Todo 126 | */ 127 | return false; 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/gad17/blatt06/SortTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt06; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Parameterized; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collection; 10 | import java.util.stream.LongStream; 11 | 12 | @RunWith(Parameterized.class) 13 | public class SortTest { 14 | private static final long RANGE = 10000; 15 | private SortingBase test; 16 | 17 | public SortTest(String name, SortingBase test) { 18 | this.test = test; 19 | } 20 | 21 | @Parameterized.Parameters(name = "{0}") 22 | public static Collection data() { 23 | return Arrays.asList(new Object[][]{ 24 | {"MergeSort", new Mergesort()}, 25 | {"QuickSort", new Quicksort()}, 26 | }); 27 | } 28 | 29 | @Test(expected = Exception.class) 30 | public void sort_inputNull_throwsException() throws Exception { 31 | test.sort(null); 32 | } 33 | 34 | private void assertSort(int[] input, int[] expected) { 35 | test.sort(input); 36 | Assert.assertArrayEquals(expected, input); 37 | } 38 | 39 | @Test 40 | public void sort_inputEmptyArray_thenNothingHappens() throws Exception { 41 | assertSort(new int[]{}, new int[]{}); 42 | } 43 | 44 | @Test 45 | public void sort_input1_thenNothingHappens() throws Exception { 46 | assertSort(new int[]{1}, new int[]{1}); 47 | } 48 | 49 | @Test 50 | public void sort_input21_thenArrayIs12() throws Exception { 51 | assertSort(new int[]{2, 1}, new int[]{1, 2}); 52 | } 53 | 54 | @Test 55 | public void sort_input132_thenArrayIs123() throws Exception { 56 | assertSort(new int[]{1, 3, 2}, new int[]{1, 2, 3}); 57 | } 58 | 59 | @Test 60 | public void sort_input312_thenArrayIs123() throws Exception { 61 | assertSort(new int[]{3, 1, 2}, new int[]{1, 2, 3}); 62 | } 63 | 64 | @Test 65 | public void sort_input2143_thenArrayIs1234() throws Exception { 66 | assertSort(new int[]{2, 1, 4, 3}, new int[]{1, 2, 3, 4}); 67 | } 68 | 69 | @Test 70 | public void sort_input21435_thenArrayIs12345() throws Exception { 71 | assertSort(new int[]{2, 1, 4, 3, 5}, new int[]{1, 2, 3, 4, 5}); 72 | } 73 | 74 | @Test 75 | public void sort_input54321_thenArrayIs12345() throws Exception { 76 | assertSort(new int[]{5, 4, 3, 2, 1}, new int[]{1, 2, 3, 4, 5}); 77 | } 78 | 79 | @Test 80 | public void sort_inputWithNegative_thenSortArray() throws Exception { 81 | assertSort(new int[]{-5, 4, -3, 2, -1}, new int[]{-5, -3, -1, 2, 4}); 82 | } 83 | 84 | @Test 85 | public void sort_inputRandom() throws Exception { 86 | int[] input = LongStream.range(0, RANGE).mapToInt(l 87 | -> (int) (Math.random() * Integer.MAX_VALUE)).toArray(); 88 | int[] expected = Arrays.copyOf(input, input.length); 89 | Arrays.sort(expected); 90 | assertSort(input, expected); 91 | } 92 | 93 | @Test 94 | public void sort_inputSorted() throws Exception { 95 | int[] input = LongStream.range(0, RANGE).mapToInt(l 96 | -> (int) (l)).toArray(); 97 | assertSort(input, input); 98 | } 99 | 100 | @Test 101 | public void sort_inputReverse() throws Exception { 102 | int[] input = LongStream.range(0, RANGE).mapToInt(l 103 | -> (int) (Integer.MAX_VALUE - l)).toArray(); 104 | int[] expected = Arrays.copyOf(input, input.length); 105 | Arrays.sort(expected); 106 | assertSort(input, expected); 107 | } 108 | } -------------------------------------------------------------------------------- /src/gad17/blatt05/DoubleHashIntTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import java.lang.reflect.Field; 8 | 9 | public class DoubleHashIntTest { 10 | private DoubleHashInt test; 11 | 12 | private int getSize() { 13 | return getFiled("size"); 14 | } 15 | 16 | private int getA() { 17 | return getFiled("a"); 18 | } 19 | 20 | private int getB() { 21 | return getFiled("b"); 22 | } 23 | 24 | private void setA(int a) { 25 | String name = "a"; 26 | setFiled(name, a); 27 | } 28 | 29 | private void setB(int b) { 30 | setFiled("b", b); 31 | } 32 | 33 | private int getFiled(String name) { 34 | try { 35 | Field field = test.getClass().getDeclaredField(name); 36 | field.setAccessible(true); 37 | return (int) field.get(test); 38 | } catch (Exception e) { 39 | Assert.fail(e.toString()); 40 | } 41 | throw new RuntimeException(); 42 | } 43 | 44 | private void setFiled(String name, int value) { 45 | try { 46 | Field field = test.getClass().getDeclaredField(name); 47 | field.setAccessible(true); 48 | field.set(test, value); 49 | } catch (Exception e) { 50 | Assert.fail(e.toString()); 51 | } 52 | } 53 | 54 | @Before 55 | public void setup() { 56 | test = new DoubleHashInt(3); 57 | } 58 | 59 | @Test 60 | public void constructor_input3_thenSizeIs3() { 61 | test = new DoubleHashInt(3); 62 | Assert.assertEquals(3, getSize()); 63 | } 64 | 65 | @Test 66 | public void constructor_inputValid_thenInitRandom_a_b() { 67 | for (int i = 0; i < 100; i++) { 68 | test = new DoubleHashInt(3); 69 | assertABValid(); 70 | } 71 | } 72 | 73 | private void assertABValid() { 74 | Assert.assertNotNull(getA()); 75 | Assert.assertNotNull(getB()); 76 | Assert.assertTrue(0 < getA()); 77 | Assert.assertTrue(0 < getB()); 78 | Assert.assertTrue(getA() < getSize()); 79 | Assert.assertTrue(getB() < getSize()); 80 | } 81 | 82 | @Test 83 | public void hash_input0_returns0() { 84 | Assert.assertEquals(0, test.hash(0)); 85 | } 86 | 87 | @Test 88 | public void hash_input1_givenAIs2_returns2() { 89 | setA(2); 90 | Assert.assertEquals(2, test.hash(1)); 91 | } 92 | 93 | @Test 94 | public void hash_input1_givenAIs4_givenSize3_returns1() { 95 | setA(4); 96 | Assert.assertEquals(1, test.hash(1)); 97 | } 98 | 99 | @Test 100 | public void hashTick_input0_returns1() { 101 | 102 | Assert.assertEquals(1, test.hashTick(0)); 103 | } 104 | 105 | @Test 106 | public void hashTick_input1_givenAIs2_returns1() { 107 | setB(2); 108 | Assert.assertEquals(1, test.hashTick(1)); 109 | } 110 | 111 | @Test 112 | public void hashTick_input1_givenAIs4_givenSize3_returns1() { 113 | setB(4); 114 | Assert.assertEquals(1, test.hashTick(1)); 115 | } 116 | 117 | @Test 118 | public void hashTick_input0_givenSize3_returns1() { 119 | Assert.assertEquals(1, test.hashTick(0)); 120 | } 121 | 122 | @Test 123 | public void hashTick_input2_givenSize3_returns1() { 124 | Assert.assertEquals(1, test.hashTick(2)); 125 | } 126 | 127 | @Test 128 | public void HashTick_input5_givenBIs1_givenSize3_return2() { 129 | setB(1); 130 | Assert.assertEquals(2, test.hashTick(5)); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /src/pgdp16/blatt08/pgdp/MoneyTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt08.pgdp; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | public class MoneyTest { 8 | private Money test; 9 | 10 | private static int randomInt(int range) { 11 | return (int) (Math.random() * range); 12 | } 13 | 14 | private static int randomInt() { 15 | return randomInt(Integer.MAX_VALUE); 16 | } 17 | 18 | @Before 19 | public void setUp() throws Exception { 20 | test = new Money(); 21 | } 22 | 23 | @Test 24 | public void constructor_givenEmpty_thenConstructWith0() throws Exception { 25 | int expected = 0; 26 | assertMoney(expected); 27 | } 28 | 29 | private void assertMoney(int expected) { 30 | Assert.assertEquals("Check the Variable: cent", expected, test.getCent()); 31 | } 32 | 33 | @Test 34 | public void constructor_given10_thenCentIs10() throws Exception { 35 | assertConstructor(10); 36 | } 37 | 38 | private void assertConstructor(int input) { 39 | test = new Money(input); 40 | assertMoney(input); 41 | } 42 | 43 | @Test 44 | public void constructor_givenRandom() throws Exception { 45 | assertConstructor(randomInt()); 46 | } 47 | 48 | @Test 49 | public void addMoney_given10_returns10() throws Exception { 50 | assertAddMoney(10, 10); 51 | } 52 | 53 | private void assertAddMoney(int expected, int addition) { 54 | int oldCent = test.getCent(); 55 | Money output = test.addMoney(new Money(addition)); 56 | String function = "addMoney(" + addition + ")"; 57 | 58 | Assert.assertNotNull(function + " shall not return null!", output); 59 | Assert.assertEquals(messageFunction("addMoney(" + addition + ")" + 60 | "; given cent == " + oldCent), 61 | expected, output.getCent()); 62 | Assert.assertEquals("Money Shell be Immutable!", oldCent, test.getCent()); 63 | } 64 | 65 | @Test 66 | public void addMoney_given10_given30_returns40() throws Exception { 67 | test = new Money(10); 68 | assertAddMoney(40, 30); 69 | } 70 | 71 | @Test 72 | public void addMoney_givenRandom_givenRandom() throws Exception { 73 | int start = randomInt(); 74 | int addition = randomInt(); 75 | test = new Money(start); 76 | assertAddMoney(start + addition, addition); 77 | } 78 | 79 | @Test 80 | public void toString_given10_returns0_10_Euro() throws Exception { 81 | assertToString("0,10 Euro", 10); 82 | } 83 | 84 | @Test 85 | public void toString_given1_returns0_01_Euro() throws Exception { 86 | assertToString("0,01 Euro", 1); 87 | } 88 | 89 | @Test 90 | public void toString_given10010_returns100_10_Euro() throws Exception { 91 | assertToString("100,10 Euro", 10010); 92 | } 93 | 94 | @Test 95 | public void toString_given12345_returns123_45_Euro() throws Exception { 96 | assertToString("123,45 Euro", 12345); 97 | } 98 | 99 | @Test 100 | public void toString_givenNegative_given100_returnsN1_00_Euro() throws Exception { 101 | assertToString("-0,10 Euro", -10); 102 | } 103 | 104 | @Test 105 | public void toString_givenNegative_given10_returnsN0_10_Euro() throws Exception { 106 | assertToString("-1,00 Euro", -100); 107 | } 108 | 109 | @Test 110 | public void toString_givenNegative_given110_returnsN1_10_Euro() throws Exception { 111 | assertToString("-1,10 Euro", -110); 112 | } 113 | 114 | private void assertToString(String expected, int input) { 115 | Assert.assertEquals(messageFunction("toString"), expected, new Money(input).toString()); 116 | } 117 | 118 | private String messageFunction(String function) { 119 | return "Check the Function: " + function; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/PredatorTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.junit.runners.Parameterized; 8 | 9 | import java.util.Arrays; 10 | import java.util.Collection; 11 | 12 | 13 | @RunWith(Parameterized.class) 14 | public class PredatorTest { 15 | private final String toTest; 16 | private final int withoutFood; 17 | private Predator test; 18 | 19 | public PredatorTest(String toTest, Predator test, int withoutFood) { 20 | this.toTest = toTest; 21 | this.test = test; 22 | this.withoutFood = withoutFood; 23 | } 24 | 25 | @Parameterized.Parameters(name = "{0}") 26 | public static Collection data() { 27 | 28 | return Arrays.asList(new Object[][]{ 29 | {"Leopard", new Leopard(false), 5}, 30 | {"Penguin", new Penguin(false), 12}, 31 | {"Snake", new Snake(false), 9}, 32 | }); 33 | } 34 | 35 | @Before 36 | public void setUp() throws Exception { 37 | test.position = new Position(); 38 | test.position.myAnimals = new Animal[]{test}; 39 | } 40 | 41 | @Test 42 | public void constructor() throws Exception { 43 | Assert.assertEquals("A " + toTest + " shall have a withoutFood of " + withoutFood, withoutFood, test.withoutFood); 44 | } 45 | 46 | 47 | @Test 48 | public void sunset_givenWithoutFoodIs5_thenBecomes4() throws Exception { 49 | test.withoutFood = 5; 50 | test.sunset(); 51 | Assert.assertEquals("On sunset() Predator.withoutFood shall be decreased", 4, test.withoutFood); 52 | assertAlive(true, "non starved Predators shall"); 53 | } 54 | 55 | @Test 56 | public void sunset_givenWithoutFoodIs0_thenAliveIsFalse() throws Exception { 57 | test.withoutFood = 0; 58 | test.sunset(); 59 | assertAlive(false, "starved Predators shall no longer"); 60 | } 61 | 62 | private void assertAlive(boolean expected, final String messagePart) { 63 | Assert.assertEquals("On sunset() " + messagePart + " be alive", expected, test.alive); 64 | } 65 | 66 | @Test 67 | public void possibleMoves_givenEnemyVegetarianG7_givenH8_ReturnG7() throws Exception { 68 | Move[] exp = buildKillerMove(); 69 | 70 | Move[] actual = test.possibleMoves(); 71 | 72 | assertMoves(exp, actual); 73 | } 74 | 75 | private Move[] buildKillerMove() { 76 | Predator predatorW_G8 = new Penguin(true); 77 | Vegetarian vegetarianW_G7 = new Vegetarian(true); 78 | Vegetarian vegetarianM_H7 = new Vegetarian(false); 79 | predatorW_G8.square = "g8"; 80 | vegetarianW_G7.square = "g7"; 81 | vegetarianM_H7.square = "h7"; 82 | 83 | test.position.myAnimals = new Animal[]{test, predatorW_G8, vegetarianW_G7, vegetarianM_H7}; 84 | return buildMoves("h8", "g7"); 85 | } 86 | 87 | private Move[] buildMoves(String start, String... targets) { 88 | test.square = start; 89 | Move[] out = new Move[targets.length]; 90 | for (int i = 0; i < out.length; i++) out[i] = new Move(start + targets[i]); 91 | return out; 92 | } 93 | 94 | 95 | private void assertMoves(Move[] exp, Move[] actual) { 96 | Assert.assertNotNull("possibleMoves() shall not return null!", actual); 97 | Assert.assertEquals("possibleMoves() shall have another length\n" 98 | + Arrays.toString(actual), exp.length, actual.length); 99 | for (Move move : exp) 100 | Assert.assertTrue("possibleMoves() does not contain: " 101 | + move + "\n" + Arrays.toString(actual), 102 | Arrays.asList(actual).contains(move)); 103 | } 104 | 105 | @Test 106 | public void applyMoves_givenKillerMove_increasesWithoutFood() throws Exception { 107 | test.withoutFood = 0; 108 | 109 | test.position.applyMoves(buildKillerMove()); 110 | 111 | Assert.assertEquals(withoutFood, test.withoutFood); 112 | } 113 | } -------------------------------------------------------------------------------- /src/gad17/blatt05/DoubleHashStringTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Ignore; 6 | import org.junit.Test; 7 | 8 | import java.lang.reflect.Field; 9 | import java.util.ArrayList; 10 | 11 | public class DoubleHashStringTest { 12 | 13 | private DoubleHashString test; 14 | 15 | private int getSize() { 16 | return (int) getFiled("size"); 17 | } 18 | 19 | private ArrayList getAVector() { 20 | return (ArrayList) getFiled("a"); 21 | } 22 | 23 | private ArrayList getBVector() { 24 | return (ArrayList) getFiled("b"); 25 | } 26 | 27 | private Object getFiled(String name) { 28 | try { 29 | Field field = test.getClass().getDeclaredField(name); 30 | field.setAccessible(true); 31 | return field.get(test); 32 | } catch (Exception e) { 33 | Assert.fail(e.toString()); 34 | } 35 | throw new RuntimeException(); 36 | } 37 | 38 | @Before 39 | public void setup() { 40 | test = new DoubleHashString(307); 41 | } 42 | 43 | @Test 44 | public void constructor_input2_saveSize2() { 45 | test = new DoubleHashString(2); 46 | Assert.assertEquals(2, getSize()); 47 | } 48 | 49 | @Test 50 | public void constructor_givenValid_thenInitA() { 51 | Assert.assertNotNull(getAVector()); 52 | Assert.assertEquals(true, getAVector().isEmpty()); 53 | } 54 | 55 | @Test 56 | public void constructor_givenValid_thenInitB() { 57 | Assert.assertNotNull(getBVector()); 58 | Assert.assertEquals(true, getBVector().isEmpty()); 59 | } 60 | 61 | 62 | @Test 63 | public void hash_givenAIsEmpty_thenASizeIs1() { 64 | test.hash("1"); 65 | assertSize(1, getAVector()); 66 | } 67 | 68 | private void assertSize(int expected, ArrayList vector) { 69 | Assert.assertNotNull(getAVector()); 70 | Assert.assertEquals(expected, vector.size()); 71 | } 72 | 73 | @Test 74 | @Ignore //Optional 75 | public void hash_givenAIsEmpty_thenA1IsLessThanSize() { 76 | test.hash("1"); 77 | Assert.assertNotNull(getAVector()); 78 | Assert.assertEquals(true, (long) getAVector().get(0) < getSize()); 79 | } 80 | 81 | @Test 82 | public void hash_input123_givenAIsEmpty_thenASizeIs3() { 83 | test.hash("123"); 84 | assertSize(3, getAVector()); 85 | } 86 | 87 | @Test 88 | public void hashTick_givenAIsEmpty_thenASizeIs1() { 89 | test.hashTick("1"); 90 | assertSize(1, getBVector()); 91 | } 92 | 93 | @Test 94 | @Ignore //Optional 95 | public void hashTick_givenBIsEmpty_thenA1IsLessThanSize() { 96 | test.hashTick("1"); 97 | Assert.assertNotNull(getBVector()); 98 | Assert.assertEquals(true, (long) getBVector().get(0) < getSize()); 99 | } 100 | 101 | @Test 102 | public void hashTick_input123_givenAIsEmpty_thenASizeIs3() { 103 | test.hashTick("123"); 104 | assertSize(3, getBVector()); 105 | } 106 | 107 | private void addA(long... i) { 108 | Assert.assertNotNull(getAVector()); 109 | for (long j : i) 110 | getAVector().add(j); 111 | } 112 | 113 | private void addB(long... i) { 114 | Assert.assertNotNull(getBVector()); 115 | for (long j : i) 116 | getBVector().add(j); 117 | } 118 | 119 | @Test 120 | public void hash_input1_givenA1_returns1() { 121 | addA(1); 122 | Assert.assertEquals('1', test.hash("1")); 123 | } 124 | 125 | @Test 126 | public void hash_input123_givenAIs3_2_1_returns298() { 127 | addA(3, 2, 1); 128 | Assert.assertEquals(298, test.hash("123")); 129 | } 130 | 131 | @Test 132 | public void hash_input1234_givenAIs4_3_2_1_returns193() { 133 | addA(4, 3, 2, 1); 134 | Assert.assertEquals(193, test.hash("1234")); 135 | } 136 | 137 | @Test 138 | public void hashTick_input1_givenA1_returns2() { 139 | addB(1); 140 | Assert.assertEquals('2', test.hashTick("1")); 141 | } 142 | 143 | @Test 144 | public void hashTick_input123_givenAIs3_2_1_returns299() { 145 | addB(3, 2, 1); 146 | Assert.assertEquals(299, test.hashTick("123")); 147 | } 148 | 149 | @Test 150 | public void hashTick_input1234_givenAIs4_3_2_1_returns195() { 151 | addB(4, 3, 2, 1); 152 | Assert.assertEquals(195, test.hashTick("1234")); 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /src/pgdp16/blatt09/fight/theWinnerTest_Marco.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt09.fight; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | public class theWinnerTest_Marco { 8 | private Position positionT; 9 | private char expected; 10 | 11 | @Before 12 | public void setUp(){ 13 | positionT = new Position(); 14 | } 15 | 16 | @Test 17 | public void EmptyBoardExpectsN() throws Exception { 18 | positionT.setMyAnimals(CreateAnimal(new Rabbit(false), "a1", false)); 19 | expected = 'N'; 20 | assertMoves(expected); 21 | } 22 | 23 | @Test 24 | public void TwoMaleVegetarianExpectsM() throws Exception { 25 | positionT.setMyAnimals(CreateAnimal(new Horse(false), "a1"), CreateAnimal(new Rabbit(false), "c3")); 26 | expected = 'M'; 27 | assertMoves(expected); 28 | } 29 | 30 | @Test 31 | public void MalePredExpectsM() throws Exception { 32 | positionT.setMyAnimals(CreateAnimal(new Leopard(false), "a1")); 33 | expected = 'M'; 34 | assertMoves(expected); 35 | } 36 | 37 | @Test 38 | public void TwoMaleVegetarianOneFemalPredExpectsX() throws Exception { 39 | positionT.setMyAnimals(CreateAnimal(new Leopard(true), "a1"), CreateAnimal(new Rabbit(false), "c3"), 40 | CreateAnimal(new Rabbit(false), "a2")); 41 | expected = 'X'; 42 | assertMoves(expected); 43 | } 44 | 45 | @Test 46 | public void MaleLeopardFemalePenguinExpectsW() throws Exception { 47 | positionT.setMyAnimals(CreateAnimal(new Leopard(false), "a1"), CreateAnimal(new Penguin(true), "c3")); 48 | expected = 'W'; 49 | assertMoves(expected); 50 | } 51 | 52 | @Test 53 | public void MaleLeopardFemaleLeopardExpectsN() throws Exception { 54 | positionT.setMyAnimals(CreateAnimal(new Leopard(false), "a1"), CreateAnimal(new Leopard(true), "c3")); 55 | expected = 'N'; 56 | assertMoves(expected); 57 | } 58 | 59 | @Test 60 | public void MaleLeopardSnakeFemalePenguinExpectsW() throws Exception { 61 | positionT.setMyAnimals(CreateAnimal(new Leopard(false), "a1"), CreateAnimal(new Penguin(true), "c3"), 62 | CreateAnimal(new Snake(false), "a2")); 63 | expected = 'W'; 64 | assertMoves(expected); 65 | } 66 | 67 | @Test 68 | public void TwoMaleVegTwoFemaleVegExpectsN() throws Exception { 69 | positionT.setMyAnimals(CreateAnimal(new Horse(false), "a1"), CreateAnimal(new Rabbit(false), "c3"), 70 | CreateAnimal(new Horse(true), "e4"), CreateAnimal(new Rabbit(true), "e5")); 71 | expected = 'N'; 72 | assertMoves(expected); 73 | } 74 | 75 | @Test 76 | public void TwoMaleVegOneFemaleVegExpectsM() throws Exception { 77 | positionT.setMyAnimals(CreateAnimal(new Horse(false), "a1"), CreateAnimal(new Rabbit(false), "c3"), 78 | CreateAnimal(new Rabbit(true), "e5")); 79 | expected = 'M'; 80 | assertMoves(expected); 81 | } 82 | 83 | @Test 84 | public void TwoMaleVegTwoFemaleVegOOneFemalePredExpectsX() throws Exception { 85 | positionT.setMyAnimals(CreateAnimal(new Horse(false), "a1"), CreateAnimal(new Rabbit(false), "c3"), 86 | CreateAnimal(new Horse(true), "e4"), CreateAnimal(new Rabbit(true), "e5"), 87 | CreateAnimal(new Leopard(true), "e5")); 88 | expected = 'X'; 89 | assertMoves(expected); 90 | } 91 | 92 | public Animal CreateAnimal(Animal toCreate, String sq) { 93 | if (toCreate instanceof Leopard) ((Predator) toCreate).withoutFood = 5; 94 | if (toCreate instanceof Penguin) ((Predator) toCreate).withoutFood = 12; 95 | if (toCreate instanceof Snake) ((Predator) toCreate).withoutFood = 9; 96 | toCreate.square = sq; 97 | toCreate.position = positionT; 98 | toCreate.alive = true; 99 | return toCreate; 100 | } 101 | 102 | public Animal CreateAnimal(Animal toCreate, String sq, boolean aliv) { 103 | if (toCreate instanceof Leopard) ((Predator) toCreate).withoutFood = 5; 104 | if (toCreate instanceof Penguin) ((Predator) toCreate).withoutFood = 12; 105 | if (toCreate instanceof Snake) ((Predator) toCreate).withoutFood = 9; 106 | toCreate.square = sq; 107 | toCreate.position = positionT; 108 | toCreate.alive = aliv; 109 | return toCreate; 110 | } 111 | 112 | private void assertMoves(char expected) { 113 | Assert.assertEquals(expected, positionT.theWinner()); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/pgdp16/blatt06/ToolboxTest.java: -------------------------------------------------------------------------------- 1 | package pgdp16.blatt06; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Arrays; 8 | import java.util.Collections; 9 | 10 | public class ToolboxTest { 11 | 12 | private static final int TEST_RANGE = 10000; 13 | private static final int TEST_INCREMENT = randomInt(1000); 14 | 15 | private static int randomInt(int range) { 16 | return (int) (Math.random() * range); 17 | } 18 | 19 | @Test 20 | public void evenSum_given0_returns0() throws Exception { 21 | assertEvenSum(0, 0); 22 | } 23 | 24 | @Test 25 | public void evenSum_given1_returns0() throws Exception { 26 | int expected = 0; 27 | int input = 1; 28 | assertEvenSum(expected, input); 29 | } 30 | 31 | private void assertEvenSum(int expected, int input) { 32 | printAssertMessage(expected, Toolbox.evenSum(input),input); 33 | printAssertMessage(-expected, Toolbox.evenSum(-input),input); 34 | } 35 | 36 | private void printAssertMessage(int expected, int actual, int... inputs) { 37 | Assert.assertEquals( 38 | inputToString(expected, inputs), 39 | inputToString(actual, inputs)); 40 | } 41 | 42 | private String inputToString(int expected, int[] inputs) { 43 | return "given: " + Arrays.toString(inputs) + "; returns: " + expected; 44 | } 45 | 46 | @Test 47 | public void evenSum_given8_returns20() throws Exception { 48 | assertEvenSum(20, 8); 49 | } 50 | 51 | @Test 52 | public void evenSum_given9_returns20() throws Exception { 53 | assertEvenSum(20, 9); 54 | } 55 | 56 | @Test 57 | public void evenSum_givenAllPossibilities() throws Exception { 58 | for (int n = 0; n <= TEST_RANGE; n += TEST_INCREMENT) assertEvenSum(evenSum(n), n); 59 | } 60 | 61 | private int evenSum(int n) { 62 | return n / 2 * (n / 2 + 1); 63 | } 64 | 65 | @Test 66 | public void multiplication_given0() throws Exception { 67 | assertMultiplication(0, 33); 68 | } 69 | 70 | @Test 71 | public void multiplication_givenAllPossibilities() throws Exception { 72 | for (int x = 0; x <= TEST_RANGE; x += TEST_INCREMENT) 73 | for (int y = 0; y <= TEST_RANGE; y += TEST_INCREMENT) 74 | assertMultiplication(x, y); 75 | } 76 | 77 | private void assertMultiplication(int x, int y) { 78 | printAssertMessage(x * y, Toolbox.multiplication(x, y),x,y); 79 | printAssertMessage(y * x, Toolbox.multiplication(y, x),x,y); 80 | } 81 | 82 | @Test 83 | public void reverse_given01_thenMakes10() throws Exception { 84 | int[] arr = {0, 1}; 85 | assertReverse(arr); 86 | } 87 | 88 | @Test 89 | public void reverse_given012_thenMakes210() throws Exception { 90 | int[] arr = {0, 1, 2}; 91 | assertReverse(arr); 92 | } 93 | 94 | @Test 95 | public void reverse_givenRandom_thenReverse() throws Exception { 96 | int[] arr = new int[randomInt(ToolboxTest.TEST_RANGE)]; 97 | for (int i = 0; i < arr.length; i++) arr[i] = randomInt(ToolboxTest.TEST_RANGE); 98 | assertReverse(arr); 99 | } 100 | 101 | private void assertReverse(int[] arr) { 102 | String expected = (reverse(arr)).toString(); 103 | Toolbox.reverse(arr); 104 | Assert.assertEquals(expected, Arrays.toString(arr)); 105 | } 106 | 107 | private ArrayList reverse(int[] arr) { 108 | ArrayList out = new ArrayList<>(); 109 | for (int anArr : arr) out.add(anArr); 110 | Collections.reverse(out); 111 | return out; 112 | } 113 | 114 | @Test 115 | public void numberOfOddIntegers_givenEmptyArray_returns0() throws Exception { 116 | assertNumberOfOddIntegers(new int[]{}, 0); 117 | } 118 | 119 | private void assertNumberOfOddIntegers(int[] m, int expected) { 120 | printAssertMessage(expected, Toolbox.numberOfOddIntegers(m),m); 121 | } 122 | 123 | @Test 124 | public void numberOfOddIntegers_given474251N50N4N3_returns5() throws Exception { 125 | assertNumberOfOddIntegers(new int[]{4, 7, 42, 5, 1, -5, 0, -4, -3}, 5); 126 | } 127 | 128 | @Test 129 | public void filterOdd_given474251N50N4N3_returns751N5N3() throws Exception { 130 | assertFilterOdd(new int[]{4, 7, 42, 5, 1, -5, 0, -4, -3}, 131 | new int[]{7, 5, 1, -5, -3}); 132 | } 133 | 134 | private void assertFilterOdd(int[] m, int[] expected) { 135 | Assert.assertArrayEquals(expected, Toolbox.filterOdd(m)); 136 | } 137 | 138 | 139 | } -------------------------------------------------------------------------------- /src/gad17/blatt05/FactoryTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt05; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Ignore; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.junit.runners.Parameterized; 9 | 10 | import java.lang.reflect.Field; 11 | import java.util.Arrays; 12 | import java.util.Collection; 13 | 14 | @RunWith(Parameterized.class) 15 | public class FactoryTest { 16 | private final HashableFactory factory; 17 | private final Object[] inputs; 18 | private DoubleHashable test; 19 | 20 | public FactoryTest(String name, HashableFactory factory, Object[] inputs) { 21 | this.factory = factory; 22 | this.inputs = inputs; 23 | } 24 | 25 | @Parameterized.Parameters(name = "{0}") 26 | public static Collection data() { 27 | return Arrays.asList(new Object[][]{ 28 | {"DoubleHashInt", new IntHashableFactory(), new Object[]{123, 124}}, 29 | {"DoubleHashString", new StringHashableFactory(), new Object[]{"123", "124"}} 30 | }); 31 | } 32 | 33 | private int getSize() { 34 | try { 35 | Field field = test.getClass().getDeclaredField("size"); 36 | field.setAccessible(true); 37 | return (int) field.get(test); 38 | } catch (Exception e) { 39 | Assert.fail(e.toString()); 40 | } 41 | throw new RuntimeException(); 42 | } 43 | 44 | @Before 45 | public void setup() { 46 | test = factory.create(101); 47 | } 48 | 49 | @Test(expected = Exception.class) 50 | @Ignore //Optional 51 | public void constructor_input0_throwsException() { 52 | factory.create(0); 53 | } 54 | 55 | @Test(expected = Exception.class) 56 | @Ignore //Optional 57 | public void constructor_inputNegative_throwsException() { 58 | factory.create(-2); 59 | } 60 | 61 | @Test(expected = Exception.class) 62 | @Ignore //Optional 63 | public void constructor_inputNotPrime_throwsException() { 64 | factory.create(12); 65 | } 66 | 67 | @Test 68 | public void constructor_input3_thenSizeIs3() { 69 | test = factory.create(3); 70 | Assert.assertEquals(3, getSize()); 71 | } 72 | 73 | @Test(expected = Exception.class) 74 | public void hash_inputNull_throwsException() { 75 | test.hash(null); 76 | } 77 | 78 | @Test(expected = Exception.class) 79 | @Ignore //Optional 80 | public void hash_inputEmptyString_throwsException() { 81 | test.hash(""); 82 | } 83 | 84 | @Test 85 | public void hash_calledWithSameKey2Times_returnsSameValue() throws Exception { 86 | Assert.assertEquals(test.hash(inputs[0]), test.hash(inputs[0])); 87 | } 88 | 89 | 90 | @Test 91 | public void hash_calledWithDifferentKey2Times_returnsDifferentValues() throws Exception { 92 | Assert.assertNotEquals(test.hash(inputs[0]), test.hash(inputs[1])); 93 | } 94 | 95 | @Test(expected = Exception.class) 96 | public void hashTick_inputNull_throwsException() { 97 | test.hashTick(null); 98 | } 99 | 100 | @Test(expected = Exception.class) 101 | @Ignore //Optional 102 | public void hashTick_inputEmptyString_throwsException() { 103 | test.hashTick(""); 104 | } 105 | 106 | @Test 107 | public void hashTick_calledWithSameKey2Times_returnsSameValue() throws Exception { 108 | Assert.assertEquals(test.hashTick(inputs[0]), test.hashTick(inputs[0])); 109 | } 110 | 111 | @Test 112 | public void hashTick_calledWithDifferentKey2Times_returnsDifferentValues() throws Exception { 113 | Assert.assertNotEquals(test.hashTick(inputs[0]), test.hashTick(inputs[1])); 114 | } 115 | 116 | @Test 117 | public void hash_returnsValuesBetween0AndSize() throws Exception { 118 | test = factory.create(101); 119 | for (int i = -10000; i <= 10000; i++) { 120 | Object key = factory instanceof StringHashableFactory ? 121 | i + "" : i; 122 | String message = "Actual:" + test.hash(key); 123 | Assert.assertTrue(message, 0 <= test.hash(key)); 124 | Assert.assertTrue(message, test.hash(key) < getSize()); 125 | } 126 | } 127 | 128 | @Test 129 | public void hashTick_returnsValuesBetween1AndSize() throws Exception { 130 | test = factory.create(101); 131 | for (int i = -10000; i <= 10000; i++) { 132 | Object key = factory instanceof StringHashableFactory ? 133 | i + "" : i; 134 | String message = "Actual:" + test.hashTick(key); 135 | Assert.assertTrue(message, 0 < test.hashTick(key)); 136 | Assert.assertTrue(message, test.hashTick(key) < getSize()); 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/gad17/blatt08/BinomialTreeNodeTest.java: -------------------------------------------------------------------------------- 1 | package gad17.blatt08; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import java.util.Arrays; 8 | 9 | public class BinomialTreeNodeTest { 10 | private BinomialTreeNode a, b; 11 | 12 | private int getKey() { 13 | return a.key; 14 | } 15 | 16 | @Before 17 | public void setUp() throws Exception { 18 | a = new BinomialTreeNode(1); 19 | b = new BinomialTreeNode(5); 20 | } 21 | 22 | @Test 23 | public void constructor() { 24 | Assert.assertEquals(1, getKey()); 25 | } 26 | 27 | @Test 28 | public void min_givenKey1_return1() throws Exception { 29 | assertMin(1, a); 30 | } 31 | 32 | @Test 33 | public void min_givenKey9_return9() throws Exception { 34 | a = new BinomialTreeNode(9); 35 | assertMin(9, a); 36 | } 37 | 38 | @Test 39 | public void rank_givenInit_returns0() throws Exception { 40 | assertRank(0, this.a); 41 | } 42 | 43 | private void assertRank(int expected, BinomialTreeNode node) { 44 | Assert.assertEquals(expected, node.rank()); 45 | } 46 | 47 | @Test(expected = Exception.class) 48 | public void merge_given_both_null_throw_exception() throws Exception { 49 | BinomialTreeNode.merge(null, null); 50 | } 51 | 52 | @Test(expected = Exception.class) 53 | public void merge_given_a_null_throw_exception() throws Exception { 54 | BinomialTreeNode.merge(null, a); 55 | } 56 | 57 | @Test(expected = Exception.class) 58 | public void merge_given_b_null_throw_exception() throws Exception { 59 | BinomialTreeNode.merge(a, null); 60 | } 61 | 62 | @Test(expected = Exception.class) 63 | public void merge_given_differentLength_throw_exception() throws Exception { 64 | BinomialTreeNode.merge(new BinomialTreeNode(10), BinomialTreeNode.merge(a, b)); 65 | } 66 | 67 | @Test(expected = Exception.class) 68 | public void merge_given_sameTree_throw_exception() throws Exception { 69 | BinomialTreeNode.merge(a,a); 70 | } 71 | 72 | @Test 73 | public void merge_givenRank0_Rank0_return_rank1() throws Exception { 74 | BinomialTreeNode actual = buildTree(1); 75 | assertRank(1, actual); 76 | } 77 | 78 | @Test 79 | public void rank_given10_returns10() throws Exception { 80 | assertRank(10, buildTree(10)); 81 | } 82 | 83 | 84 | static BinomialTreeNode buildTree(int rank) { 85 | return buildTree(rank, 0); 86 | } 87 | 88 | static BinomialTreeNode buildTree(int rank, int min) { 89 | if (rank <= 0) return new BinomialTreeNode(min); 90 | return BinomialTreeNode.merge(buildTree(rank - 1, 91 | min, 1), 92 | buildTree(rank - 1, min + 1, 1)); 93 | } 94 | 95 | static BinomialTreeNode buildTree(int rank, int counter, int delta) { 96 | if (rank <= 0) return new BinomialTreeNode(counter); 97 | return BinomialTreeNode.merge(buildTree(rank - 1, 98 | counter, delta), 99 | buildTree(rank - 1, counter + delta, delta)); 100 | } 101 | 102 | @Test 103 | public void merge_givenRank1_Rank1_return_rank2() throws Exception { 104 | assertRank(2, buildTree(2)); 105 | } 106 | 107 | @Test 108 | public void merge_givenAGreaterB_returnB() throws Exception { 109 | a = new BinomialTreeNode(99); 110 | Assert.assertEquals(b, BinomialTreeNode.merge(a, b)); 111 | } 112 | 113 | @Test 114 | public void merge_givenBGreaterA_returnB() throws Exception { 115 | b = new BinomialTreeNode(99); 116 | Assert.assertEquals(a, BinomialTreeNode.merge(a, b)); 117 | } 118 | 119 | @Test 120 | public void deleteMin_givenRank1_returnsTreeRank0() throws Exception { 121 | Assert.assertArrayEquals(new BinomialTreeNode[0], a.deleteMin()); 122 | } 123 | 124 | @Test 125 | public void min_givenIsRoot_returnKey() throws Exception { 126 | assertMin(0, buildTree(2)); 127 | } 128 | 129 | private void assertMin(int expected, BinomialTreeNode node) { 130 | Assert.assertEquals(expected, node.min()); 131 | } 132 | 133 | 134 | @Test 135 | public void deleteMin_given_treeRank1_returnsTreeRank0() throws Exception { 136 | assertTreeArray(new BinomialTreeNode[]{buildTree(0, 1)}, buildTree(1).deleteMin()); 137 | } 138 | 139 | private void assertTreeArray(BinomialTreeNode[] expected, BinomialTreeNode[] actual) { 140 | Assert.assertEquals(Arrays.toString(expected), Arrays.toString(actual)); 141 | } 142 | 143 | @Test 144 | public void deleteMin_given_treeRank2_returnsArraySize2() throws Exception { 145 | assertTreeArray(new BinomialTreeNode[]{buildTree(0, 1), 146 | buildTree(1, 1)}, buildTree(2).deleteMin()); 147 | } 148 | } --------------------------------------------------------------------------------