forked from pr2-lecture/uebungen
Update of exercises
parent
86415254cb
commit
3cfff58597
|
@ -1,6 +1,6 @@
|
|||
# MatrixSuche
|
||||
## Lernziel
|
||||
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
Statische innere Klassen einsetzen.
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# Lösung: MatrixSuche
|
||||
|
||||
Musterlösung:
|
||||
|
||||
[pr2.lambda.matrixsuche](../../solutions/src/main/java/pr2/lambda/matrixsuche/)
|
|
@ -1,6 +1,6 @@
|
|||
# Innere Klasse Beobachter
|
||||
## Lernziel
|
||||
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
Zugriff von inneren Klassen auf die umgebende Klasse.
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# Lösung: Innere Klasse Beobachter
|
||||
|
||||
Musterlösung:
|
||||
|
||||
[pr2.lambda.observer](../../solutions/src/main/java/pr2/lambda/observer/).
|
|
@ -1,6 +1,6 @@
|
|||
# Callback mit anonymer Klasse realisieren
|
||||
## Lernziel
|
||||
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
Eine gegebene Funktion durch einen Callback parametrieren. Der Callback wird als anonyme innere Klasse realisiert.
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# Lösung: Callback mit anonymer Klasse realisieren
|
||||
|
||||
Musterlösung:
|
||||
|
||||
[pr2.lambda.callback](../../solutions/src/main/java/pr2/lambda/callback/)
|
|
@ -1,6 +1,6 @@
|
|||
# StringTransmogrifier
|
||||
## Lernziel
|
||||
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
Funktionale Interfaces entwerfen und verwenden.
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# Lösung: StringTransmogrifier
|
||||
|
||||
Musterlösung:
|
||||
|
||||
[pr2.lambda.mogrifier_1](../../solutions/src/main/java/pr2/lambda/mogrifier_1/)
|
|
@ -1,6 +1,6 @@
|
|||
# StringTransmogrifier erweitern
|
||||
## Lernziel
|
||||
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
Funktionale Interfaces entwerfen und verwenden.
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# Lösung: StringTransmogrifier erweitern
|
||||
|
||||
Musterlösung:
|
||||
|
||||
[pr2.lambda.mogrifier_2](../../solutions/src/main/java/pr2/lambda/mogrifier_2/)
|
16
readme.md
16
readme.md
|
@ -82,14 +82,14 @@ Hinweise zur nötigen Softwareausstattung finden Sie [hier](help/softwareausstat
|
|||
| 68. | Generische Typen | [Generische Queue](Generische_Typen_007/readme.md) | [✅](Generische_Typen_007/solution/) |
|
||||
| 69. | Generische Typen | [`super` und `extends` einsetzen](Generische_Typen_008/readme.md) | [✅](Generische_Typen_008/solution/) |
|
||||
| 70. | Generische Typen | [Generische Typen zusammen mit Wildcards einsetzen](Generische_Typen_009/readme.md) | [✅](Generische_Typen_009/solution/) |
|
||||
| 71. | Geschachtelte Klassen | [Eigene compare-Methode schreiben](Geschachtelte_Klassen_001/readme.md) | |
|
||||
| 72. | Geschachtelte Klassen | [Innere Klasse Beobachter](Geschachtelte_Klassen_002/readme.md) | |
|
||||
| 73. | Geschachtelte Klassen | [Callback mit anonymer Klasse realisieren](Geschachtelte_Klassen_003/readme.md) | |
|
||||
| 74. | Lambdas | [Comparator als Lambda](Lambdas_001/readme.md) | |
|
||||
| 75. | Lambdas | [Callback mit Lambda realisieren](Lambdas_002/readme.md) | |
|
||||
| 76. | Geschachtelte Klassen | [MatrixSuche](Geschachtelte_Klassen_001/readme.md) | |
|
||||
| 77. | Lambdas | [StringTransmogrifier](Lambdas_001/readme.md) | |
|
||||
| 78. | Lambdas | [StringTransmogrifier erweitern](Lambdas_002/readme.md) | |
|
||||
| 71. | Geschachtelte Klassen | [Eigene compare-Methode schreiben](Geschachtelte_Klassen_001/readme.md) | [✅](Geschachtelte_Klassen_001/solution/) |
|
||||
| 72. | Geschachtelte Klassen | [Innere Klasse Beobachter](Geschachtelte_Klassen_002/readme.md) | [✅](Geschachtelte_Klassen_002/solution/) |
|
||||
| 73. | Geschachtelte Klassen | [Callback mit anonymer Klasse realisieren](Geschachtelte_Klassen_003/readme.md) | [✅](Geschachtelte_Klassen_003/solution/) |
|
||||
| 74. | Lambdas | [Comparator als Lambda](Lambdas_001/readme.md) | [✅](Lambdas_001/solution/) |
|
||||
| 75. | Lambdas | [Callback mit Lambda realisieren](Lambdas_002/readme.md) | [✅](Lambdas_002/solution/) |
|
||||
| 76. | Geschachtelte Klassen | [MatrixSuche](Geschachtelte_Klassen_001/readme.md) | [✅](Geschachtelte_Klassen_001/solution/) |
|
||||
| 77. | Lambdas | [StringTransmogrifier](Lambdas_001/readme.md) | [✅](Lambdas_001/solution/) |
|
||||
| 78. | Lambdas | [StringTransmogrifier erweitern](Lambdas_002/readme.md) | [✅](Lambdas_002/solution/) |
|
||||
| 79. | Collections | [Iterator schreiben](Collections_001/readme.md) | |
|
||||
| 80. | Collections | [Iterator programmieren](Collections_002/readme.md) | |
|
||||
| 81. | Collections | [`List<T>` und dessen Implementierungen](Collections_003/readme.md) | |
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package pr2.lambda.array_sorter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
|
||||
public class ReverseSort {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Date[] dates = {
|
||||
new Date(9200000000L),
|
||||
new Date(9300000000L),
|
||||
new Date(92100000000L),
|
||||
new Date(9600000000L),
|
||||
new Date(93300000000L)
|
||||
};
|
||||
|
||||
Arrays.sort(dates, new Comparator<Date>() {
|
||||
|
||||
@Override
|
||||
public int compare(Date o1, Date o2) {
|
||||
return o1.compareTo(o2) * -1;
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println(Arrays.asList(dates));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package pr2.lambda.callback;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Hauptklasse.
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
private static final int[] ZAHLEN =
|
||||
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20};
|
||||
|
||||
/**
|
||||
* Hauptmethode.
|
||||
*
|
||||
* @param args Kommandozeilenargumente.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
NumberSelector s = new NumberSelector();
|
||||
|
||||
int[] gerade;
|
||||
|
||||
gerade = s.filter(new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean accept(Integer object) {
|
||||
return object % 2 == 0;
|
||||
}
|
||||
}, ZAHLEN);
|
||||
|
||||
System.out.println(Arrays.toString(gerade));
|
||||
|
||||
int[] ungerade;
|
||||
|
||||
ungerade = s.filter(new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean accept(Integer object) {
|
||||
return object % 2 != 0;
|
||||
}
|
||||
}, ZAHLEN);
|
||||
|
||||
System.out.println(Arrays.toString(ungerade));
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package pr2.lambda.callback;
|
||||
|
||||
/**
|
||||
* Auswählen von Zahlen mithilfe eines Callbacks.
|
||||
*/
|
||||
public class NumberSelector {
|
||||
|
||||
/**
|
||||
* Filtert die übergebenen Zahlen anhand des Prädikates.
|
||||
*
|
||||
* @param predicate Prädikat.
|
||||
* @param numbers die zu filternden Zahlen.
|
||||
* @return Das Ergebnis
|
||||
*/
|
||||
public int[] filter(Predicate<Integer> predicate, int[] numbers) {
|
||||
|
||||
int[] temp = new int[numbers.length];
|
||||
int count = 0;
|
||||
|
||||
for (int i : numbers) {
|
||||
if (predicate.accept(i)) {
|
||||
temp[count++] = i;
|
||||
}
|
||||
}
|
||||
|
||||
int[] result = new int[count];
|
||||
System.arraycopy(temp, 0, result, 0, count);
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package pr2.lambda.callback;
|
||||
|
||||
/**
|
||||
* Interface, um Entscheidung über ein Objekt zu
|
||||
* treffen.
|
||||
*
|
||||
* @param <T> Typ des Objekts
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Predicate<T> {
|
||||
|
||||
/**
|
||||
* Entscheidet, ob ein Objekt akzeptiert wird oder nicht.
|
||||
*
|
||||
* @param object das zu untersuchende Objekt
|
||||
* @return {@code true} wenn das Objekt akzeptiert wird,
|
||||
* andernfalls {@code false}.
|
||||
*/
|
||||
boolean accept(T object);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package pr2.lambda.comparator;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SortLambda {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Integer[] zahlen = {23, 42, 17, 9, 1, 5, 7, 88, 35};
|
||||
Arrays.sort(zahlen, (a, b) -> b - a);
|
||||
System.out.println(Arrays.toString(zahlen));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package pr2.lambda.lambdas;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Hauptklasse.
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
private static final int[] ZAHLEN =
|
||||
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20};
|
||||
|
||||
/**
|
||||
* Hauptmethode.
|
||||
*
|
||||
* @param args Kommandozeileargumente.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
NumberSelector s = new NumberSelector();
|
||||
|
||||
int[] gerade = s.filter((i) -> i % 2 == 0, ZAHLEN);
|
||||
|
||||
System.out.println(Arrays.toString(gerade));
|
||||
|
||||
int[] ungerade = s.filter((i) -> i % 2 != 0, ZAHLEN);
|
||||
|
||||
System.out.println(Arrays.toString(ungerade));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package pr2.lambda.lambdas;
|
||||
|
||||
/**
|
||||
* Auswählen von Zahlen mithilfe eines Callbacks.
|
||||
*/
|
||||
public class NumberSelector {
|
||||
|
||||
/**
|
||||
* Filtert die übergebenen Zahlen anhand des Prädikates.
|
||||
*
|
||||
* @param predicate Prädikat.
|
||||
* @param numbers die zu filternden Zahlen.
|
||||
* @return Das Ergebnis
|
||||
*/
|
||||
public int[] filter(Predicate<Integer> predicate, int[] numbers) {
|
||||
|
||||
int[] temp = new int[numbers.length];
|
||||
int count = 0;
|
||||
|
||||
for (int i : numbers) {
|
||||
if (predicate.accept(i)) {
|
||||
temp[count++] = i;
|
||||
}
|
||||
}
|
||||
|
||||
int[] result = new int[count];
|
||||
System.arraycopy(temp, 0, result, 0, count);
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package pr2.lambda.lambdas;
|
||||
|
||||
/**
|
||||
* Interface, um Entscheidung über ein Objekt zu
|
||||
* treffen.
|
||||
*
|
||||
* @param <T> Typ des Objekts
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Predicate<T> {
|
||||
|
||||
/**
|
||||
* Entscheidet, ob ein Objekt akzeptiert wird oder nicht.
|
||||
*
|
||||
* @param object das zu untersuchende Objekt
|
||||
* @return {@code true} wenn das Objekt akzeptiert wird,
|
||||
* andernfalls {@code false}.
|
||||
*/
|
||||
boolean accept(T object);
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package pr2.lambda.matrixsuche;
|
||||
|
||||
public class MatrixSuche {
|
||||
|
||||
public static class Position {
|
||||
private final int xPos;
|
||||
private final int yPos;
|
||||
|
||||
public Position(int xPos, int yPos) {
|
||||
this.xPos = xPos;
|
||||
this.yPos = yPos;
|
||||
}
|
||||
|
||||
public int getxPos() {
|
||||
return xPos;
|
||||
}
|
||||
|
||||
public int getyPos() {
|
||||
return yPos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("(%d, %d)", xPos, yPos);
|
||||
}
|
||||
}
|
||||
|
||||
public static Position findEntry(int[][] matrix, int wert) {
|
||||
for (int i = 0; i < matrix.length; i++) {
|
||||
for (int k = 0; k < matrix[i].length; k++) {
|
||||
if (matrix[i][k] == wert) {
|
||||
return new Position(i, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package pr2.lambda.matrixsuche.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.lambda.matrixsuche.MatrixSuche;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class MatrixSucheTest {
|
||||
|
||||
@Test
|
||||
void testSuche() {
|
||||
|
||||
int[][] matrix = {
|
||||
{3, 5, 6, 7, 8},
|
||||
{10, 12, 14, 16, 18},
|
||||
{23, 25, 26, 27, 28}
|
||||
};
|
||||
|
||||
Assertions.assertEquals("(0, 1)",
|
||||
MatrixSuche.findEntry(matrix, 5).toString());
|
||||
assertEquals("(2, 0)",
|
||||
MatrixSuche.findEntry(matrix, 23).toString());
|
||||
assertEquals("(1, 2)",
|
||||
MatrixSuche.findEntry(matrix, 14).toString());
|
||||
assertEquals("(0, 4)",
|
||||
MatrixSuche.findEntry(matrix, 8).toString());
|
||||
assertNull(
|
||||
MatrixSuche.findEntry(matrix, 99));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package pr2.lambda.mogrifier_1;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface StringFunction {
|
||||
String apply(String s);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package pr2.lambda.mogrifier_1;
|
||||
|
||||
public class StringTransmogrifier {
|
||||
|
||||
public static String[] transmogrify(String[] elements, StringFunction t) {
|
||||
String[] result = new String[elements.length];
|
||||
|
||||
for (int i = 0; i < elements.length; i++) {
|
||||
result[i] = t.apply(elements[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package pr2.lambda.mogrifier_1.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.lambda.mogrifier_1.StringTransmogrifier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
public class StringTransmogrifierTest {
|
||||
|
||||
@Test
|
||||
void testMogrification() {
|
||||
String[] elements = {"Hello", "World"};
|
||||
|
||||
Assertions.assertArrayEquals(new String[] { "hello", "world"},
|
||||
StringTransmogrifier.transmogrify(elements,
|
||||
String::toLowerCase));
|
||||
|
||||
assertArrayEquals(new String[] { "HELLO", "WORLD"},
|
||||
StringTransmogrifier.transmogrify(elements,
|
||||
String::toUpperCase));
|
||||
|
||||
assertArrayEquals(new String[] { "Ifmmp", "Xpsme"},
|
||||
StringTransmogrifier.transmogrify(elements,
|
||||
s -> {
|
||||
char[] c = s.toCharArray();
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
c[i] = (char) (c[i] + 1);
|
||||
}
|
||||
return new String(c);
|
||||
}));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package pr2.lambda.mogrifier_2;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface StringFunction {
|
||||
static StringFunction caesar(final int shift) {
|
||||
return (s) -> shiftCharacters(s, shift);
|
||||
}
|
||||
|
||||
static String shiftCharacters(String s, int shift) {
|
||||
char[] c = s.toCharArray();
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
c[i] = (char) (c[i] + shift);
|
||||
}
|
||||
return new String(c);
|
||||
}
|
||||
|
||||
String apply(String s);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package pr2.lambda.mogrifier_2;
|
||||
|
||||
public class StringTransmogrifier {
|
||||
|
||||
public static String[] transmogrify(String[] elements, StringFunction t) {
|
||||
String[] result = new String[elements.length];
|
||||
|
||||
for (int i = 0; i < elements.length; i++) {
|
||||
result[i] = t.apply(elements[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package pr2.lambda.mogrifier_2.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.lambda.mogrifier_2.StringFunction;
|
||||
import pr2.lambda.mogrifier_2.StringTransmogrifier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
public class StringTransmogrifierTest {
|
||||
|
||||
static final String[] ELEMENTS = {"Hello", "World"};
|
||||
|
||||
@Test
|
||||
void testMogrification() {
|
||||
|
||||
|
||||
Assertions.assertArrayEquals(new String[] {"hello", "world"},
|
||||
StringTransmogrifier.transmogrify(ELEMENTS,
|
||||
String::toLowerCase));
|
||||
|
||||
assertArrayEquals(new String[] {"HELLO", "WORLD"},
|
||||
StringTransmogrifier.transmogrify(ELEMENTS,
|
||||
String::toUpperCase));
|
||||
|
||||
assertArrayEquals(new String[] {"Ifmmp", "Xpsme"},
|
||||
StringTransmogrifier.transmogrify(ELEMENTS,
|
||||
s -> {
|
||||
char[] c = s.toCharArray();
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
c[i] = (char) (c[i] + 1);
|
||||
}
|
||||
return new String(c);
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCaesar() {
|
||||
assertArrayEquals(new String[] {"Khoor", "Zruog"},
|
||||
StringTransmogrifier.transmogrify(ELEMENTS,
|
||||
StringFunction.caesar(3)));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package pr2.lambda.observer;
|
||||
|
||||
public interface Beobachter {
|
||||
int getValue();
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
|
||||
package pr2.lambda.observer;
|
||||
|
||||
public class Datenhalter {
|
||||
|
||||
private final int geheimerWert;
|
||||
|
||||
public Datenhalter(int geheimerWert) {
|
||||
this.geheimerWert = geheimerWert;
|
||||
}
|
||||
|
||||
public Beobachter getBeobachter() {
|
||||
return new BeobachterImpl();
|
||||
}
|
||||
|
||||
private class BeobachterImpl implements Beobachter {
|
||||
|
||||
@Override
|
||||
public int getValue() {
|
||||
return geheimerWert;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
package pr2.lambda.observer;
|
||||
|
||||
public class DatenhalterAnonym {
|
||||
|
||||
private final int geheimerWert;
|
||||
|
||||
public DatenhalterAnonym(int geheimerWert) {
|
||||
this.geheimerWert = geheimerWert;
|
||||
}
|
||||
|
||||
public Beobachter getBeobachter() {
|
||||
return new Beobachter() {
|
||||
@Override
|
||||
public int getValue() {
|
||||
return geheimerWert;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
|
||||
package pr2.lambda.observer.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.lambda.observer.Beobachter;
|
||||
import pr2.lambda.observer.Datenhalter;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class BeobachterTest {
|
||||
|
||||
@Test
|
||||
void testBeobachter() {
|
||||
Datenhalter dh = new Datenhalter(42);
|
||||
Beobachter b = dh.getBeobachter();
|
||||
assertEquals(42, b.getValue());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue