Compare commits

...

23 Commits

Author SHA1 Message Date
b2eb22a6cc uebung09 small adjustments 2025-06-12 12:10:00 +02:00
57aae3eda6 uebung09 2025-06-12 11:59:41 +02:00
f42c1b23ab angabe ue09 2025-06-10 13:33:42 +02:00
9e5e9eb91a ue6 2025-06-06 14:30:41 +02:00
4b73c1b5e3 correction ue7 2025-06-06 14:21:53 +02:00
1b07a3b6ab uebung07 2025-06-04 18:27:10 +02:00
701e732eb6 uebung08 2025-06-04 18:25:35 +02:00
ac9d7fdd39 updated to generic 2025-05-26 14:39:37 +02:00
8942229d87 added uebung07 angabe 2025-05-23 20:54:12 +02:00
6efe637f23 uebung05 ende 2025-05-13 18:13:15 +02:00
cd913d2703 uebung05 aufgabenstellung 2025-05-13 13:13:09 +02:00
e7f7a20ea6 formatting 2025-05-13 13:06:17 +02:00
703097ad4d fixed Exception handling 2025-05-13 13:01:17 +02:00
136be17443 uebung04 2025-05-13 12:49:13 +02:00
192db94f14 added uebung04 task 2025-05-02 14:01:50 +02:00
38a6313e08 fixed init nodes 2025-05-02 13:54:37 +02:00
6426a6ccfb fixed typos 2025-05-02 13:53:22 +02:00
a50307c4d4 added demo classes 2025-05-02 13:53:13 +02:00
1ff5ea2a75 uebung03 2025-05-02 13:48:51 +02:00
2be863dcb0 refactor for tests 2025-04-29 14:41:38 +02:00
5f3c838363 added uebung03 tests 2025-04-28 15:20:58 +02:00
7f113c4526 uebung02 aufgabe 2 2025-04-28 15:16:11 +02:00
bb54a8a684 uebung02 aufgabe 1 2025-04-28 15:15:53 +02:00
114 changed files with 5451 additions and 1 deletions

View File

@@ -5,6 +5,8 @@ public class ChessApp {
final Piece p2 = new Piece(Kind.QUEEN, Color.WHITE, board, 4, 8);
final Piece p3 = new Piece(Kind.KNIGHT, Color.WHITE, board, 3, 3);
final Piece p4 = new Piece(Kind.KNIGHT, Color.BLACK, board, 4, 6);
final Piece p5 = new Piece(Kind.ROOK, Color.BLACK, board, 7, 6);
final Piece p6 = new Piece(Kind.ROOK, Color.WHITE, board, 7, 2);
board.printBoard(System.out);
board.check(System.out);
}

View File

@@ -159,4 +159,10 @@ public class ChessTest {
numPieces++;
assertEquals(expectedNumPieces, numPieces);
}
@Test
public void testRunWithErrors() {
final Piece rook = new Piece(Kind.ROOK, Color.WHITE, board, 1, 1);
assertEquals(Kind.ROOK, board.pieceAt(1, 1).kind);
}
}

View File

@@ -1,3 +1,3 @@
public enum Kind {
QUEEN, KNIGHT
QUEEN, KNIGHT, ROOK
}

View File

@@ -25,6 +25,8 @@ class Piece {
return queenCharRep();
case KNIGHT:
return knightCharRep();
case ROOK:
return rookCharRep();
}
throw new IllegalArgumentException("Unknown piece " + kind);
}
@@ -35,6 +37,8 @@ class Piece {
return queenShowPiece();
case KNIGHT:
return knightShowPiece();
case ROOK:
return rookShowPiece();
}
throw new IllegalArgumentException("Unknown piece " + kind);
}
@@ -45,6 +49,8 @@ class Piece {
return queenCanCapture(other);
case KNIGHT:
return knightCanCapture(other);
case ROOK:
return rookCanCapture(other);
}
throw new IllegalArgumentException("Unknown piece " + kind);
}
@@ -63,6 +69,14 @@ class Piece {
return 'N';
}
char rookCharRep() {
if (color == Color.WHITE) {
return 'r';
} else {
return 'R';
}
}
String queenShowPiece() {
return "" + color.toString().toLowerCase() + " queen at (" + row + ", " + col + ")";
}
@@ -71,6 +85,10 @@ class Piece {
return "" + color.toString().toLowerCase() + " knight at (" + row + ", " + col + ")";
}
String rookShowPiece() {
return "" + color.toString().toLowerCase() + " rook at (" + row + ", " + col + ")";
}
boolean queenCanCapture(Piece other) {
if (board != other.board || color == other.color)
return false;
@@ -97,4 +115,22 @@ class Piece {
final int dc = abs(col - other.col);
return dr == 2 && dc == 1 || dr == 1 && dc == 2;
}
boolean rookCanCapture(Piece other) {
if (board != other.board || color == other.color)
return false;
if (other.row != row && other.col != col )
return false;
final int dr = signum(other.row - row);
final int dc = signum(other.col - col);
int r = row + dr;
int c = col + dc;
while (r != other.row || c != other.col) {
if (board.pieceAt(r, c) != null) return false;
r += dr;
c += dc;
}
return true;
}
}

View File

@@ -0,0 +1,122 @@
package uebung02.rationals;
public class Rational {
private final int num, denom;
Rational(int num, int denom) {
if (denom == 0) {
throw new IllegalArgumentException("Zahl darf nicht 0 sein!");
} else {
int gcd = gcDivider(num, denom);
if (denom < 0) {
this.num = -num / gcd;
this.denom = -denom / gcd;
} else {
this.num = num / gcd;
this.denom = denom / gcd;
}
}
}
Rational (int num) {
this(num, 1);
}
private int gcDivider(int x, int y) {
//return x == 0 ? y : gcDivider(y % x, x);
if (x == 0) {
return y;
} else {
return gcDivider(y % x, x);
}
}
public int getNum() {
return num;
}
public int getDenom() {
return denom;
}
public Rational add(Rational other) {
return new Rational(num * other.getDenom() + denom * other.getNum(),
denom * other.getDenom());
}
public Rational add(int i) {
return add(new Rational(i));
}
public Rational sub(Rational r) {
return new Rational(num * r.denom - denom * r.num, denom * r.denom);
}
public Rational sub(int i) {
return this.sub(new Rational(i));
}
public Rational mult(Rational r) {
return new Rational(num * r.num, denom * r.denom);
}
public Rational mult(int i) {
return mult(new Rational(i));
}
public Rational div(Rational r) {
return new Rational(num * r.denom, denom * r.num);
}
public Rational div(int i) {
return div(new Rational(i));
}
@Override
public String toString() {
if (denom == 1) {
return "" + num;
} else {
return num + "/" + denom;
}
}
public boolean lessThan(Rational r) {
return this.sub(r).num < 0;
}
public boolean lessThan(int i) {
return this.sub(i).num < 0;
}
public boolean greaterThan(Rational r) {
return this.sub(r).num > 0;
}
public boolean greaterThan(int i) {
return this.sub(i).num > 0;
}
public boolean lessThanOrEqual(Rational r) {
return this.sub(r).num <= 0;
}
public boolean lessThanOrEqual(int i) {
return this.sub(i).num <= 0;
}
public boolean greaterThanOrEqual(Rational r) {
return this.sub(r).num >= 0;
}
public boolean isEqual(Rational r) {
return this.num == r.num && this.denom == r.denom;
}
public boolean isEqual(int i) {
return this.isEqual(new Rational(i));
}
}

View File

@@ -0,0 +1,23 @@
package uebung02.rationals;
public class RationalDemo {
public static void main(String[] args) {
Rational r1 = new Rational(1, 2);
Rational r2 = new Rational(2, 4);
Rational r3 = new Rational(6, 18);
Rational r4 = new Rational(3, 12);
Rational r5 = new Rational(1);
Rational r6 = new Rational(2);
Rational r7 = new Rational(4, 3);
System.out.println("1/2 = " + r1);
System.out.println("2/4 = " + r2);
System.out.println("6/18 = " + r3);
System.out.println("1/2 + 2/4 = " + r1.add(r2));
System.out.println("1/2 - 6/18 = " + r1.sub(r3));
System.out.println("6/18 * 2/4 = " + r3.mult(r2));
System.out.println("1/2 == 2/4 ? " + r1.isEqual(r2));
System.out.println("2/4 == 3/12 ? " + r2.isEqual(r4));
System.out.println("1 + 2 / (4/3) = " + r5.add(r6.div(r7)));
}
}

View File

@@ -0,0 +1,21 @@
package graph;
public class Edge {
private int length;
private final Node target;
public Edge (int lenght, Node target) {
this.target = target;
this.length = lenght;
}
@Override
public String toString() {
return "-" + length + "->" + target.getName();
}
public Node getTarget() {
return target;
}
}

View File

@@ -0,0 +1,70 @@
package graph;
import java.util.ArrayList;
public class Graph {
private ArrayList<Node> nodes = new ArrayList<>();
public ArrayList<Node> getNodes() {
return nodes;
}
public Node getFirstNode() {
return nodes.get(0);
}
public void addNode(Node n) {
if (nodes.contains(n)) {
throw new IllegalArgumentException();
}
nodes.add(n);
}
public ArrayList<Node> depthFirst(Node startNode) {
unmarkAllNodes();
final ArrayList<Node> collect = new ArrayList<>();
search(startNode, collect);
return collect;
}
private void search(Node node, ArrayList<Node> collect) {
if (node.getMarked()) return;
node.setMarked(true);
collect.add(node);
for (Edge e : node.getOut())
search(e.getTarget(), collect);
}
public ArrayList<Node> breadthFirst(Node startNode) {
unmarkAllNodes();
final ArrayList<Node> collect = new ArrayList<>();
startNode.setMarked(true);
collect.add(startNode);
// Die folgende For-Schleife kann nicht als foreach-loop
// realisiert werden, weil sich collect laufend ändert
for (int i = 0; i < collect.size(); i++)
for (Edge edge : collect.get(i).getOut()) {
final Node target = edge.getTarget();
if (!target.getMarked()) {
target.setMarked(true);
collect.add(target);
}
}
return collect;
}
private void unmarkAllNodes() {
for (Node n : nodes)
n.setMarked(false);
}
@Override
public String toString() {
StringBuilder str = new StringBuilder();
str.append("Grap=\n");
for (Node n : nodes) {
str.append(n).append(";\n");
}
return str.toString();
}
}

View File

@@ -0,0 +1,47 @@
package graph;
import java.util.ArrayList;
public class GraphDemo {
public static void main(String[] args) {
final Graph graph = makeGraph();
System.out.println(graph);
System.out.println();
final Node nodeA = graph.getNodes().get(0);
System.out.println("breadth first: " + nodeList(graph.breadthFirst(nodeA)));
System.out.println("depth first: " + nodeList(graph.depthFirst(nodeA)));
}
private static Graph makeGraph() {
final Node nodeA = new Node("A");
final Node nodeB = new Node("B");
final Node nodeC = new Node("C");
final Node nodeD = new Node("D");
final Node nodeE = new Node("E");
final Graph graph = new Graph();
graph.addNode(nodeA);
graph.addNode(nodeB);
graph.addNode(nodeC);
graph.addNode(nodeD);
graph.addNode(nodeE);
nodeA.addEdge(new Edge(2, nodeB));
nodeA.addEdge(new Edge(4, nodeD));
nodeB.addEdge(new Edge(2, nodeA));
nodeB.addEdge(new Edge(2, nodeC));
nodeB.addEdge(new Edge(4, nodeE));
nodeC.addEdge(new Edge(2, nodeB));
nodeD.addEdge(new Edge(4, nodeA));
nodeD.addEdge(new Edge(2, nodeE));
nodeE.addEdge(new Edge(4, nodeB));
nodeE.addEdge(new Edge(2, nodeD));
return graph;
}
private static String nodeList(ArrayList<Node> nodes) {
final ArrayList<String> names = new ArrayList<>();
for (Node n : nodes)
names.add(n.getName());
return names.toString();
}
}

View File

@@ -0,0 +1,79 @@
package graph;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertEquals;
public class GraphTest {
private final Node nodeA = new Node("A");
private final Node nodeB = new Node("B");
private final Node nodeC = new Node("C");
private final Node nodeD = new Node("D");
private final Node nodeE = new Node("E");
private final Node nodeF = new Node("F");
private final Node nodeG = new Node("G");
private final Node nodeH = new Node("H");
private final Graph graph = new Graph();
@Before
public void setup() {
graph.addNode(nodeA);
graph.addNode(nodeB);
graph.addNode(nodeC);
graph.addNode(nodeD);
graph.addNode(nodeE);
graph.addNode(nodeF);
graph.addNode(nodeG);
nodeA.addEdge(new Edge(3, nodeB));
nodeB.addEdge(new Edge(7, nodeD));
nodeD.addEdge(new Edge(1, nodeE));
nodeE.addEdge(new Edge(2, nodeC));
nodeE.addEdge(new Edge(2, nodeH));
nodeE.addEdge(new Edge(4, nodeA));
nodeF.addEdge(new Edge(3, nodeG));
nodeH.addEdge(new Edge(6, nodeB));
}
@Test
public void testBreadthFirstGraphE() {
final Set<Node> result = new HashSet<>(graph.breadthFirst(nodeE));
assertEquals(Set.of(nodeA, nodeB, nodeC, nodeD, nodeE, nodeH), result);
}
@Test
public void testBreadthFirstGraphF() {
final Set<Node> result = new HashSet<>(graph.breadthFirst(nodeF));
assertEquals(Set.of(nodeF, nodeG), result);
}
@Test
public void testBreadthFirstGraphC() {
final ArrayList<Node> result = graph.breadthFirst(nodeC);
assertEquals(List.of(nodeC), result);
}
@Test
public void testDepthFirstGraphE() {
final Set<Node> result = new HashSet<>(graph.depthFirst(nodeE));
assertEquals(Set.of(nodeA, nodeB, nodeC, nodeD, nodeE, nodeH), result);
}
@Test
public void testDepthFirstGraphF() {
final Set<Node> result = new HashSet<>(graph.depthFirst(nodeF));
assertEquals(Set.of(nodeF, nodeG), result);
}
@Test
public void testDepthFirstGraphC() {
final ArrayList<Node> result = graph.depthFirst(nodeC);
assertEquals(List.of(nodeC), result);
}
}

View File

@@ -0,0 +1,44 @@
package graph;
import java.util.ArrayList;
public class Node {
private final String name;
private ArrayList<Edge> out = new ArrayList<>();
private boolean marked;
public Node(String name) {
this.name = name;
}
public String getName() {
return name;
}
public ArrayList<Edge> getOut() {
return out;
}
public boolean getMarked() {
return marked;
}
public void setMarked(boolean n) {
marked = n;
}
public void addEdge(Edge e) {
if (out.contains(e)) {
throw new IllegalArgumentException();
}
out.add(e);
}
@Override
public String toString() {
return name + ": " + out;
}
}

View File

@@ -0,0 +1,191 @@
package polynomial;
import java.util.Arrays;
public class Polynomial {
private final int[] werte;
private final int degree;
public Polynomial(int[] w) {
werte = w.clone();
int deg = 0;
for(int i = 0; i<w.length; i++){
if (werte[i] != 0){
deg = i;
}
}
degree = deg;
//gleichwertige Variante
/*for (int i = w.length; i >= 0; i--) {
if (w[i] != 0) {
degree = i;
}
}*/
}
public Polynomial() {
this(new int[]{0,1});
}
public int getDegree() {
return degree;
}
//Option 1 Kopie des Arrays
public int[] getCoeef() {
return werte.clone();
}
//Option 2 Koeef an der Stelle i
public int getCoeefAt(int i) {
return i > degree ? 0 : werte[i];
}
public static Polynomial constant(int v) {
return new Polynomial(new int[]{v});
}
@Override
public String toString() {
if (isNull()){return "0";}
String result = "";
for (int i = degree; i >= 0; i--) {
if (werte[i] == 0) {continue;}
if (!result.isEmpty()) {
if (werte[i] > 0) {result += " + ";}
if (werte[i] < 0) {result += " - ";}
}
if (result.isEmpty() && werte[i] <0){result += "-";} // Minus am Anfang
int absCoeff = Math.abs(werte[i]);
if (absCoeff != 1 && i == 0) {result += absCoeff;}
if (absCoeff == 1 && i == 0) {result += "1";}
if (absCoeff != 1 && i != 0) {result += absCoeff +"*";}
if (i > 0) {
result += "x";
if (i > 1) {result += "^" + i;}
}
}
return result;
}
public Boolean isNull(){
for(int i = werte.length-1; i >= 0; i--){
if (werte[i] != 0) {
return false;
}
}
return true;
}
public String toString1() {
StringBuilder result = new StringBuilder();
for (int i = degree; i >= 0; i--) {
if (werte[i] == 0) {continue;}
if (!result.isEmpty()) {
if (werte[i] > 0) {result.append(" + ");}
if (werte[i] < 0) {result.append(" - ");}
}
if (result.isEmpty() && werte[i] <0){result.append("-");} // Minus am Anfang
int absCoeff = Math.abs(werte[i]);
if (absCoeff != 1 && i == 0) {result.append(absCoeff);}
if (absCoeff == 1 && i == 0) {result.append(1);}
if (absCoeff != 1 && i != 0) {
result.append(absCoeff);
result.append("*");}
if (i > 0) {
result.append("x");
if (i > 1) {result.append("^");
result.append(i);
}
}
}
return result.toString();
}
public Polynomial add(Polynomial p2){
int[] p1 = getCoeef();
int[] p2Coeff = p2.getCoeef();
int[] p3 = new int[Math.max(p1.length,p2Coeff.length)];
for(int x = p3.length-1; x >= 0; x --){
int coeff1 = (x < p1.length) ? p1[x] : 0;
int coeff2 = (x < p2Coeff.length) ? p2Coeff[x] : 0;
p3[x] = coeff1 + coeff2;
}
Polynomial result = new Polynomial(p3);
return result;
}
public Polynomial sub(Polynomial that) {
int[] res = Arrays.copyOf(werte, Math.max(this.degree, that.degree) + 1);
for (int i = 0; i <= that.degree; i++)
res[i] -= that.werte[i];
return new Polynomial(res);
}
public Polynomial mult(Polynomial that) {
int[] res = new int[this.degree + that.degree + 1];
Arrays.fill(res, 0);
for (int i = 0; i <= this.degree; i++)
for (int j = 0; j <= that.degree; j++)
res[i + j] += this.werte[i] * that.werte[j];
return new Polynomial(res);
}
public Polynomial exp(int e) {
if (e < 0)
throw new IllegalArgumentException("Exponent must not be negative");
else if (e == 0)
return constant(1);
else if (e == 1)
return this;
else if (e % 2 == 0)
return this.mult(this).exp(e / 2);
else
return this.mult(this.exp(e - 1));
}
public Polynomial add(int v) {
int[] res = Arrays.copyOf(werte, werte.length);
res[0] += v;
return new Polynomial(res);
}
public Polynomial sub(int v) {
return this.add(-v);
}
public Polynomial mult(int v) {
int[] res = Arrays.copyOf(werte, werte.length);
for (int j = 0; j < res.length; j++)
res[j] *= v;
return new Polynomial(res);
}
public int apply(int v) {
int res = werte[werte.length - 1];
for (int i = werte.length - 2; i >= 0; i--)
res = res * v + werte[i];
return res;
}
}

View File

@@ -0,0 +1,76 @@
package polynomial;
public class PolynomialDemo {
public static void main(String[] args) {
//b
Polynomial p1 = new Polynomial(new int[]{1, 2, 3, 4});
Polynomial p2 = new Polynomial(new int[]{0, 0, 0});
Polynomial p3 = new Polynomial(new int[]{3});
//Polynomial p99 = new Polynomial(new int[]{});
//c
System.out.println();
System.out.println(p1.getDegree());
System.out.println(p2.getDegree());
System.out.println(p3.getDegree());
//f
System.out.println();
Polynomial p4 = new Polynomial();
System.out.println(p4.getDegree());
//g
System.out.println();
Polynomial p5 = Polynomial.constant(5);
System.out.println(p5.getDegree());
//h
System.out.println();
Polynomial p6 = new Polynomial(new int[]{0, -1, 2, +3, -2, 0, 1});
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
System.out.println(p4);
System.out.println(p5);
System.out.println(p6);
//i
System.out.println();
Polynomial p7 = p1.add(p6);
System.out.println(p7);
//j
System.out.println();
Polynomial p8 = p1.sub(p6);
Polynomial p9 = p6.sub(p6);
Polynomial p10 = p1.mult(p1);
Polynomial p11 = p1.mult(p6);
System.out.println(p8);
System.out.println(p9);
System.out.println(p10);
System.out.println(p11);
//k
System.out.println();
Polynomial p12 = p1.add(5);
Polynomial p13 = p6.sub(5);
Polynomial p14 = p1.mult(5);
System.out.println(p12);
System.out.println(p13);
System.out.println(p14);
//l
System.out.println();
Polynomial p15 = p1.exp(5);
System.out.println(p15);
System.out.println(p1.mult(p1).mult(p1).mult(p1).mult(p1));
//m
System.out.println();
System.out.println(p1.apply(0));
System.out.println(p1.apply(1));
System.out.println(p1.apply(2));
System.out.println(p6.apply(4));
}
}

View File

@@ -0,0 +1,69 @@
package polynomial;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class PolynomialTest {
private final Polynomial p1 = new Polynomial(new int[]{1, 2, 3, 4});
private final Polynomial p2 = new Polynomial(new int[]{0, 0, 0});
private final Polynomial p3 = new Polynomial(new int[]{3});
private final Polynomial p4 = new Polynomial();
private final Polynomial p5 = Polynomial.constant(5);
private final Polynomial p6 = new Polynomial(new int[]{0, -1, 2, +3, -2, 0, 1});
@Test
public void testDegree() {
assertEquals(3, p1.getDegree());
assertEquals(0, p2.getDegree());
assertEquals(0, p3.getDegree());
}
@Test
public void testToString() {
assertEquals("4*x^3 + 3*x^2 + 2*x + 1", p1.toString());
assertEquals("0", p2.toString());
assertEquals("3", p3.toString());
assertEquals("x", p4.toString());
assertEquals("5", p5.toString());
assertEquals("x^6 - 2*x^4 + 3*x^3 + 2*x^2 - x", p6.toString());
}
@Test
public void testAdd() {
assertEquals("x^6 - 2*x^4 + 7*x^3 + 5*x^2 + x + 1", p1.add(p6).toString());
}
@Test
public void testSub() {
assertEquals("-x^6 + 2*x^4 + x^3 + x^2 + 3*x + 1", p1.sub(p6).toString());
assertEquals("0", p6.sub(p6).toString());
}
@Test
public void testMult() {
assertEquals("16*x^6 + 24*x^5 + 25*x^4 + 20*x^3 + 10*x^2 + 4*x + 1", p1.mult(p1).toString());
assertEquals("4*x^9 + 3*x^8 - 6*x^7 + 7*x^6 + 13*x^5 + 6*x^4 + 4*x^3 - x", p1.mult(p6).toString());
}
@Test
public void testOverloaded() {
assertEquals("4*x^3 + 3*x^2 + 2*x + 6", p1.add(5).toString());
assertEquals("x^6 - 2*x^4 + 3*x^3 + 2*x^2 - x - 5", p6.sub(5).toString());
assertEquals("20*x^3 + 15*x^2 + 10*x + 5", p1.mult(5).toString());
}
@Test
public void testExp() {
assertEquals("1024*x^15 + 3840*x^14 + 8320*x^13 + 13280*x^12 + 16660*x^11 + 17203*x^10 + 14970*x^9 + 11085*x^8 + 7040*x^7 + 3830*x^6 + 1772*x^5 + 690*x^4 + 220*x^3 + 55*x^2 + 10*x + 1",
p1.exp(5).toString());
}
@Test
public void testApply() {
assertEquals(1, p1.apply(0));
assertEquals(10, p1.apply(1));
assertEquals(49, p1.apply(2));
assertEquals(3804, p6.apply(4));
}
}

54
uebung04/chess/Board.java Normal file
View File

@@ -0,0 +1,54 @@
package chess;
import java.util.ArrayList;
public class Board {
private final Piece[][] field = new Piece[8][8];
private final ArrayList<Piece> pieces = new ArrayList<Piece>();
void add(Piece piece) {
if (piece.getBoard() != this)
throw new IllegalArgumentException("wrong board");
final Piece existing = pieceAt(piece.getRow(), piece.getCol());
if (existing != null)
throw new IllegalArgumentException("already occupied by " +
existing.toString());
field[piece.getRow() - 1][piece.getCol() - 1] = piece;
pieces.add(piece);
}
public void printBoard() {
System.out.println(" 1 2 3 4 5 6 7 8");
System.out.println(" +---+---+---+---+---+---+---+---+");
for (int row = 1; row <= 8; row++) {
System.out.print("" + row + " ");
for (int col = 1; col <= 8; col++) {
final Piece p = pieceAt(row, col);
final char c = p == null ? ' ' : p.charRep();
System.out.print("| " + c + " ");
}
System.out.println("|");
System.out.println(" +---+---+---+---+---+---+---+---+");
}
}
public Piece pieceAt(int row, int col) {
return field[row - 1][col - 1];
}
public void check() {
for (Piece p1 : pieces) {
System.out.println(p1.toString());
for (Piece p2 : pieces)
if (p1 != p2)
if (p1.canCapture(p2))
System.out.println(" can capture " + p2.toString());
else
System.out.println(" cannot capture " + p2.toString());
}
}
public String toString() {
return pieces.toString();
}
}

15
uebung04/chess/Chess.java Normal file
View File

@@ -0,0 +1,15 @@
package chess;
public class Chess {
public static void main(String[] args) {
final Board board = new Board();
final Piece p1 = new Queen(Color.black, board, 1, 4);
final Piece p2 = new Queen(Color.white, board, 8, 4);
final Piece p3 = new Knight(Color.white, board, 3, 3);
final Piece p4 = new Knight(Color.black, board, 6, 4);
System.out.println(board);
System.out.println(board.toString());
board.printBoard();
board.check();
}
}

View File

@@ -0,0 +1,168 @@
package chess;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.Assert.assertEquals;
public class ChessTest {
// Das Zeilenende wird auf Windows-Systemen anders codiert als auf Nicht-Windows-Systemen.
// Die folgende Zeile ermittelt die aktuelle Codierung.
// EOL = "End of Line"
private static final String LS = System.lineSeparator();
private static final String HE = " 1 2 3 4 5 6 7 8" + LS;
private static final String LI = " +---+---+---+---+---+---+---+---+" + LS;
private static final String EMP = " | | | | | | | | |" + LS;
private static final String WK1 = " | n | | | | | | | |" + LS;
private static final String WQ3 = " | | | q | | | | | |" + LS;
private static final String CO1 = " | | | n | | N | Q | | |" + LS;
private static final String INIT_BOARD = board(EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP);
private static final String WK61_BOARD = board(EMP, EMP, EMP, EMP, EMP, WK1, EMP, EMP);
private static final String APP1_BOARD = board(EMP, EMP, EMP, EMP, WQ3, CO1, EMP, EMP);
private static final String CHECK_TEXT1 = "white queen at (4, 2)" + LS +
" cannot capture black queen at (8, 6)" + LS +
" cannot capture white knight at (6, 4)" + LS +
"black queen at (8, 6)" + LS +
" cannot capture white queen at (4, 2)" + LS +
" can capture white knight at (6, 4)" + LS +
"white knight at (6, 4)" + LS +
" cannot capture white queen at (4, 2)" + LS +
" cannot capture black queen at (8, 6)" + LS;
private static final String CHECK_TEXT2 = "white knight at (6, 3)" + LS +
" cannot capture black knight at (6, 5)" + LS +
" cannot capture black queen at (6, 6)" + LS +
" cannot capture white queen at (5, 3)" + LS +
"black knight at (6, 5)" + LS +
" cannot capture white knight at (6, 3)" + LS +
" cannot capture black queen at (6, 6)" + LS +
" can capture white queen at (5, 3)" + LS +
"black queen at (6, 6)" + LS +
" cannot capture white knight at (6, 3)" + LS +
" cannot capture black knight at (6, 5)" + LS +
" cannot capture white queen at (5, 3)" + LS +
"white queen at (5, 3)" + LS +
" cannot capture white knight at (6, 3)" + LS +
" cannot capture black knight at (6, 5)" + LS +
" cannot capture black queen at (6, 6)" + LS;
private static String board(String... s) {
if (s.length != 8)
throw new IllegalArgumentException("Expected 8, but got " + s.length + " arguments");
StringBuilder sb = new StringBuilder();
sb.append(HE).append(LI);
for (int i = 1; i <= 8; i++)
sb.append(i).append(s[i - 1]).append(LI);
return sb.toString();
}
private Board board;
private final ByteArrayOutputStream printed = new ByteArrayOutputStream();
private final PrintStream printStream = new PrintStream(printed, true);
@Before
public void setup() {
board = new Board();
printed.reset();
System.setOut(printStream);
}
@After
public void tearDown() {
printStream.close();
}
@Test
public void testEmpty() {
board.printBoard();
assertEquals(INIT_BOARD, printed.toString());
}
@Test(expected = IllegalArgumentException.class)
public void testInvalidPos1() {
new Knight(Color.white, board, 0, 4);
}
@Test(expected = IllegalArgumentException.class)
public void testInvalidPos2() {
new Queen(Color.white, board, 0, 4);
}
@Test
public void testWhiteknight() {
final Piece knight = new Knight(Color.white, board, 6, 1);
checkPieces(1);
assertEquals(knight, board.pieceAt(6, 1));
board.printBoard();
assertEquals(WK61_BOARD, printed.toString());
}
@Test
public void test3Pieces() {
final Piece queen1 = new Queen(Color.white, board, 4, 2);
final Piece queen2 = new Queen(Color.black, board, 8, 6);
final Piece knight = new Knight(Color.white, board, 6, 4);
Assert.assertFalse(queen1.canCapture(queen2));
Assert.assertFalse(queen1.canCapture(knight));
Assert.assertFalse(queen2.canCapture(queen1));
Assert.assertTrue(queen2.canCapture(knight));
Assert.assertFalse(knight.canCapture(queen1));
Assert.assertFalse(knight.canCapture(queen2));
checkPieces(3);
assertEquals(queen1, board.pieceAt(4, 2));
assertEquals(queen2, board.pieceAt(8, 6));
assertEquals(knight, board.pieceAt(6, 4));
board.check();
assertEquals(CHECK_TEXT1, printed.toString());
}
@Test
public void test4Pieces() {
final Piece knight1 = new Knight(Color.white, board, 6, 3);
final Piece knight2 = new Knight(Color.black, board, 6, 5);
final Piece queen1 = new Queen(Color.black, board, 6, 6);
final Piece queen2 = new Queen(Color.white, board, 5, 3);
checkPieces(4);
assertEquals(knight1, board.pieceAt(6, 3));
assertEquals(knight2, board.pieceAt(6, 5));
assertEquals(queen1, board.pieceAt(6, 6));
assertEquals(queen2, board.pieceAt(5, 3));
board.printBoard();
assertEquals(APP1_BOARD, printed.toString());
Assert.assertFalse(knight1.canCapture(knight2));
Assert.assertFalse(knight1.canCapture(queen1));
Assert.assertFalse(knight1.canCapture(queen2));
Assert.assertFalse(knight2.canCapture(knight1));
Assert.assertFalse(knight2.canCapture(queen1));
Assert.assertTrue(knight2.canCapture(queen2));
Assert.assertFalse(queen1.canCapture(knight1));
Assert.assertFalse(queen1.canCapture(knight2));
Assert.assertFalse(queen1.canCapture(queen2));
Assert.assertFalse(queen2.canCapture(knight1));
Assert.assertFalse(queen2.canCapture(knight2));
Assert.assertFalse(queen2.canCapture(queen1));
printed.reset();
board.check();
assertEquals(CHECK_TEXT2, printed.toString());
}
private void checkPieces(int expectedNumPieces) {
int numPieces = 0;
for (int row = 1; row < 9; row++)
for (int col = 1; col < 9; col++)
if (board.pieceAt(row, col) != null)
numPieces++;
assertEquals(expectedNumPieces, numPieces);
}
@Test
public void testRunWithErrors() {
final Piece rook = new Rook(Color.white, board, 1, 1);
assertEquals(rook, board.pieceAt(1, 1));
}
}

View File

@@ -0,0 +1,5 @@
package chess;
public enum Color {
black, white
}

View File

@@ -0,0 +1,28 @@
package chess;
import static java.lang.Math.abs;
public class Knight extends Piece {
public Knight(Color color, Board board, int row, int col) {
super(color, board, row, col);
}
@Override
public char charRep() {
return getColor() == Color.white ? 'n' : 'N';
}
@Override
public String toString() {
return "" + getColor() + " knight at (" + getRow() + ", " + getCol() + ")";
}
@Override
public boolean canCapture(Piece other) {
if (getBoard() != other.getBoard() || getColor() == other.getColor())
return false;
final int dr = abs(getRow() - other.getRow());
final int dc = abs(getCol() - other.getCol());
return dr == 2 && dc == 1 || dr == 1 && dc == 2;
}
}

38
uebung04/chess/Piece.java Normal file
View File

@@ -0,0 +1,38 @@
package chess;
public abstract class Piece {
private Color color;
private Board board;
private int row;
private int col;
protected Piece(Color color, Board board, int row, int col) {
if (row < 1 || row > 8 || col < 1 || col > 8)
throw new IllegalArgumentException("Invalid pos " + row + "/" + col);
this.color = color;
this.board = board;
this.row = row;
this.col = col;
board.add(this);
}
public Color getColor() {
return color;
}
public Board getBoard() {
return board;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public abstract char charRep();
public abstract boolean canCapture(Piece other);
}

40
uebung04/chess/Queen.java Normal file
View File

@@ -0,0 +1,40 @@
package chess;
import static java.lang.Integer.signum;
import static java.lang.Math.abs;
public class Queen extends Piece {
public Queen(Color color, Board board, int row, int col) {
super(color, board, row, col);
}
@Override
public char charRep() {
return getColor() == Color.white ? 'q' : 'Q';
}
@Override
public String toString() {
return "" + getColor() + " queen at (" + getRow() + ", " + getCol() + ")";
}
@Override
public boolean canCapture(Piece other) {
if (getBoard() != other.getBoard() || getColor() == other.getColor())
return false;
if (other.getRow() != getRow() &&
other.getCol() != getCol() &&
abs(other.getRow() - getRow()) != abs(other.getCol() - getCol()))
return false;
final int dr = signum(other.getRow() - getRow());
final int dc = signum(other.getCol() - getCol());
int r = getRow() + dr;
int c = getCol() + dc;
while (r != other.getRow() || c != other.getCol()) {
if (getBoard().pieceAt(r, c) != null) return false;
r += dr;
c += dc;
}
return true;
}
}

39
uebung04/chess/Rook.java Normal file
View File

@@ -0,0 +1,39 @@
package chess;
import static java.lang.Integer.signum;
import static java.lang.Math.abs;
public class Rook extends Piece{
public Rook(Color color, Board board, int row, int col) {
super(color, board, row, col);
}
@Override
public char charRep() {
return getColor() == Color.white ? 'r' : 'R';
}
@Override
public String toString() {
return "" + getColor() + " rook at (" + getRow() + ", " + getCol() + ")";
}
@Override
public boolean canCapture(Piece other) {
if (getBoard() != other.getBoard() || getColor() == other.getColor())
return false;
if (other.getRow() != getRow() &&
other.getCol() != getCol())
return false;
final int dr = signum(other.getRow() - getRow());
final int dc = signum(other.getCol() - getCol());
int r = getRow() + dr;
int c = getCol() + dc;
while (r != other.getRow() || c != other.getCol()) {
if (getBoard().pieceAt(r, c) != null) return false;
r += dr;
c += dc;
}
return true;
}
}

View File

@@ -0,0 +1,59 @@
package tournament;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public class ByeGame extends Game{
private final String player1;
private final Game ref;
ByeGame(String player1, Game ref) {
super();
this.ref = ref;
this.player1 = player1;
}
public Game getRef() {
return ref;
}
@Override
public String getPlayer1() {
return player1;
}
@Override
public String getPlayer2() {
return ref.getWinner();
}
@Override
public List<String> getAllPlayers() {
List<String> players = new ArrayList<String>();
players.add(player1);
players.addAll(ref.getAllPlayers());
return players;
}
@Override
public List<Game> getAllGames() {
List<Game> temp = new ArrayList<>();
temp.add(this);
temp.addAll(ref.getAllGames());
return temp;
}
@Override
public List<String> getRemaningPlayers() {
if (winner != null) {
return Arrays.asList(winner);
} else {
List<String> players = new ArrayList<>();
players.add(player1);
players.addAll(ref.getRemaningPlayers());
return players;
}
}
}

View File

@@ -0,0 +1,51 @@
package tournament;
import java.util.List;
public abstract class Game {
private static int counter = 0;
protected final int id;
//null nicht notwendig
protected String winner = null;
protected Game() {
this.id = counter++;
}
public int getId(){
return this.id;
}
public String getWinner(){
return this.winner;
}
public void setWinner(String winner) {
if (this.winner != null)
throw new IllegalStateException("winner already set");
if (getPlayer1() == null || getPlayer2() == null)
throw new IllegalStateException("Opponents are not yet known");
if (getPlayer1().equals(winner) || getPlayer2().equals(winner))
this.winner = winner;
else
throw new IllegalArgumentException("Unknown player " + winner);
}
public abstract String getPlayer1();
public abstract String getPlayer2();
public abstract List<String> getAllPlayers();
public abstract List<String> getRemaningPlayers();
public abstract List<Game> getAllGames();
public String toString() {
return "Game: " + getId() + " Player: "+ getPlayer1()+ " vs Player: " + getPlayer2() + "Winner is: "+ getWinner();
}
}

View File

@@ -0,0 +1,52 @@
package tournament;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class OrdinaryGame extends Game{
private Game Player1;
private Game Player2;
public OrdinaryGame(Game Player1, Game Player2) {
this.Player1 = Player1;
this.Player2 = Player2;
}
@Override
public String getPlayer1() {
return Player1.getWinner();
}
@Override
public String getPlayer2() {
return Player2.getWinner();
}
@Override
public List<String> getAllPlayers() {
List<String> temp = new ArrayList<>();
temp.addAll(Player1.getAllPlayers());
temp.addAll(Player2.getAllPlayers());
return temp;
}
@Override
public List<String> getRemaningPlayers() {
if(this.getWinner() != null)
return new ArrayList<>(Arrays.asList(this.getWinner()));
List<String> temp = new ArrayList<>();
temp.addAll(Player1.getRemaningPlayers());
temp.addAll(Player2.getRemaningPlayers());
return temp;
}
@Override
public List<Game> getAllGames() {
List<Game> temp = new ArrayList<>();
temp.addAll(Player1.getAllGames());
temp.addAll(Player2.getAllGames());
return temp;
}
}

View File

@@ -0,0 +1,49 @@
package tournament;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SeededGame extends Game{
private String player1;
private String player2;
public SeededGame(String player1, String player2) {
super();
this.player1 = player1;
this.player2 = player2;
}
@Override
public String getPlayer1() {
return player1;
}
@Override
public String getPlayer2() {
return player2;
}
@Override
public List<String> getAllPlayers() {
//return new ArrayList<String>(Arrays.asList(player1, player2));
return List.of(player1, player2);
}
@Override
public List<String> getRemaningPlayers() {
if(this.getWinner() != null) {
return new ArrayList<>(Arrays.asList(this.getWinner()));
}
return List.of(player1, player2);
}
@Override
public List<Game> getAllGames() {
return List.of(this);
}
}

View File

@@ -0,0 +1,19 @@
package tournament;
import java.util.List;
public class Tournament {
private final Game finale;
private final String name;
public Tournament(String name, Game finale)
{
this.finale = finale;
this.name = name;
}
public List<String> getAllPlayers() {return finale.getAllPlayers();}
public List<Game> getAllGames() {return finale.getAllGames();}
public List<String> getRemainingPlayers() {return finale.getRemaningPlayers();}
}

View File

@@ -0,0 +1,134 @@
package tournament;
import org.junit.Before;
import org.junit.Test;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
public class TournamentTest {
private static final String BORGHOFF = "Prof. Borghoff";
private static final String HOMMEL = "Prof. Hommel";
private static final String KOCH = "Prof. Koch";
private static final String MINAS = "Prof. Minas";
private static final String BUCHENRIEDER = "Prof. Buchenrieder";
private static final String DREO = "Prof. Dreo";
private static final String ROSE = "Prof. Rose";
private static final String SIEGLE = "Prof. Siegle";
private static final String TEEGE = "Prof. Teege";
private static final Set<String> SET1 = Set.of(BORGHOFF, HOMMEL, KOCH, MINAS, BUCHENRIEDER, DREO, ROSE, SIEGLE, TEEGE);
private static final Set<String> SET2 = Set.of(BORGHOFF, MINAS, SIEGLE, HOMMEL, DREO);
private static final Set<String> SET3 = Set.of(BORGHOFF, MINAS, SIEGLE);
private static final Set<String> SET4 = Set.of(MINAS, SIEGLE);
private static final Set<String> SET5 = Set.of(MINAS);
private Tournament tournament;
private Game r1s1;
private Game r1s2;
private Game r1s3;
private Game r1s4;
private Game r2s1;
private Game r2s2;
private Game r3s1;
private Game r4s1;
@Before
public void setup() {
r1s1 = new SeededGame(HOMMEL, KOCH);
r1s2 = new SeededGame(MINAS, BUCHENRIEDER);
r1s3 = new SeededGame(DREO, ROSE);
r1s4 = new SeededGame(SIEGLE, TEEGE);
r2s1 = new ByeGame(BORGHOFF, r1s1);
r2s2 = new OrdinaryGame(r1s2, r1s3);
r3s1 = new OrdinaryGame(r2s1, r2s2);
r4s1 = new OrdinaryGame(r3s1, r1s4);
tournament = new Tournament("UniBw Sportschießen", r4s1);
}
@Test
public void testBeforeFirstRound() {
assertEquals(SET1, asSet(tournament.getAllPlayers()));
assertEquals(SET1, asSet(tournament.getRemainingPlayers()));
}
@Test
public void testFirstRound() {
r1s1.setWinner(HOMMEL);
r1s2.setWinner(MINAS);
r1s3.setWinner(DREO);
r1s4.setWinner(SIEGLE);
assertEquals(SET1, asSet(tournament.getAllPlayers()));
assertEquals(SET2, asSet(tournament.getRemainingPlayers()));
}
@Test
public void testSecondRound() {
r1s1.setWinner(HOMMEL);
r1s2.setWinner(MINAS);
r1s3.setWinner(DREO);
r1s4.setWinner(SIEGLE);
r2s1.setWinner(BORGHOFF);
r2s2.setWinner(MINAS);
assertEquals(SET1, asSet(tournament.getAllPlayers()));
assertEquals(SET3, asSet(tournament.getRemainingPlayers()));
}
@Test
public void testThirdRound() {
r1s1.setWinner(HOMMEL);
r1s2.setWinner(MINAS);
r1s3.setWinner(DREO);
r1s4.setWinner(SIEGLE);
r2s1.setWinner(BORGHOFF);
r2s2.setWinner(MINAS);
r3s1.setWinner(MINAS);
assertEquals(SET1, asSet(tournament.getAllPlayers()));
assertEquals(SET4, asSet(tournament.getRemainingPlayers()));
}
@Test
public void testCompleteTournament() {
r1s1.setWinner(HOMMEL);
r1s2.setWinner(MINAS);
r1s3.setWinner(DREO);
r1s4.setWinner(SIEGLE);
r2s1.setWinner(BORGHOFF);
r2s2.setWinner(MINAS);
r3s1.setWinner(MINAS);
r4s1.setWinner(MINAS);
assertEquals(SET1, asSet(tournament.getAllPlayers()));
assertEquals(SET5, asSet(tournament.getRemainingPlayers()));
}
@Test
public void testException() {
r1s1.setWinner(HOMMEL);
r1s2.setWinner(MINAS);
r1s3.setWinner(DREO);
r1s4.setWinner(SIEGLE);
r2s1.setWinner(BORGHOFF);
r2s2.setWinner(MINAS);
r3s1.setWinner(MINAS);
assertThrows(IllegalArgumentException.class, () -> r4s1.setWinner(BUCHENRIEDER));
}
@Test(expected = IllegalStateException.class)
public void testException2() {
r4s1.setWinner(MINAS);
}
@Test
public void testException3() {
r1s1.setWinner(HOMMEL);
assertThrows(IllegalStateException.class, () -> r1s1.setWinner(HOMMEL));
}
private static <T> Set<T> asSet(Collection<T> collection) {
return new HashSet<>(collection);
}
}

View File

@@ -0,0 +1,43 @@
package logistics;
import logistics.material.BulletBelts;
import logistics.material.Grease;
import logistics.material.LiterDiesel;
import logistics.material.MetGallonsKerosene;
import logistics.material.Oil;
import logistics.material.RocketPods;
import logistics.material.ShellBatches;
public class Demo {
public static void main(String[] args) {
final Manager manager = new Manager();
manager.addVehicle(new Tank("Leo1", 50, 2, 3));
manager.addVehicle(new Tank("Leo2", 60, 3, 4));
manager.addVehicle(new Helicopter("Tiger1", 300, 2, 5));
manager.addVehicle(new Helicopter("Tiger2", 400, 7, 8));
System.out.println();
manager.showOverallNeed();
System.out.println();
manager.fillUpVehicles();
manager.showOverallNeed();
System.out.println();
manager.logTick(1);
System.out.println();
manager.showNeed(LiterDiesel.INSTANCE);
System.out.println();
manager.showNeed(MetGallonsKerosene.INSTANCE);
System.out.println();
manager.showNeed(BulletBelts.INSTANCE);
System.out.println();
manager.showNeed(RocketPods.INSTANCE);
System.out.println();
manager.showNeed(ShellBatches.INSTANCE);
System.out.println();
manager.showNeed(Oil.INSTANCE);
System.out.println();
manager.showNeed(Grease.INSTANCE);
System.out.println();
manager.showOverallNeed();
System.out.println();
}
}

View File

@@ -0,0 +1,50 @@
package logistics;
import logistics.material.BulletBelts;
import logistics.material.MetGallonsKerosene;
import logistics.material.RocketPods;
import logistics.quantities.NeedCollector;
import logistics.storage.FloatStorage;
import logistics.storage.IntStorage;
public class Helicopter extends Vehicle {
public IntStorage bullets;
public IntStorage rockets;
public FloatStorage tank;
public Helicopter(String name, float kerosene, int bulletBelts, int rocketPods) {
super(name);
this.tank = new FloatStorage(kerosene, MetGallonsKerosene.INSTANCE, 500);
bullets = new IntStorage(bulletBelts, BulletBelts.INSTANCE, 2);
rockets = new IntStorage(rocketPods, RocketPods.INSTANCE, 2);
}
@Override
public void reportNeeds(NeedCollector collector) {
oil.reportNeed(collector);
grease.reportNeed(collector);
tank.reportNeed(collector);
bullets.reportNeed(collector);
rockets.reportNeed(collector);
}
@Override
public void fillUpAll() {
oil.fillUp();
grease.fillUp();
tank.fillUp();
bullets.fillUp();
rockets.fillUp();
}
@Override
public void consumeAll(int intensityRate) {
tank.consume(intensityRate * 200);
bullets.consume(intensityRate);
rockets.consume(intensityRate);
oil.consume(1);
grease.consume(1);
}
}

View File

@@ -0,0 +1,54 @@
package logistics;
import logistics.quantities.FloatUnit;
import logistics.quantities.IntUnit;
import logistics.quantities.NeedCollector;
import java.util.ArrayList;
import java.util.List;
public class Manager {
private final List<Vehicle> vehicles = new ArrayList<>();
public void addVehicle(Vehicle vehicle) {
vehicles.add(vehicle);
}
public void fillUpVehicles() {
for (Vehicle vehicle : vehicles) {
vehicle.fillUpAll();
}
}
public void logTick(int intensityRate) {
for (Vehicle vehicle : vehicles) {
vehicle.consumeAll(intensityRate);
}
}
public NeedCollector collectNeeds() {
NeedCollector collector = new NeedCollector();
for (Vehicle vehicle : vehicles) {
vehicle.reportNeeds(collector);
}
return collector;
}
public void showOverallNeed() {
collectNeeds().show();
}
public void showNeed(IntUnit unit) {
NeedCollector collector = collectNeeds();
System.out.println(collector.getNeed(unit));
}
public void showNeed(FloatUnit unit) {
NeedCollector collector = collectNeeds();
System.out.println(collector.getNeed(unit));
}
}

View File

@@ -0,0 +1,47 @@
package logistics;
import logistics.material.BulletBelts;
import logistics.material.LiterDiesel;
import logistics.material.ShellBatches;
import logistics.quantities.NeedCollector;
import logistics.storage.FloatStorage;
import logistics.storage.IntStorage;
public class Tank extends Vehicle {
public final IntStorage bullets;
public final IntStorage shells;
public final FloatStorage tank;
public Tank(String name, float diesel,int bulletBelts, int shellBatches) {
super(name);
bullets = new IntStorage(bulletBelts, BulletBelts.INSTANCE, 10);
shells = new IntStorage(shellBatches, ShellBatches.INSTANCE, 10);
tank = new FloatStorage(diesel, LiterDiesel.INSTANCE, 1200);
}
@Override
public void reportNeeds(NeedCollector collector) {
super.reportNeedsSuper(collector);
bullets.reportNeed(collector);
shells.reportNeed(collector);
tank.reportNeed(collector);
}
@Override
public void fillUpAll() {
super.fillUpAllSuper();
bullets.fillUp();
shells.fillUp();
tank.fillUp();
}
@Override
public void consumeAll(int intesity) {
super.consumeAll();
bullets.consume(intesity*2);
tank.consume(intesity*180f);
shells.consume(intesity*2);
}
}

View File

@@ -0,0 +1,45 @@
package logistics;
import logistics.material.BulletBelts;
import logistics.material.LiterDiesel;
import logistics.quantities.NeedCollector;
import logistics.storage.FloatStorage;
import logistics.storage.IntStorage;
public class Truck extends Vehicle {
private final FloatStorage tank;
private final IntStorage bullets;
public Truck(String name, float diesel, int bullets) {
super(name);
this.tank = new FloatStorage(diesel, LiterDiesel.INSTANCE, 180);
this.bullets = new IntStorage(bullets, BulletBelts.INSTANCE, 3);
}
@Override
public void consumeAll(int intensity) {
oil.consume(1);
grease.consume(1);
tank.consume(intensity * 25);
bullets.consume(intensity * 2);
}
@Override
public void reportNeeds(NeedCollector collector) {
oil.reportNeed(collector);
grease.reportNeed(collector);
tank.reportNeed(collector);
bullets.reportNeed(collector);
}
@Override
public void fillUpAll() {
oil.fillUp();
grease.fillUp();
tank.fillUp();
bullets.fillUp();
}
}

View File

@@ -0,0 +1,43 @@
package logistics;
import logistics.material.Grease;
import logistics.material.Oil;
import logistics.quantities.NeedCollector;
import logistics.storage.IntStorage;
public abstract class Vehicle {
public String name;
public IntStorage oil;
public IntStorage grease;
public Vehicle(String name) {
this.name = name;
this.oil = new IntStorage(0, Oil.INSTANCE, 3);
this.grease = new IntStorage(0, Grease.INSTANCE, 3);
}
public abstract void reportNeeds(NeedCollector collector);
public abstract void fillUpAll();
public abstract void consumeAll(int intensity);
public void consumeAll(){
oil.consume(1);
grease.consume(1);
}
public void fillUpAllSuper(){
oil.fillUp();
grease.fillUp();
}
public void reportNeedsSuper(NeedCollector collector){
oil.reportNeed(collector);
grease.reportNeed(collector);
}
}

View File

@@ -0,0 +1,14 @@
package logistics.material;
import logistics.quantities.IntUnit;
public class BulletBelts implements IntUnit{
public static BulletBelts INSTANCE = new BulletBelts();
private BulletBelts() {}
@Override
public String toString() {
return "belts of 7.62 bullets";
}
}

View File

@@ -0,0 +1,15 @@
package logistics.material;
import logistics.quantities.IntUnit;
public class Grease implements IntUnit{
public static Grease INSTANCE = new Grease();
private Grease() {}
@Override
public String toString() {
return "units of grease";
}
}

View File

@@ -0,0 +1,14 @@
package logistics.material;
import logistics.quantities.FloatUnit;
public class LiterDiesel implements FloatUnit{
public static LiterDiesel INSTANCE = new LiterDiesel();
private LiterDiesel() {}
@Override
public String toString() {
return "liters of diesel";
}
}

View File

@@ -0,0 +1,14 @@
package logistics.material;
import logistics.quantities.FloatUnit;
public class MetGallonsKerosene implements FloatUnit{
public static MetGallonsKerosene INSTANCE = new MetGallonsKerosene();
private MetGallonsKerosene() {}
@Override
public String toString() {
return "met gallons of kerosene";
}
}

View File

@@ -0,0 +1,14 @@
package logistics.material;
import logistics.quantities.IntUnit;
public class Oil implements IntUnit{
public static Oil INSTANCE = new Oil();
private Oil() {}
@Override
public String toString() {
return "units of oil";
}
}

View File

@@ -0,0 +1,14 @@
package logistics.material;
import logistics.quantities.IntUnit;
public class RocketPods implements IntUnit{
public static RocketPods INSTANCE = new RocketPods();
private RocketPods() {}
@Override
public String toString() {
return "pods of 70mm rockets";
}
}

View File

@@ -0,0 +1,14 @@
package logistics.material;
import logistics.quantities.IntUnit;
public class ShellBatches implements IntUnit{
public static ShellBatches INSTANCE = new ShellBatches();
private ShellBatches() {}
@Override
public String toString() {
return "batches of 120mm shells";
}
}

View File

@@ -0,0 +1,5 @@
package logistics.quantities;
public interface FloatUnit {
}

View File

@@ -0,0 +1,5 @@
package logistics.quantities;
public interface IntUnit {
}

View File

@@ -0,0 +1,43 @@
package logistics.quantities;
import java.util.HashMap;
import java.util.Map;
public class NeedCollector {
private Map<IntUnit, Integer> intNeeded = new HashMap<>();
private Map<FloatUnit, Float> floatNeeded;
public NeedCollector() {
// wird bei Deklaration initialisiert
//intNeeded = new HashMap<>();
floatNeeded = new HashMap<>();
}
public void add(int amount, IntUnit unit) {
if (intNeeded.containsKey(unit)) {
intNeeded.put(unit, intNeeded.get(unit) + amount);
}
else
intNeeded.put(unit, amount);
}
public void add(float amount, FloatUnit unit) {
if (floatNeeded.containsKey(unit))
floatNeeded.put(unit, floatNeeded.get(unit) + amount);
else
floatNeeded.put(unit, amount);
}
public int getNeed(IntUnit unit) {
return intNeeded.get(unit);
}
public float getNeed(FloatUnit unit) {
return floatNeeded.get(unit);
}
public void show() {
System.out.println(intNeeded);
System.out.println(floatNeeded);
}
}

View File

@@ -0,0 +1,59 @@
package logistics.storage;
import logistics.quantities.FloatUnit;
import logistics.quantities.NeedCollector;
public class FloatStorage {
private float stored;
private final FloatUnit unit;
private final float max;
public FloatStorage(float stored, FloatUnit unit, float max) {
if (stored < 0 || max < 0)
throw new IllegalArgumentException("negative");
this.stored = stored;
this.unit = unit;
this.max = max;
}
public float consume(float amount) {
if (amount < 0)
throw new IllegalArgumentException("negative");
amount = Math.min(amount, stored);
stored -= amount;
return amount;
}
public void fill(float amount) {
stored = Math.min(stored + amount, max);
}
public void fillUp() {
stored = max;
}
public void reportNeed(NeedCollector collector) {
collector.add(max - stored, unit);
}
public float getStored() {
return stored;
}
public FloatUnit getUnit() {
return unit;
}
public float getMax() {
return max;
}
@Override
public String toString() {
return "storage with " + stored + " of " + max + " " + unit;
}
}

View File

@@ -0,0 +1,60 @@
package logistics.storage;
import logistics.quantities.IntUnit;
import logistics.quantities.NeedCollector;
public class IntStorage {
private int stored;
private final IntUnit unit;
private final int max;
public IntStorage(int stored, IntUnit unit, int max) {
if (stored < 0 || max < 0)
throw new IllegalArgumentException("negative");
this.stored = Math.min(stored, max);
this.unit = unit;
this.max = max;
}
public int consume(int amount) {
if (amount < 0)
throw new IllegalArgumentException("negative");
int result = Math.min(amount, stored);
stored -= result;
return result;
}
public void fill(int amount) {
if (amount < 0) throw new IllegalArgumentException("negative");
stored = Math.min(stored + amount, max);
}
public void fillUp() {
stored = max;
}
public void reportNeed(NeedCollector collector) {
collector.add(max - stored, unit);
}
public int getStored() {
return stored;
}
public int getMax() {
return max;
}
public IntUnit getUnit() {
return unit;
}
@Override
public String toString() {
return "storage with " + stored + " of " + max + " " + unit;
}
}

View File

@@ -0,0 +1,67 @@
package logistics;
import logistics.material.BulletBelts;
import logistics.material.Grease;
import logistics.material.LiterDiesel;
import logistics.material.MetGallonsKerosene;
import logistics.material.Oil;
import logistics.material.RocketPods;
import logistics.material.ShellBatches;
import logistics.quantities.NeedCollector;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ManagerTest {
private static final float EPS = 1e-5f;
private Manager manager;
@Before
public void setup() {
manager = new Manager();
manager.addVehicle(new Tank("Leo1", 50, 2, 3));
manager.addVehicle(new Tank("Leo2", 60, 3, 4));
manager.addVehicle(new Helicopter("Tiger1", 300, 2, 5));
manager.addVehicle(new Helicopter("Tiger2", 400, 7, 8));
}
@Test
public void testInitialNeed() {
final NeedCollector collector = manager.collectNeeds();
assertEquals(4, collector.getNeed(RocketPods.INSTANCE));
assertEquals(12, collector.getNeed(Oil.INSTANCE));
assertEquals(24, collector.getNeed(BulletBelts.INSTANCE));
assertEquals(20, collector.getNeed(ShellBatches.INSTANCE));
assertEquals(12, collector.getNeed(Grease.INSTANCE));
assertEquals(1000f, collector.getNeed(MetGallonsKerosene.INSTANCE), EPS);
assertEquals(2400f, collector.getNeed(LiterDiesel.INSTANCE), EPS);
}
@Test
public void testNeedAfterFillUp() {
manager.fillUpVehicles();
final NeedCollector collector = manager.collectNeeds();
assertEquals(0, collector.getNeed(RocketPods.INSTANCE));
assertEquals(0, collector.getNeed(Oil.INSTANCE));
assertEquals(0, collector.getNeed(BulletBelts.INSTANCE));
assertEquals(0, collector.getNeed(ShellBatches.INSTANCE));
assertEquals(0, collector.getNeed(Grease.INSTANCE));
assertEquals(0f, collector.getNeed(MetGallonsKerosene.INSTANCE), EPS);
assertEquals(0f, collector.getNeed(LiterDiesel.INSTANCE), EPS);
}
@Test
public void testNeedAfterLogTick() {
manager.fillUpVehicles();
manager.logTick(1);
final NeedCollector collector = manager.collectNeeds();
assertEquals(2, collector.getNeed(RocketPods.INSTANCE));
assertEquals(4, collector.getNeed(Oil.INSTANCE));
assertEquals(6, collector.getNeed(BulletBelts.INSTANCE));
assertEquals(4, collector.getNeed(ShellBatches.INSTANCE));
assertEquals(4, collector.getNeed(Grease.INSTANCE));
assertEquals(400f, collector.getNeed(MetGallonsKerosene.INSTANCE), EPS);
assertEquals(360f, collector.getNeed(LiterDiesel.INSTANCE), EPS);
}
}

View File

@@ -0,0 +1,80 @@
package logistics.storage;
import logistics.material.Oil;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class IntStorageTest {
private static final int MAX = 3;
private IntStorage intStorage;
@Before
public void setup() {
intStorage = new IntStorage(0, Oil.INSTANCE, MAX);
}
@Test
public void testEmptyStorage() {
assertEquals(0, intStorage.getStored());
}
@Test
public void testGetStorage() {
intStorage.fill(1);
assertEquals(1, intStorage.getStored());
}
@Test
public void testGetMax() {
assertEquals(MAX, intStorage.getMax());
}
@Test
public void testFillUp() {
intStorage.fillUp();
assertEquals(MAX, intStorage.getStored());
}
@Test
public void testUpperBound() {
intStorage.fill(MAX + 1);
assertEquals(MAX, intStorage.getStored());
}
@Test
public void testConsume() {
intStorage.fillUp();
assertEquals(MAX, intStorage.getStored());
assertEquals(MAX, intStorage.consume(MAX));
}
@Test
public void testLowerBound() {
intStorage.consume(1);
assertEquals(0, intStorage.getStored());
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalFill() {
intStorage.fill(-1);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalConsume() {
intStorage.consume(-1);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalLowerBoundConstructor() {
new IntStorage(-1, Oil.INSTANCE, MAX);
}
@Test
public void testUpperBoundConstructor() {
final IntStorage storage = new IntStorage(MAX + 1, Oil.INSTANCE, MAX);
assertEquals(MAX, storage.getStored());
}
}

View File

@@ -0,0 +1,123 @@
package sudoku;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import static java.util.Collections.emptySet;
/**
* A field of a Sudoku board.
*/
public class Field {
private static final int DEP_LENGTH = 2 * Sudoku.SIZE + 2;
public final Sudoku sudoku;
public final int x;
public final int y;
private Value value;
private List<Field> dependents;
private final Set<Value> domain = EnumSet.allOf(Value.class);
/**
* Creates a field of the specified Sudoku board at the specified coordinates.
*
* @param sudoku the Sudoku board
* @param x x coordinate (0 <= x < 9)
* @param y y coordinate (0 <= y < 9)
*/
public Field(Sudoku sudoku, int x, int y) {
if (x < 0 || x >= Sudoku.SIZE || y < 0 || y >= Sudoku.SIZE)
throw new IllegalArgumentException("Illegal coordinates " + x + ", " + y);
this.sudoku = sudoku;
this.x = x;
this.y = y;
}
/**
* Returns the value of this field, or null if it is empty.
*
* @return the value of this field, or null if it is empty.
*/
public Value getValue() {
return value;
}
/**
* Sets the value of this field
*
* @param v the value, or null if this field shall be empty.
*/
public void setValue(Value v) {
value = v;
}
/**
* Removes the value of this field so that it is empty.
*/
public void clearValue() {
setValue(null);
}
/**
* Checks whether this field is empty.
*
* @return true iff this field is empty.
*/
public boolean isEmpty() {
return value == null;
}
/**
* Returns a string representation of this field. As a matter of fact,
* its value is returned, or "." if it is empty.
*
* @return a string representation of this field.
*/
public String toString() {
return isEmpty() ? "." : value.toString();
}
/**
* Returns the set of all values possible for this field that do not violate the Sudoku constraints,
* if the field is empty. Otherwise, it returns the empty set. The returned set may be modified if
* the field is empty.
*
* @return the domain of this field.
*/
public Set<Value> getDomain() {
return isEmpty() ? domain : emptySet();
}
/**
* Returns the list of all other fields of this Sudoku field whose values are constrained by the
* value of this field.
*
* @return the list of all dependent fields.
*/
List<Field> getDependents() {
if (dependents == null) {
dependents = new ArrayList<>(DEP_LENGTH);
// fill the list with all fields in the given row and column
for (int i = 0; i < Sudoku.SIZE; i++) {
if (i != x)
dependents.add(sudoku.field(i, y));
if (i != y)
dependents.add(sudoku.field(x, i));
}
// determine the coordinates of the upper left corner of this field's block
final int x0 = 3 * (x / 3);
final int y0 = 3 * (y / 3);
// fill the list with the whole block
for (int x1 = x0; x1 < x0 + 3; x1++)
if (x1 != x)
for (int y1 = y0; y1 < y0 + 3; y1++)
if (y1 != y)
dependents.add(sudoku.field(x1, y1));
}
return dependents;
}
}

View File

@@ -0,0 +1,206 @@
package sudoku;
import java.util.ArrayList;
import java.util.List;
/**
* A Sudoku board as a matrix of {@linkplain sudoku.Field} instances.
* <p>
* Call {@linkplain #solve()} to solve it.
*/
public class Sudoku {
/**
* Edge size of the Sudoku board.
*/
public static final int SIZE = 9;
private static final String LS = System.lineSeparator();
private final Field[] board = new Field[SIZE * SIZE];
/**
* Creates a Sudoku board with all of its fields being empty.
*/
public Sudoku() {
for (int x = 0; x < SIZE; x++)
for (int y = 0; y < SIZE; y++)
board[SIZE * y + x] = new Field(this, x, y);
}
/**
* Returns the field at the specified coordinates
*
* @param x x coordinate (0 <= x < 9)
* @param y y coordinate (0 <= x < 9)
* @return the field at the specified coordinates
*/
public Field field(int x, int y) {
return board[SIZE * y + x];
}
/**
* Bulk operation to set values of the fields.
*
* @param values a sequence of integers where 0 means empty
*/
public void initialize(int... values) {
if (values.length != board.length)
throw new IllegalArgumentException("Wrong Sudoku board with " + values.length + " values");
for (int i = 0; i < values.length; i++)
if (values[i] != 0)
setValue(board[i], Value.of(values[i]));
}
/**
* Sets the specified value of the specified empty field
*
* @param field the empty field whose value is set
* @param value the value of the field
* @throws IllegalStateException if the field is not empty
* @throws IllegalArgumentException if setting this value would violate the Sudoku constraints
* @throws NullPointerException if the value is null
*/
public void setValue(Field field, Value value) {
if (value == null)
throw new NullPointerException("value is null");
if (!field.isEmpty())
throw new IllegalStateException("Value already set: " + field);
if (!field.getDomain().contains(value))
throw new IllegalArgumentException(value + " is not permitted");
for (Field dependent : field.getDependents())
dependent.getDomain().remove(value);
field.setValue(value);
}
/**
* Returns a string representation of this board.
*
* @return a string representation of this board
*/
public String toString() {
StringBuilder sb = new StringBuilder();
for (int y = 0; y < SIZE; y++) {
if (y == 3 || y == 6) sb.append("---------+---------+---------").append(LS);
for (int x = 0; x < SIZE; x++) {
if (x == 3 || x == 6) sb.append("|");
sb.append(" ").append(field(x, y)).append(" ");
}
sb.append(LS);
}
return sb.toString();
}
/**
* Looks for an empty field with minimal domain.
*
* @return a field with minimal domain, or null no field is empty
*/
private Field findBestCandidate() {
Field best = null;
int min = Integer.MAX_VALUE;
for (Field f : board)
if (f.isEmpty() && f.getDomain().size() < min) {
best = f;
min = best.getDomain().size();
}
return best;
}
/**
* Tries to solve the current board. The board is in the solved state if a solution is found,
* and unmodified if no solution is found.
*
* @return true iff a solution has been found.
*/
public boolean solve() {
final Field field = findBestCandidate();
if (field == null)
// A solution has been found
return true;
// There are empty fields. Try all values of its domain
final List<Field> revoke = new ArrayList<>();
for (Value v : field.getDomain()) {
field.setValue(v);
boolean consistent = true;
for (Field dependent : field.getDependents())
if (dependent.getDomain().contains(v)) {
if (dependent.getDomain().size() == 1) {
// This is a dead end because field 'dependent'
// would have an empty domain
consistent = false;
break;
}
revoke.add(dependent);
dependent.getDomain().remove(v);
}
if (consistent && solve())
return true;
// Choosing v as a value lead to a dead end.
// Revoke all modifications
field.clearValue();
for (Field f : revoke)
f.getDomain().add(v);
revoke.clear();
}
return false;
}
public List<Sudoku> solveAll() {
List<Sudoku> solutions = new ArrayList<>();
solveAll(solutions);
return solutions;
}
private void solveAll(List<Sudoku> solutions) {
final Field field = findBestCandidate();
if (field == null) {
// A solution has been found
Sudoku copy = new Sudoku();
copy.initialize(this.export());
solutions.add(copy);
return;
}
// There are empty fields. Try all values of its domain
final List<Field> revoke = new ArrayList<>();
for (Value v : field.getDomain()) {
field.setValue(v);
boolean consistent = true;
for (Field dependent : field.getDependents())
if (dependent.getDomain().contains(v)) {
if (dependent.getDomain().size() == 1) {
// This is a dead end because field 'dependent'
// would have an empty domain
consistent = false;
break;
}
revoke.add(dependent);
dependent.getDomain().remove(v);
}
if (consistent) solveAll(solutions);
// Revoke all modifications
field.clearValue();
for (Field f : revoke)
f.getDomain().add(v);
revoke.clear();
}
}
private int[] export() {
int[] values = new int[SIZE * SIZE];
int idx = 0;
while (idx < values.length) {
Field f = board[idx];
if (f != null) {
Value value = f.getValue();
if (value != null) values[idx] = value.getId();
else values[idx] = 0;
}
else throw new IllegalStateException("at least one field is not set");
++idx;
}
return values;
}
}

View File

@@ -0,0 +1,68 @@
package sudoku;
import java.util.List;
public class SudokuApp {
public static void main(String[] args) {
System.out.println("Sudoku Aufgabenblatt:");
System.out.println();
final Sudoku boardBlatt = new Sudoku();
boardBlatt.initialize(4, 5, 0, 0, 0, 0, 2, 0, 0,
6, 0, 0, 0, 2, 4, 8, 0, 0,
8, 0, 0, 0, 6, 1, 3, 0, 0,
0, 9, 0, 4, 0, 0, 0, 5, 0,
0, 1, 0, 2, 0, 8, 0, 7, 0,
0, 3, 0, 0, 0, 9, 0, 8, 0,
0, 0, 7, 1, 4, 0, 0, 0, 8,
0, 0, 2, 7, 9, 0, 0, 0, 6,
0, 0, 5, 0, 0, 0, 0, 2, 1);
printSolution(boardBlatt);
System.out.println("Sudoku Handzettel:");
System.out.println();
final Sudoku boardHandzettel = new Sudoku();
boardHandzettel.initialize(7, 0, 0, 0, 2, 5, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 9, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 0, 9, 1, 0, 0, 0, 0, 0,
0, 0, 0, 6, 0, 0, 2, 0, 0,
0, 0, 0, 0, 0, 0, 0, 3, 0,
8, 2, 0, 0, 0, 0, 5, 0, 0,
0, 0, 0, 0, 3, 0, 6, 0, 0,
0, 0, 0, 9, 0, 0, 0, 0, 0);
printSolution(boardHandzettel);
System.out.println("Sudoku mit 2 Lösungen:");
System.out.println();
final Sudoku boardWithTwoSolutions = new Sudoku();
boardWithTwoSolutions.initialize(2, 9, 5, 7, 4, 3, 8, 6, 1,
4, 3, 1, 8, 6, 5, 9, 0, 0,
8, 7, 6, 1, 9, 2, 5, 4, 3,
3, 8, 7, 4, 5, 9, 2, 1, 6,
6, 1, 2, 3, 8, 7, 4, 9, 5,
5, 4, 9, 2, 1, 6, 7, 3, 8,
7, 6, 3, 5, 2, 4, 1, 8, 9,
9, 2, 8, 6, 7, 1, 3, 5, 4,
1, 5, 4, 9, 3, 8, 6, 0, 0);
System.out.println(boardWithTwoSolutions);
System.out.println("Solutions:");
System.out.println();
List<Sudoku> solutions = boardWithTwoSolutions.solveAll();
System.out.println(String.join(System.lineSeparator(), solutions.stream().map(Sudoku::toString).toList()));
}
private static void printSolution(Sudoku sudoku) {
System.out.println(sudoku);
System.out.println();
if (sudoku.solve()) {
System.out.println("Solution:");
System.out.println();
System.out.println(sudoku);
}
else
System.out.println("No solution!");
}
}

View File

@@ -0,0 +1,41 @@
package sudoku;
/**
* The enumeration type of all possible values of a Sudoku field.
*/
public enum Value {
ONE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9);
private final int id;
Value(int id) {
this.id = id;
}
/**
* Returns a string representation of this value. As a matter of fact, its id is returned.
*
* @return a string representation of this value.
*/
@Override
public String toString() {
return String.valueOf(id);
}
/**
* Returns the value with the specified id.
*
* @param id an id (1 <= id <= 9)
* @return the value with the specified id.
*/
public static Value of(int id) {
return values()[id - 1];
}
/**
* Returns the id of this instance.
*
* @return the id of this instance.
*/
public int getId() {return id;}
}

View File

@@ -0,0 +1,65 @@
package uebung06.test.sudoku;
import org.junit.Before;
import org.junit.Test;
import sudoku.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class FieldTest {
private static final int X = 8;
private static final int Y = 5;
private static final int NUL = -1;
private static final Value VAL = Value.EIGHT;
private static Sudoku sudoku;
private static Field field;
@Before
public void init() {
sudoku = new Sudoku();
field = new Field(sudoku, X, Y);
}
@Test(expected = IllegalArgumentException.class)
public void illegalXLowerBoundFieldTest() {
field = new Field(sudoku, NUL, Y);
}
@Test(expected = IllegalArgumentException.class)
public void illegalXUpperBoundFieldTest() {
field = new Field(sudoku, Sudoku.SIZE, Y);
}
@Test(expected = IllegalArgumentException.class)
public void illegalYLowerBoundFieldTest() {
field = new Field(sudoku, X, NUL);
}
@Test(expected = IllegalArgumentException.class)
public void illegalYUpperBoundFieldTest() {
field = new Field(sudoku, X, Sudoku.SIZE);
}
@Test
public void valueFieldTest() {
field.setValue(VAL);
assertEquals(field.getValue(), VAL);
}
@Test
public void clearValueFieldTest() {
field.setValue(VAL);
field.clearValue();
assertNull(field.getValue());
}
@Test
public void isValueEmptyFieldTest() {
field.setValue(VAL);
field.clearValue();
assertTrue(field.isEmpty());
}
}

View File

@@ -0,0 +1,129 @@
package uebung06.test.sudoku;
import org.junit.Before;
import org.junit.Test;
import sudoku.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.IntStream;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class SudokuAppTest {
private static final int MAX = 81;
private static final int MIN = 0;
private static final int SUDOKU_SIZE = 9;
private static Sudoku board;
private static int[] init;
private static int[] wrongSol;
@Before
public void init() {
board = new Sudoku();
init = IntStream.of(4, 5, 0, 0, 0, 0, 2, 0, 0,
6, 0, 0, 0, 2, 4, 8, 0, 0,
8, 0, 0, 0, 6, 1, 3, 0, 0,
0, 9, 0, 4, 0, 0, 0, 5, 0,
0, 1, 0, 2, 0, 8, 0, 7, 0,
0, 3, 0, 0, 0, 9, 0, 8, 0,
0, 0, 7, 1, 4, 0, 0, 0, 8,
0, 0, 2, 7, 9, 0, 0, 0, 6,
0, 0, 5, 0, 0, 0, 0, 2, 1).toArray();
wrongSol = IntStream.of(4, 5, 1, 8, 7, 3, 2, 6, 9,
6, 7, 3, 9, 2, 4, 8, 1, 5,
8, 2, 9, 5, 6, 1, 3, 4, 7,
2, 9, 8, 4, 1, 7, 6, 5, 3,
5, 1, 6, 2, 3, 8, 9, 7, 4,
7, 3, 4, 6, 5, 9, 1, 8, 2,
3, 6, 7, 1, 4, 2, 5, 9, 8,
1, 8, 2, 7, 9, 5, 4, 3, 6,
9, 5, 4, 3, 8, 6, 7, 2, 1).toArray();
}
//Testing first If-condition in Sudoku.init
@Test(expected = IllegalArgumentException.class)
public void illegalLowerBoundInitSudokuAppTest() {
board.initialize(IntStream.range(MIN, MIN+1).toArray());
}
//Testing first If-condition in Sudoku.init
@Test(expected = IllegalArgumentException.class)
public void illegalUpperBoundInitSudokuAppTest() {
board.initialize(IntStream.range(MIN, MAX+1).toArray());
}
//Testing ArrayIndex of Value
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void illegalArrayIndexOutOfBoundInitSudokuAppTest() {
board.initialize(IntStream.range(MIN, MAX).toArray());
}
//Testing Sudoku.solve()
@Test
public void correctSolutionSudokuAppTest() {
board.initialize(init);
board.solve();
assertTrue(verifySudokuSol());
}
//Just testing verifySudokuSol-Method
@Test
public void wrongSolutionSudokuAppTest() {
try {
board.initialize(wrongSol);
} catch (final IllegalArgumentException e) {
//do nothing, just handling throwing exceptions from Sudoku-class
}
assertFalse(verifySudokuSol());
}
public Boolean verifySudokuSol() {
HashSet<Value> rows = new HashSet<>();
HashSet<Value> cols = new HashSet<>();
HashMap<Integer, HashSet<Value>> blocks = new HashMap<>();
IntStream.range(0, SUDOKU_SIZE)
.forEach(block -> blocks.put(block, new HashSet<>())); //init Hashsets for every block
for (int y = 0; y < SUDOKU_SIZE; y++) {
for (int x = 0; x < SUDOKU_SIZE; x++) {
//check column
Value val = board.field(x, y).getValue();
if (!cols.add(val))
return false;
if (cols.size() == SUDOKU_SIZE)
cols.clear();
//check row
val = board.field(y, x).getValue();
if (!rows.add(val))
return false;
if (rows.size() == SUDOKU_SIZE)
rows.clear();
//check 3x3 block
val = board.field(x, y).getValue();
if (!blocks.get(getBlockIndex(x, y)).add(val))
return false;
}
}
return true;
}
public int getBlockIndex(int x, int y) {
return getColIndex(x) + getRowIndex(y);
}
public int getColIndex(int x) {
return x / 3;
}
public int getRowIndex(int y) {
return (y/3)*3;
}
}

View File

@@ -0,0 +1,79 @@
package oop.ch02.cards;
/**
* Represents any card with a name and a unique serial number.
*
* @author Mark Minas
*/
public class Card {
/**
* The name of this card.
*/
private final String name;
/**
* Counts the number of instantiations of this class.
*/
private static int counter = 0;
/**
* The unique serial number of this card.
*/
private final int number;
/**
* Creates a new card with the specified name.
*
* @param name the name of this card
*/
public Card(String name) {
if (name == null)
throw new NullPointerException("name is null");
this.name = name;
this.number = ++counter;
}
/**
* Creates a new card for a student with the specified number.
*
* @param num the student's number
*/
public Card(int num) {
this("MatrNr. " + num);
}
/**
* Returns the name of this card.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Returns the serial number of this card.
*
* @return the serial number
*/
public int getNumber() {
return number;
}
/**
* Returns the number of instantiations of this class.
*
* @return the number of instantiations.
*/
public static int getCounter() {
return counter;
}
/**
* Returns a string representation of this card.
*/
@Override
public String toString() {
return "Card " + name + " (no " + number + ")";
}
}

View File

@@ -0,0 +1,34 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecuredContent;
import oop.ch05.secured.SecurityClient;
public class AccountCard extends MensaCard {
private final SecuredContent<String> account;
public AccountCard(String key, String account, int password) {
super(key, Color.white);
this.account = new SecuredContent<>(password, account);
}
public String getAccount() {
return account.getContent();
}
public void setAccount(SecurityClient client, String account) throws AuthorizationException {
if (account == null || account.trim().isEmpty())
throw new IllegalArgumentException("Invalid account " + account);
this.account.setContent(client, account);
}
@Override
public String toString() {
return super.toString() + " for account " + account.getContent();
}
@Override
public void pass(CashPoint cashPoint) throws RejectedException, AuthorizationException {
cashPoint.charge(this);
}
}

View File

@@ -0,0 +1,51 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class AccountManagement {
private final int password;
public final String name;
private final Random random = new Random();
private final Map<String, Integer> accounts = new TreeMap<>();
public AccountManagement(int password, String name) {
this.password = password;
this.name = name;
}
public void deposit(String account, int euros) {
final int amount = accounts.getOrDefault(account, 0);
accounts.put(account, amount + 100 * euros);
}
public void pay(AccountCard card, int price, CashPoint cashPoint) throws RejectedException, AuthorizationException {
final int amount = accounts.getOrDefault(card.getAccount(), 0);
final int challenge = nextChallenge();
if (cashPoint.challengeResponse(challenge) != requiredResponse(challenge))
throw new AuthorizationException(cashPoint + " is not authorized to access accounts on " + name);
if (amount < price)
throw new RejectedException(card + " bounced");
accounts.put(card.getAccount(), amount - price);
}
public int getAmount(String account) {
return accounts.getOrDefault(account, 0);
}
private int nextChallenge() {
return random.nextInt();
}
private int requiredResponse(int challenge) {
return challenge ^ password;
}
@Override
public String toString() {
return "Account Management " + name + " " + accounts;
}
}

View File

@@ -0,0 +1,43 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecuredContent;
public class CashCard extends MensaCard {
private final SecuredContent<Integer> balance;
public CashCard(String key, Color color, int password) {
super(key, color);
balance = new SecuredContent<>(password, 0);
if (color != Color.blue && color != Color.green)
throw new IllegalArgumentException("Invalid CashCard color " + color);
}
public int getBalance() {
return balance.getContent();
}
void deposit(VendingMachine client, int cents)
throws AuthorizationException {
if (cents <= 0)
throw new IllegalArgumentException("Non-positive deposit");
final int newBalance = getBalance() + cents;
balance.setContent(client, newBalance);
}
void charge(CashPoint client, int cents) throws AuthorizationException {
if (cents < 0)
throw new IllegalArgumentException("Negative charge");
balance.setContent(client, getBalance() - cents);
}
@Override
public String toString() {
return super.toString() + " with " + balance.getContent() + " cents";
}
@Override
public void pass(CashPoint cashPoint) throws AuthorizationException, RejectedException {
cashPoint.charge(this);
}
}

View File

@@ -0,0 +1,65 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecurityClient;
public class CashPoint implements SecurityClient {
private final int password;
public final String name;
private final AccountManagement accountMgmt;
private int counter;
private int cents;
public CashPoint(String name, int password, AccountManagement accountMgmt) {
this.name = name;
this.password = password;
this.accountMgmt = accountMgmt;
}
public int challengeResponse(int challenge) {
return challenge ^ password;
}
@Override
public String toString() {
return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)";
}
public int getCounter() {
return counter;
}
public int getCents() {
return cents;
}
private int getPrice(Color color) {
return switch (color) {
case green -> 267;
case blue -> 357;
case white -> 495;
default -> 0;
};
}
void count(MensaCard card) {
counter++;
}
void charge(CashCard cashCard) throws AuthorizationException, RejectedException {
final int price = getPrice(cashCard.color);
if (cashCard.getBalance() < price)
throw new RejectedException("insufficient payment");
cashCard.charge(this, price);
count(cashCard);
cents += price;
}
void charge(AccountCard accountCard) throws RejectedException, AuthorizationException {
final int price = getPrice(accountCard.color);
System.out.println("Charging " + price + " cents on account " + accountCard.getAccount());
accountMgmt.pay(accountCard, price, this);
count(accountCard);
cents += price;
}
}

View File

@@ -0,0 +1,5 @@
package oop.ch05.mensa;
public enum Color {
green, red, blue, white, gray
}

View File

@@ -0,0 +1,15 @@
package oop.ch05.mensa;
public class CountCard extends MensaCard {
public CountCard(String key, Color color) {
super(key, color);
if (color != Color.red && color != Color.gray)
throw new IllegalArgumentException("Invalid CountCard color " + color);
}
@Override
public void pass(CashPoint cashPoint) {
cashPoint.count(this);
}
}

View File

@@ -0,0 +1,20 @@
package oop.ch05.mensa;
import oop.ch02.cards.Card;
import oop.ch05.secured.AuthorizationException;
public abstract class MensaCard extends Card {
public final Color color;
protected MensaCard(String name, Color color) {
super(name);
this.color = color;
}
@Override
public String toString() {
return color + " card " + getNumber() + " (" + getName() + ")";
}
public abstract void pass(CashPoint cashPoint) throws RejectedException, AuthorizationException;
}

View File

@@ -0,0 +1,69 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
public class MensaExample {
public static void main(String[] args) {
AccountManagement mgmt = new AccountManagement(4711, "Uni");
VendingMachine vm1 = new VendingMachine("left", 4711);
VendingMachine vm2 = new VendingMachine("right", 4711);
VendingMachine tumVM = new VendingMachine("TUM Mensa", 3141);
CashPoint unibwMensa = new CashPoint("UniBw Mensa", 4711, mgmt);
AccountCard conf = new AccountCard("conference", "33-1298", 42);
MensaCard frankSmith = new CountCard("Frank Smith", Color.gray);
CashCard hansMueller = new CashCard("Hans Müller", Color.green, 4711);
CashCard peterSchmidt = new CashCard("Peter Schmidt", Color.green, 4711);
CashCard thomasMayer = new CashCard("Thomas Mayer", Color.blue, 4711);
deposit(vm1, hansMueller, 10);
deposit(vm1, peterSchmidt, 5);
deposit(vm2, thomasMayer, 2);
deposit(tumVM, hansMueller, 10);
System.out.println(vm1);
System.out.println(vm2);
System.out.println(tumVM);
System.out.println(hansMueller);
System.out.println(peterSchmidt);
System.out.println(thomasMayer);
System.out.println();
pass(hansMueller, unibwMensa);
System.out.println(hansMueller);
System.out.println(unibwMensa);
pass(frankSmith, unibwMensa);
pass(conf, unibwMensa);
pass(thomasMayer, unibwMensa);
pass(hansMueller, unibwMensa);
pass(hansMueller, unibwMensa);
pass(hansMueller, unibwMensa);
System.out.println(unibwMensa);
System.out.println(hansMueller);
System.out.println(peterSchmidt);
System.out.println(thomasMayer);
}
private static void pass(MensaCard mensaCard, CashPoint cashPoint) {
try {
mensaCard.pass(cashPoint);
}
catch (RejectedException e) {
System.out.println("rejected: " + e.getMessage());
}
catch (AuthorizationException e) {
System.out.println("authrozation failed: " + e.getMessage());
}
}
private static void deposit(VendingMachine vm, CashCard cashCard, int euros) {
try {
vm.deposit(cashCard, euros);
}
catch (AuthorizationException e) {
System.out.println("authorization failed: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,7 @@
package oop.ch05.mensa;
public class RejectedException extends Exception {
public RejectedException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,37 @@
package oop.ch05.mensa;
import oop.ch05.secured.AuthorizationException;
import oop.ch05.secured.SecurityClient;
public class VendingMachine implements SecurityClient {
private final int password;
public final String name;
private int euros;
public VendingMachine(String name, int password) {
this.name = name;
this.password = password;
this.euros = 0;
}
@Override
public int challengeResponse(int challenge) {
return challenge ^ password;
}
@Override
public String toString() {
return "Vending machine " + name + " (contains EUR " + euros + ")";
}
public int getEuros() {
return euros;
}
public void deposit(CashCard card, int euros) throws AuthorizationException {
if (euros <= 0)
throw new IllegalArgumentException("Non-positive deposit");
card.deposit(this, euros * 100);
this.euros += euros;
}
}

View File

@@ -0,0 +1,20 @@
package oop.ch05.secured;
/**
* This class represents exceptions that are thrown whenever an authorization
* fails.
*
* @author Mark Minas
*/
public class AuthorizationException extends Exception {
/**
* Constructs a new authorization exception with the specified detail message
* and cause.
*
* @param message the detail message (which is saved for later retrieval by the
* {@link #getMessage()} method).
*/
public AuthorizationException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,80 @@
package oop.ch05.secured;
import java.util.Random;
/**
* Represents containers for some content that is secured against modifications
* by a challenge-response approach. The content may be read at any time, but
* only authorized clients are allowed to modify the contents.
*
* @param <E> the type of the actual secured content
* @author Mark Minas
*/
public class SecuredContent<E> {
/**
* The actual, secured content
*/
private E content;
/**
* A random number generator used for generating challenges.
*/
private final Random random = new Random();
/**
* The password used for encryption.
*/
private final int password;
/**
* Creates a new container containing the specified contents.
*
* @param password this password is used for computing the expected response for a
* challenge.
* @param content the contained content
*/
public SecuredContent(int password, E content) {
this.password = password;
this.content = content;
}
/**
* Returns the contained contents. There is no authorization necessary for
* reading the contents.
*/
public E getContent() {
return content;
}
/**
* Sets the new contained contents. Only authorized clients are allowed to do
* so.
*
* @param client The accessing client. Authorization is checked prior to really
* modifying he contained content.
* @param content The new contained contents
* @throws AuthorizationException if the specified client cannot authorize himself
*/
public void setContent(SecurityClient client, E content)
throws AuthorizationException {
final int challenge = nextChallenge();
if (client.challengeResponse(challenge) != requiredResponse(challenge))
throw new AuthorizationException(client
+ " is not authorized to access contents.");
this.content = content;
}
/**
* Computes the next random challenge.
*/
private int nextChallenge() {
return random.nextInt();
}
/**
* Returns the expected response for the specified challenge.
*
* @param challenge an arbitrary integer used as a challenge
*/
private int requiredResponse(int challenge) {
return challenge ^ password;
}
}

View File

@@ -0,0 +1,18 @@
package oop.ch05.secured;
/**
* This interface must be implemented by any client that is going to be
* authorized by a challenge-response approach.
*
* @author Mark Minas
*/
public interface SecurityClient {
/**
* Returns the response for the specified challenge. Authorization succeeds
* if this response is the same as the one expected by the server.
*
* @param challenge the integer number for which the correct response has to be
* computed.
*/
int challengeResponse(int challenge);
}

View File

@@ -0,0 +1,22 @@
package quantities.generic;
import static quantities.generic.LengthUnit.METER;
import static quantities.generic.TimeUnit.SECOND;
import static quantities.generic.VelocityUnit.METER_PER_SECOND;
public class Length extends Quantity<Length>{
public Length(double value, Unit<Length> unit) {
super(value, unit);
}
public Velocity div(Time t) {
return METER_PER_SECOND.quantity(this.value(METER) / t.value(SECOND));
}
public Time div(Velocity v) {
return SECOND.quantity(this.value(METER) / v.value(METER_PER_SECOND));
}
}

View File

@@ -0,0 +1,19 @@
package quantities.generic;
public class LengthUnit extends Unit<Length> {
public LengthUnit(String name, double baseFactor) {
super(name, baseFactor);
}
public static final LengthUnit METER = new LengthUnit("m", 1);
public static final LengthUnit MILLIMETER = new LengthUnit("mm", 0.001);
public static final LengthUnit KILOMETER = new LengthUnit("km", 1000);
public static final LengthUnit MILE = new LengthUnit("mi", 1609.344);
public static final LengthUnit LIGHTYEAR = new LengthUnit("ly", 9460730472580800.0);
public static final LengthUnit PARSEC = new LengthUnit("pc", 3.0856776e16);
@Override
public Length quantity(double value) {
return new Length(value, this);
}
}

View File

@@ -0,0 +1,55 @@
package quantities.generic;
public abstract class Quantity<Q extends Quantity<Q>> {
public final double value;
public final Unit<Q> unit;
protected Quantity(double value, Unit<Q> unit) {
this.value = value;
this.unit = unit;
}
public double getBaseValue() {
return value * unit.baseFactor;
}
public double value(Unit<Q> unit) {
return getBaseValue() / unit.baseFactor;
}
//neue Methoden
public Q plus(Q other) {
return unit.quantity(value + other.getBaseValue() / unit.baseFactor);
}
public Q minus(Q other) {
return unit.quantity(value - other.getBaseValue() / unit.baseFactor);
}
public Q mult(double f) {
return unit.quantity(value * f);
}
public Q div(double f) {
return unit.quantity(value / f);
}
public Q to(Unit<Q> unit) {
return unit.quantity(getBaseValue() / unit.baseFactor);
}
public double div(Q other) {
return getBaseValue() / other.getBaseValue();
}
@Override
public String toString() {
return value + " " + unit;
}
public String format(String fmt, Unit unit) {
return String.format(fmt, value(unit), unit);
}
}

View File

@@ -0,0 +1,11 @@
package quantities.generic;
public class Time extends Quantity<Time> {
public Time(double value, Unit<Time> unit) {
super(value, unit);
}
public Length mult(Velocity v) {
return v.mult(this);
}
}

View File

@@ -0,0 +1,17 @@
package quantities.generic;
public class TimeUnit extends Unit<Time> {
public TimeUnit(String name, double baseFactor) {
super(name, baseFactor);
}
@Override
public Time quantity(double value) {
return new Time(value, this);
}
public static final TimeUnit SECOND = new TimeUnit("s", 1);
public static final TimeUnit MINUTE = new TimeUnit("min", 60);
public static final TimeUnit HOUR = new TimeUnit("h", 3600);
public static final TimeUnit DAY = new TimeUnit("d", 86400);
}

View File

@@ -0,0 +1,18 @@
package quantities.generic;
public abstract class Unit<T extends Quantity<T>> {
public final String name;
public final double baseFactor;
public Unit(String name, double baseFactor) {
this.name = name;
this.baseFactor = baseFactor;
}
public abstract T quantity(double value);
@Override
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,15 @@
package quantities.generic;
import static quantities.generic.LengthUnit.METER;
import static quantities.generic.TimeUnit.SECOND;
import static quantities.generic.VelocityUnit.METER_PER_SECOND;
public class Velocity extends Quantity<Velocity> {
public Velocity(double value, Unit<Velocity> unit) {
super(value, unit);
}
public Length mult(Time t) {
return METER.quantity(this.value(METER_PER_SECOND) * t.value(SECOND));
}
}

View File

@@ -0,0 +1,16 @@
package quantities.generic;
public class VelocityUnit extends Unit<Velocity> {
public VelocityUnit(String name, double baseFactor) {
super(name, baseFactor);
}
@Override
public Velocity quantity(double value) {
return new Velocity(value, this);
}
public static final VelocityUnit METER_PER_SECOND = new VelocityUnit("m/s", 1);
public static final VelocityUnit KMH = new VelocityUnit("km/h", 1.0 / 3.6);
public static final VelocityUnit MPH = new VelocityUnit("mph", 0.44704);
}

View File

@@ -0,0 +1,46 @@
package quantities.plain;
import static quantities.plain.LengthUnit.METER;
import static quantities.plain.TimeUnit.SECOND;
import static quantities.plain.VelocityUnit.METER_PER_SECOND;
public class Length extends Quantity {
private final LengthUnit unit;
public Length(double value, LengthUnit unit) {
super(value, unit);
this.unit = unit;
}
public Length plus(Length other) {
return new Length(value + other.getBaseValue() / unit.baseFactor, unit);
}
public Length minus(Length other) {
return new Length(value - other.getBaseValue() / unit.baseFactor, unit);
}
public Length mult(double f) {
return new Length(value * f, unit);
}
public Length div(double f) {
return new Length(value / f, unit);
}
public Length to(LengthUnit unit) {
return new Length(getBaseValue() / unit.baseFactor, unit);
}
public double div(Length other) {
return getBaseValue() / other.getBaseValue();
}
public Velocity div(Time t) {
return new Velocity(this.value(METER) / t.value(SECOND), METER_PER_SECOND);
}
public Time div(Velocity v) {
return new Time(this.value(METER) / v.value(METER_PER_SECOND), SECOND);
}
}

View File

@@ -0,0 +1,14 @@
package quantities.plain;
public class LengthUnit extends Unit {
public LengthUnit(String name, double baseFactor) {
super(name, baseFactor);
}
public static final LengthUnit METER = new LengthUnit("m", 1);
public static final LengthUnit MILLIMETER = new LengthUnit("mm", 0.001);
public static final LengthUnit KILOMETER = new LengthUnit("km", 1000);
public static final LengthUnit MILE = new LengthUnit("mi", 1609.344);
public static final LengthUnit LIGHTYEAR = new LengthUnit("ly", 9460730472580800.0);
public static final LengthUnit PARSEC = new LengthUnit("pc", 3.0856776e16);
}

View File

@@ -0,0 +1,60 @@
package quantities.plain;
import static quantities.plain.LengthUnit.KILOMETER;
import static quantities.plain.LengthUnit.MILE;
import static quantities.plain.LengthUnit.MILLIMETER;
import static quantities.plain.LengthUnit.PARSEC;
import static quantities.plain.TimeUnit.HOUR;
import static quantities.plain.TimeUnit.MINUTE;
import static quantities.plain.TimeUnit.SECOND;
import static quantities.plain.VelocityUnit.KMH;
import static quantities.plain.VelocityUnit.METER_PER_SECOND;
import static quantities.plain.VelocityUnit.MPH;
public class PlainQuantitiesDemo {
public static void main(String[] args) {
final Length l1 = new Length(1, KILOMETER);
final Length l2 = new Length(1200, MILLIMETER);
final Length l3 = new Length(1, MILE);
System.out.println(l1);
System.out.println(l2);
System.out.println(l1 + " + " + l2 + " = " + l1.plus(l2));
System.out.println(l1 + " + " + l2 + " (in mm) = " + l1.plus(l2).to(MILLIMETER));
System.out.println(l3 + " / " + l1 + " = " + l3.div(l1));
final Time t1 = new Time(100, SECOND);
final Time t2 = new Time(5, HOUR);
System.out.println(t1);
System.out.println(t2);
System.out.println(t1.plus(t2));
System.out.println(t1.plus(t2).to(MINUTE));
final Velocity v1 = new Velocity(12, KMH);
final Velocity v2 = new Velocity(100, METER_PER_SECOND);
System.out.println(v1);
System.out.println(v2);
System.out.println(v2.to(KMH));
System.out.println(v1.plus(v2));
final Length l4 = new Length(300, KILOMETER).to(PARSEC);
final Time t3 = new Time(2, HOUR);
final Velocity v3 = l4.div(t3);
System.out.println(l4 + " / " + l3 + " = " + v3);
System.out.println(v1 + " * " + t1 + " = " + v1.mult(t1).to(KILOMETER));
final Length l5 = v3.mult(t1.to(HOUR));
System.out.println(v3 + " * " + t1 + " = " + l5);
final Time t5 = l4.div(v2);
System.out.println(l4 + " / " + v2 + " = " + t5.to(MINUTE));
Velocity v5 = new Velocity(55, MPH);
System.out.println(v5 + " = " + v5.format("%4.1f %s", KMH));
System.out.println((v5.mult(new Time(30, MINUTE)).to(MILE)));
}
}

View File

@@ -0,0 +1,28 @@
package quantities.plain;
public abstract class Quantity {
public final double value;
public final Unit unit;
protected Quantity(double value, Unit unit) {
this.value = value;
this.unit = unit;
}
public double getBaseValue() {
return value * unit.baseFactor;
}
public double value(Unit unit) {
return getBaseValue() / unit.baseFactor;
}
@Override
public String toString() {
return value + " " + unit;
}
public String format(String fmt, Unit unit) {
return String.format(fmt, value(unit), unit);
}
}

View File

@@ -0,0 +1,34 @@
package quantities.plain;
public class Time extends Quantity{
private final TimeUnit unit;
public Time(double value, TimeUnit unit) {
super(value, unit);
this.unit = unit;
}
public Time plus(Time other) {
return new Time(value + other.getBaseValue() / unit.baseFactor, unit);
}
public Time minus(Time other) {
return new Time(value - other.getBaseValue() / unit.baseFactor, unit);
}
public Time mult(double f) {
return new Time(value * f, unit);
}
public Time div(double f) {
return new Time(value / f, unit);
}
public Time to(TimeUnit unit) {
return new Time(getBaseValue() / unit.baseFactor, unit);
}
public Length mult(Velocity v) {
return v.mult(this);
}
}

View File

@@ -0,0 +1,13 @@
package quantities.plain;
public class TimeUnit extends Unit{
public TimeUnit(String name, double baseFactor) {
super(name, baseFactor);
}
public static final TimeUnit SECOND = new TimeUnit("s", 1);
public static final TimeUnit MINUTE = new TimeUnit("m", 60);
public static final TimeUnit HOUR = new TimeUnit("h", 3600);
}

View File

@@ -0,0 +1,16 @@
package quantities.plain;
public abstract class Unit {
public final String name;
public final double baseFactor;
public Unit(String name, double baseFactor) {
this.name = name;
this.baseFactor = baseFactor;
}
@Override
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,38 @@
package quantities.plain;
import static quantities.plain.LengthUnit.METER;
import static quantities.plain.TimeUnit.SECOND;
import static quantities.plain.VelocityUnit.METER_PER_SECOND;
public class Velocity extends Quantity {
private final VelocityUnit unit;
public Velocity(double value, VelocityUnit unit) {
super(value, unit);
this.unit = unit;
}
public Velocity plus(Velocity other) {
return new Velocity(value + other.getBaseValue() / unit.baseFactor, unit);
}
public Velocity minus(Velocity other) {
return new Velocity(value - other.getBaseValue() / unit.baseFactor, unit);
}
public Velocity mult(double f) {
return new Velocity(value * f, unit);
}
public Velocity div(double f) {
return new Velocity(value / f, unit);
}
public Velocity to(VelocityUnit unit) {
return new Velocity(getBaseValue() / unit.baseFactor, unit);
}
public Length mult(Time t) {
return new Length(this.value(METER_PER_SECOND) * t.value(SECOND), METER);
}
}

View File

@@ -0,0 +1,13 @@
package quantities.plain;
public class VelocityUnit extends Unit{
public VelocityUnit(String name, double baseFactor) {
super(name, baseFactor);
}
public static final VelocityUnit KMH = new VelocityUnit("kmh", 1000/3600); // 1/3.6
public static final VelocityUnit MPH = new VelocityUnit("mph", 1609.344/3600);
public static final VelocityUnit METER_PER_SECOND = new VelocityUnit("meter per second", 1);
}

View File

@@ -0,0 +1,82 @@
package oop.ch05.generic.mensa;
import oop.ch05.secured.AuthorizationException;
import oop.ch05.mensa.*;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
public class MensaTest {
private static final String CONF_ACCOUNT = "33-1298";
private static final String OTHER_ACCOUNT = "33-1299";
private AccountManagement accountMgt;
private VendingMachine vm1;
private VendingMachine vm2;
private VendingMachine tumVM;
private CashPoint unibwMensa;
private AccountCard conf;
private MensaCard frankSmith;
private CashCard hansMueller;
private CashCard peterSchmidt;
private CashCard thomasMayer;
@Before
public void setup() {
accountMgt = new AccountManagement(4711, "UniBw admin");
vm1 = new VendingMachine("left", 4711);
vm2 = new VendingMachine("right", 4711);
tumVM = new VendingMachine("TUM Mensa", 3141);
unibwMensa = new CashPoint("UniBw Mensa", 4711, accountMgt);
conf = new AccountCard("conference", CONF_ACCOUNT, 42);
frankSmith = new CountCard("Frank Smith", Color.gray);
hansMueller = new CashCard("Hans Müller", Color.green, 4711);
peterSchmidt = new CashCard("Peter Schmidt", Color.green, 4711);
thomasMayer = new CashCard("Thomas Mayer", Color.blue, 4711);
}
@Test
public void testPayment() throws AuthorizationException, RejectedException {
vm1.deposit(hansMueller, 10);
vm1.deposit(peterSchmidt, 5);
vm2.deposit(thomasMayer, 2);
assertThrows(AuthorizationException.class, () -> tumVM.deposit(hansMueller, 10));
assertEquals(15, vm1.getEuros());
assertEquals(2, vm2.getEuros());
assertEquals(0, tumVM.getEuros());
assertEquals(1000, hansMueller.getBalance());
assertEquals(500, peterSchmidt.getBalance());
assertEquals(200, thomasMayer.getBalance());
hansMueller.pass(unibwMensa);
assertEquals(733, hansMueller.getBalance());
assertEquals(1, unibwMensa.getCounter());
assertEquals(267, unibwMensa.getCents());
frankSmith.pass(unibwMensa);
assertEquals(0, accountMgt.getAmount(CONF_ACCOUNT));
assertThrows(RejectedException.class, () -> conf.pass(unibwMensa));
assertThrows(RejectedException.class, () -> thomasMayer.pass(unibwMensa));
hansMueller.pass(unibwMensa);
hansMueller.pass(unibwMensa);
assertEquals(199, hansMueller.getBalance());
assertThrows(RejectedException.class, () -> hansMueller.pass(unibwMensa));
accountMgt.deposit(CONF_ACCOUNT, 1000);
accountMgt.deposit(OTHER_ACCOUNT, 2000);
assertEquals(100000, accountMgt.getAmount(CONF_ACCOUNT));
assertEquals(200000, accountMgt.getAmount(OTHER_ACCOUNT));
conf.pass(unibwMensa);
assertEquals(99505, accountMgt.getAmount(CONF_ACCOUNT));
assertEquals(200000, accountMgt.getAmount(OTHER_ACCOUNT));
assertEquals(5, unibwMensa.getCounter());
}
}

View File

@@ -0,0 +1,63 @@
package doc;
import java.util.List;
import java.util.ArrayList;
public class Book{
private String title;
private String author;
private List<TextComponent> contents;
public Book(String author, String title, List<TextComponent> contents){
this.title = title;
this.author = author;
this.contents = contents;
}
public int countWords(){
Section helpSection = new Section("help", contents);
return helpSection.countWords();
}
public int countWordsByVisitor(){
CountWordsVisitor countWords = new CountWordsVisitor();
int wordCount = 0;
for(TextComponent i : contents){
wordCount += i.accept(countWords);
}
return wordCount;
}
public List<String> tableOfContents(){
TableOfContentsVisitor contentTable = new TableOfContentsVisitor();
List<String> returnedContent = new ArrayList<String>();
for(TextComponent e : contents){
//contentTable.setPrefix(Integer.toString(itteration));
List<String> returned = e.accept(contentTable);
if(returned != null){
returnedContent.addAll(returned);
}
}
return returnedContent;
}
public String toText(){
StringBuilder st = new StringBuilder();
ToTextVisitor textVisitor = new ToTextVisitor();
st.append(" ");
st.append(author);
st.append("\n");
st.append(title);
st.append("\n\n");
for(TextComponent i : contents){
st.append(i.accept(textVisitor));
st.append("\n");
}
return st.toString();
}
}

View File

@@ -0,0 +1,148 @@
package doc;
import java.util.List;
public class BookDemo {
private static final String TEXT0 = "Über Entwurf und Realisierung eierlegender Wollmilchsäue.";
private static final String TEXT1 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod " +
"tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. " +
"At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd " +
"gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum " +
"dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor " +
"invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.";
private static final String TEXT2 = "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie " +
"consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan " +
"et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis " +
"dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer " +
"adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore " +
"magna aliquam erat volutpat.";
private static final String TEXT3 = "Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit " +
"lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure " +
"dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore " +
"eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim " +
"qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla " +
"facilisi.";
private static final String TEXT4 = "Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming " +
"id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, " +
"consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet " +
"dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud " +
"exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo " +
"consequat.";
private static final String TEXT5 = "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie " +
"consequat, vel illum dolore eu feugiat nulla facilisis.";
private static final String TEXT6 = "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, " +
"no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, " +
"consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et " +
"dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo " +
"duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est " +
"Lorem ipsum dolor sit amet.";
private static final String TEXT7 = "Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore " +
"et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et " +
"justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata " +
"sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur " +
"sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore " +
"magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo " +
"dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est " +
"Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing " +
"elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " +
"erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea " +
"rebum. Stet clita kasd gubergren, no sea takimata sanctus.";
private static final String TEXT8 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam " +
"voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet " +
"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit " +
"amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " +
"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed " +
"diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet " +
"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ";
private static final String TEXT9 = "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie " +
"consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan " +
"et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis " +
"dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer " +
"adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore " +
"magna aliquam erat volutpat.";
private static final String TEXT10 = "Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit " +
"lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure " +
"dolor in hendrerit in vulputate velit esse molestie consequat, vel illum " +
"dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio " +
"dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te " +
"feugait nulla facilisi.";
private static final String TEXT11 = "Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet " +
"doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, " +
"consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut " +
"laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, " +
"quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex " +
"ea commodo";
private static final String TEXT12 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam " +
"voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet " +
"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit " +
"amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " +
"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed " +
"diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet " +
"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
private static final String TEXT13 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam " +
"voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita " +
"kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
private static final String TEXT14 = "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, " +
"no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit " +
"amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut " +
"labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam " +
"et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata " +
"sanctus est Lorem ipsum dolor sit amet.";
private static final String TEXT15 = "Lorem ipsum dolor sit amet, consetetur sadipscing " +
"elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et " +
"nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd " +
"magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum " +
"dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed " +
"diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.";
public static void main(String[] args) {
final Book book = makeBook();
// Aufgabe 1
System.out.println("Buch enthält " + book.countWords() + " Wörter");
// Aufgabe 2
System.out.println("Buch enthält " + book.countWordsByVisitor() + " Wörter");
System.out.println("Inhalt: " + book.tableOfContents());
System.out.println(book.toText());
}
private static Book makeBook() {
final Section sec1 = new Section("Einführung",
List.of(new Paragraph(TEXT1),
new Paragraph(TEXT2)));
final Section sec2 = new Section("Hintergrund",
List.of(new Paragraph(TEXT3),
new Section("Geschichte",
List.of(new Paragraph(TEXT4),
new Image("https://www.bekannt.de/p1.pdf",
"Bild 1"),
new Paragraph(TEXT5))),
new Section("Literatur",
List.of(new Paragraph(TEXT6),
new Image("https://www.eh-klar.de/p2.pdf",
"Bild 2"),
new Paragraph(TEXT7),
new Image("https://www.jeder-weiss-es.de/p3.pdf",
"Bild 3"),
new Paragraph(TEXT8)))));
final Section sec3 = new Section("Idee",
List.of(new Paragraph(TEXT9)));
final Section sec4 = new Section("Umsetzung",
List.of(new Paragraph(TEXT10),
new Section("Entwurf",
List.of(new Paragraph(TEXT11),
new Section("Grobentwurf",
List.of(new Paragraph(TEXT12))),
new Section("Feinentwurf",
List.of(new Paragraph(TEXT13))))),
new Section("Realisierung",
List.of(new Paragraph(TEXT14)))));
final Section sec5 = new Section("Zusammenfassung und Ausblick", List.of(new Paragraph(TEXT15)));
return new Book("Peter Mustermann", "Die eierlegende Wollmilchsau",
List.of(new Paragraph(TEXT0), sec1, sec2, sec3, sec4, sec5));
}
}

View File

@@ -0,0 +1,38 @@
package doc;
import java.util.List;
public class CountWordsVisitor implements Visitor<Integer>{
public Integer visit(Paragraph e){
String text = e.getText();
int wordCount = 0;
boolean open = false;
for (char i : text.toCharArray()){
if(!open && Character.isAlphabetic(i)){
open = true;
}
else if(open && !Character.isAlphabetic(i)){
open = false;
wordCount++;
}
}
if (open){
open = false;
wordCount++;
}
return wordCount;
}
public Integer visit(Image e){
return 0;
}
public Integer visit(Section e){
List<TextComponent> contents = e.getContents();
int wordCount = 0;
for (TextComponent c: contents){
wordCount += c.accept(this);
}
return wordCount;
}
}

View File

@@ -0,0 +1,29 @@
package doc;
public class Image implements TextComponent{
private String url;
private String caption;
public Image(String url, String caption) {
this.url = url;
this.caption = caption;
}
public int countWords(){
return 0;
}
public String getUrl(){
return url;
}
public String getCaption(){
return caption;
}
public <T> T accept(Visitor<T> v){
return v.visit(this);
}
}

View File

@@ -0,0 +1,39 @@
package doc;
public class Paragraph implements TextComponent{
private String text;
public Paragraph(String text){
this.text = text;
}
public String getText(){
return text;
}
public int countWords(){
int wordCount = 0;
boolean open = false;
for(char i : text.toCharArray()){
if(!open && Character.isAlphabetic(i)){
open = true;}
else if(open && !Character.isAlphabetic(i)){
open = false;
wordCount ++;
}
}
if(open){
open = false;
wordCount++;
}
return wordCount;
}
public <T> T accept(Visitor<T> v){
return v.visit(this);
}
}

View File

@@ -0,0 +1,35 @@
package doc;
import java.util.List;
public class Section implements TextComponent{
private String header;
private List<TextComponent> contents;
public Section(String header, List<TextComponent> contents){
this.header = header;
this.contents = contents;
}
public String getHeader(){
return header;
}
public List<TextComponent> getContents(){
return contents;
}
public int countWords(){
int wordCount = 0;
for(TextComponent i : contents){
wordCount = wordCount + i.countWords();
}
return wordCount;
}
public <T> T accept(Visitor<T> v){
return v.visit(this);
}
}

View File

@@ -0,0 +1,77 @@
package doc;
import java.util.ArrayList;
import java.util.List;
public class TableOfContentsVisitor implements Visitor <List<String>>{
private String prefix = "1";
public TableOfContentsVisitor(){
}
public void setPrefix(String pre){
prefix = pre;
}
public List<String> visit(Paragraph e){
return null;
}
public List<String> visit(Image e){
return null;
}
/*public List<String> visit(Section e){
List<String> tabelOfContentsList = new ArrayList<String>();
List<TextComponent> copied = e.getContents();
String oldPrefix = prefix;
if(prefix.length() == 0){
tabelOfContentsList.add(prefix+ " " + e.getHeader());
}
else {
tabelOfContentsList.add(prefix + " " + e.getHeader());
}
int chapter = 1;
for(TextComponent i : e.getContents()){
String savedPrefix = prefix;
setPrefix(prefix + "." + Integer.toString(chapter));
List<String> returnedContent = i.accept(this);
if (returnedContent != null){
tabelOfContentsList.addAll(i.accept(this));
chapter++;
}
setPrefix(savedPrefix);
}
setPrefix(oldPrefix);
return tabelOfContentsList;
}*/
//Alternative die immer den aktuellen Stand übergibt und somit auf jeder "Ebene" ein eigener Visitor agiert
private int sectionCtr = 1;
public TableOfContentsVisitor(String prefix) {
this.prefix = prefix;
}
@Override
public List<String> visit(Section section) {
final List<String> list = new ArrayList<>();
sectionCtr++;
list.add(prefix + sectionCtr + " " + section.getHeader());
final TableOfContentsVisitor subVisitor =
new TableOfContentsVisitor(prefix + sectionCtr + ".");
for (TextComponent c : section.getContents())
list.addAll(c.accept(subVisitor));
return list;
}
}

View File

@@ -0,0 +1,7 @@
package doc;
public interface TextComponent {
int countWords();
<T> T accept(Visitor<T> visitor);
}

View File

@@ -0,0 +1,38 @@
package doc;
class ToTextVisitor implements Visitor<String> {
private final String prefix;
private int sectionCtr = 0;
private int imageCtr = 0;
public ToTextVisitor() {
this("");
}
public ToTextVisitor(String prefix) {
this.prefix = prefix;
}
@Override
public String visit(Paragraph section) {
return section.getText();
}
@Override
public String visit(Image image) {
imageCtr++;
final String num = prefix + imageCtr;
return "<image " + image.getUrl() + ">\nFig. " + num + ": " + image.getCaption();
}
@Override
public String visit(Section section) {
final StringBuilder b = new StringBuilder();
sectionCtr++;
b.append(prefix).append(sectionCtr).append(" ").append(section.getHeader());
final ToTextVisitor subVisitor = new ToTextVisitor(prefix + sectionCtr + ".");
for (TextComponent c : section.getContents())
b.append("\n\n").append(c.accept(subVisitor));
return b.toString();
}
}

View File

@@ -0,0 +1,10 @@
package doc;
public interface Visitor<T> {
T visit(Image image);
T visit(Paragraph paragraph);
T visit(Section section);
// semantisch falsch
//T visit(TextComponent c);
}

View File

@@ -0,0 +1,70 @@
package chess;
import java.util.Iterator;
import iterator.Array2dIterator;
import iterator.SkipNullIterator;
public class Board implements Iterable<Piece> {
private final Piece[][] field = new Piece[8][8];
//private final List<Piece> pieces = new ArrayList<>();
void add(Piece piece) {
if (piece.getBoard() != this)
throw new IllegalArgumentException("wrong board");
final Piece existing = pieceAt(piece.getRow(), piece.getCol());
if (existing != null)
throw new IllegalArgumentException("already occupied by " + existing);
field[piece.getRow() - 1][piece.getCol() - 1] = piece;
//pieces.add(piece);
}
public void printBoard() {
System.out.println(" 1 2 3 4 5 6 7 8");
System.out.println(" +---+---+---+---+---+---+---+---+");
for (int row = 1; row <= 8; row++) {
System.out.print("" + row + " ");
for (int col = 1; col <= 8; col++) {
final Piece p = pieceAt(row, col);
final char c = p == null ? ' ' : p.charRep();
System.out.print("| " + c + " ");
}
System.out.println("|");
System.out.println(" +---+---+---+---+---+---+---+---+");
}
}
public Piece pieceAt(int row, int col) {
return field[row - 1][col - 1];
}
public void check() {
for (Piece p1 : this) {
System.out.println(p1.toString());
for (Piece p2 : this)
if (p1 != p2)
if (p1.canCapture(p2))
System.out.println(" can capture " + p2.toString());
else
System.out.println(" cannot capture " + p2.toString());
}
}
public String toString() {
StringBuilder sb = new StringBuilder("[");
Iterator<Piece> it = iterator();
while (it.hasNext()) {
sb.append(it.next());
if (it.hasNext()) sb.append(", ");
}
sb.append("]");
return sb.toString();
}
@Override
public Iterator<Piece> iterator() {
return new SkipNullIterator<>(new Array2dIterator<>(field));
}
}

View File

@@ -0,0 +1,95 @@
package chess;
import java.io.InputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ChessApp {
private static final String HELP = "h";
private static final String CHECK = "c";
private static final String ABORT = "a";
private static final String WHITE_QUEEN = "q";
private static final String BLACK_QUEEN = "Q";
private static final String WHITE_KNIGHT = "n";
private static final String BLACK_KNIGHT = "N";
private static final String ITERATOR = "i";
private final Scanner scanner;
private final Board board;
public static void main(String[] args) {
new ChessApp(System.in, new Board()).playChess();
}
private ChessApp(InputStream in, Board board) {
scanner = new Scanner(in);
this.board = board;
}
private void playChess() {
board.printBoard();
commandLoop();
System.out.println("Terminated");
}
private void commandLoop() {
while (true) {
System.out.printf("Type in command (%s for help):%n", HELP);
try {
final String command = scanner.next();
if (ABORT.equals(command)) return;
switch (command) {
case HELP -> help();
case CHECK -> board.check();
case BLACK_QUEEN -> addQueen(Color.black);
case WHITE_QUEEN -> addQueen(Color.white);
case BLACK_KNIGHT -> addKnight(Color.black);
case WHITE_KNIGHT -> addKnight(Color.white);
case ITERATOR -> iterate();
default -> System.out.println("Invalid command " + command);
}
}
catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
}
catch (NoSuchElementException ex) {
return;
}
}
}
private void addKnight(Color color) {
final int row = scanner.nextInt();
final int col = scanner.nextInt();
new Knight(color, board, row, col);
board.printBoard();
}
private void addQueen(Color color) {
final int row = scanner.nextInt();
final int col = scanner.nextInt();
new Queen(color, board, row, col);
board.printBoard();
}
private void iterate() {
final Iterator<Piece> it = board.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
private void help() {
System.out.println("Commands:");
System.out.println(ABORT + ": terminate the program");
System.out.println(CHECK + ": check the pieces on the board");
System.out.println(WHITE_KNIGHT + " <int> <int>: place a new white knight at specified position");
System.out.println(BLACK_KNIGHT + " <int> <int>: place a new black knight at specified position");
System.out.println(WHITE_QUEEN + " <int> <int>: place a new white queen at specified position");
System.out.println(BLACK_QUEEN + " <int> <int>: place a new black queen at specified position");
System.out.println(ITERATOR + ": iterate over the board and print all pieces to the console");
}
}

View File

@@ -0,0 +1,5 @@
package chess;
public enum Color {
black, white
}

View File

@@ -0,0 +1,28 @@
package chess;
import static java.lang.Math.abs;
public class Knight extends Piece {
public Knight(Color color, Board board, int row, int col) {
super(color, board, row, col);
}
@Override
public char charRep() {
return getColor() == Color.white ? 'n' : 'N';
}
@Override
public String toString() {
return String.format("%s knight at (%d,%d)", getColor(), getRow(), getCol());
}
@Override
public boolean canCapture(Piece other) {
if (getBoard() != other.getBoard() || getColor() == other.getColor())
return false;
final int dr = abs(getRow() - other.getRow());
final int dc = abs(getCol() - other.getCol());
return dr == 2 && dc == 1 || dr == 1 && dc == 2;
}
}

Some files were not shown because too many files have changed in this diff Show More