1
0
Fork 0

Update of exercises

master
Thomas Smits 2023-11-14 11:06:59 +01:00
parent 054e9d0f9b
commit e5816459dd
19 changed files with 176 additions and 36 deletions

View File

@ -1,6 +1,6 @@
# Labeled Break
[Musterlösung](solution/)
## Lernziel

View File

@ -0,0 +1,4 @@
# Lösung: Labeled Break
[pr2.auffrischung.labeled_break](../../solutions/src/main/java/pr2/auffrischung/labeled_break)

View File

@ -1,6 +1,6 @@
# Passwortbewertung
[Musterlösung](solution/)
## Lernziel

View File

@ -0,0 +1,4 @@
# Lösung: Passwortbewertung
[pr2.auffrischung.password](../../solutions/src/main/java/pr2/auffrischung/password)

View File

@ -1,6 +1,6 @@
# printf mit Formatstring
[Musterlösung](solution/)
## Lernziel

View File

@ -0,0 +1,4 @@
# Lösung: printf mit Formatstring
[pr2.auffrischung.printf](../../solutions/src/main/java/pr2/auffrischung/printf)

View File

@ -1,6 +1,6 @@
# Maximum in einem Array suchen
[Musterlösung](solution/)
## Lernziel

View File

@ -0,0 +1,4 @@
# Lösung: Maximum in einem Array suchen
[pr2.auffrischung.suchemax](../../solutions/src/main/java/pr2/auffrischung/suchemax)

View File

@ -1,6 +1,6 @@
# Taschenrechner
[Musterlösung](solution/)
## Lernziel

View File

@ -0,0 +1,4 @@
# Lösung: Taschenrechner
[pr2.auffrischung.taschenrechner](../../solutions/src/main/java/pr2/auffrischung/taschenrechner)

View File

@ -1,6 +1,6 @@
# Quine
[Musterlösung](solution/)
## Lernziel

View File

@ -1,5 +0,0 @@
# Lösung: Quine
Musterlösung:
[pr2.auffrischung.quine](../../solutions/src/main/java/pr2/auffrischung/quine/)

View File

@ -11,12 +11,12 @@ Hinweise zur nötigen Softwareausstattung finden Sie [hier](help/softwareausstat
| # | Ausgabe | Thema | Musterlösung |
|----|---------------------------------------------------------------------------------------------|----------------|----------------|
| 1. | n/a | [String in Großbuchstaben umwandeln](Assignment_001/readme.md) | [](Assignment_001/solution/readme.md) |
| 2. | n/a | [Labeled Break](Assignment_002/readme.md) | |
| 3. | n/a | [Passwortbewertung](Assignment_003/readme.md) | |
| 4. | n/a | [printf mit Formatstring](Assignment_004/readme.md) | |
| 5. | n/a | [Maximum in einem Array suchen](Assignment_005/readme.md) | |
| 6. | n/a | [Taschenrechner](Assignment_006/readme.md) | |
| 7. | n/a | [Quine](Assignment_007/readme.md) | [](Assignment_007/solution/readme.md) |
| 2. | n/a | [Labeled Break](Assignment_002/readme.md) | [](Assignment_002/solution/readme.md) |
| 3. | n/a | [Passwortbewertung](Assignment_003/readme.md) | [](Assignment_003/solution/readme.md) |
| 4. | n/a | [printf mit Formatstring](Assignment_004/readme.md) | [](Assignment_004/solution/readme.md) |
| 5. | n/a | [Maximum in einem Array suchen](Assignment_005/readme.md) | [](Assignment_005/solution/readme.md) |
| 6. | n/a | [Taschenrechner](Assignment_006/readme.md) | [](Assignment_006/solution/readme.md) |
| 7. | n/a | [Quine](Assignment_007/readme.md) | |
| 8. | n/a | [Java-Coding-Standard anwenden](Assignment_008/readme.md) | |
| 9. | n/a | [JavaDoc schreiben](Assignment_009/readme.md) | |
| 10. | n/a | [Klasse mit JUnit testen](Assignment_010/readme.md) | |

View File

@ -0,0 +1,27 @@
package pr2.auffrischung.labeled_break;
public class ArraySucher {
/**
* Sucht das erste Element, dass nicht 0 ist.
*
* @param array das Array in dem gesucht werden soll
* @return {@code true}, wenn ein Element gefunden wird,
* andernfalls {@code false}.
*/
public boolean suche(int[][] array) {
boolean found = false;
outer:
for (int[] is : array) {
for (int i : is) {
if (i != 0) {
found = true;
break outer;
}
}
}
return found;
}
}

View File

@ -0,0 +1,53 @@
package pr2.auffrischung.password;
public class PasswortChecker {
private static final char[] NUMBERS = "1234567890".toCharArray();
private static final char[] LOWERCASE
= "abcdefghijklmnopqrstuvwxyz".toCharArray();
private static final char[] UPPERCASE
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static final char[] SPECIAL_CHARS = "!%()\"".toCharArray();
private static boolean contains(String string, char[] chars) {
for (char c : chars) {
if (string.indexOf(c) >= 0) {
return true;
}
}
return false;
}
public static int checkPassword(String password) {
int points = 0;
if (password.length() >= 8) {
points++;
}
if (contains(password, NUMBERS)
&& (contains(password, LOWERCASE)
|| contains(password, UPPERCASE))) {
points++;
}
if (contains(password, SPECIAL_CHARS)) {
points++;
}
if (contains(password, LOWERCASE) && contains(password, UPPERCASE)) {
points++;
}
return points;
}
public static void main(String[] args) {
System.out.println(checkPassword("mutti"));
System.out.println(checkPassword("Mutti"));
System.out.println(checkPassword("mutti123"));
System.out.println(checkPassword("Mutti123"));
System.out.println(checkPassword("Mutti123!%"));
System.out.println(checkPassword("1234"));
}
}

View File

@ -0,0 +1,18 @@
package pr2.auffrischung.printf;
public class DoubleFormatter {
public static void printDouble(double d) {
System.out.printf("%.3f%n", d);
}
public static void main(String[] args) {
printDouble(1.0);
printDouble(10.1);
printDouble(2.01);
printDouble(2.001);
printDouble(2.0001);
printDouble(2.0004);
printDouble(2.0005);
}
}

View File

@ -1,19 +0,0 @@
package pr2.auffrischung.quine;
public class Quine {
private static final String DATA = "package pr2.auffrischung.quine;%n%n"
+ "public class Quine {%n%n"
+ " private static final String DATA = \"%s\";%n%n"
+ " public static void main(String[] args) {%n"
+ " System.out.printf(data, %n"
+ " DATA.replace(\"\\\\\", \"\\\\\\\\\")%n"
+ " .replace(\"\\\"\", \"\\\\\\\"\"));%n }%n}";
public static void main(String[] args) {
System.out.printf(DATA,
DATA.replace("\\", "\\\\")
.replace("\"", "\\\""));
}
}

View File

@ -0,0 +1,21 @@
package pr2.auffrischung.suchemax;
public class GroessteZahl {
public int sucheMax(int[] zahlen) {
int max = Integer.MIN_VALUE;
for (int zahl : zahlen) {
max = Math.max(max, zahl);
}
return max;
}
public static void main(String[] args) {
GroessteZahl g = new GroessteZahl();
System.out.println(g.sucheMax(new int[] {1, 5, 8, 2, 0}));
System.out.println(g.sucheMax(new int[] {-1, -5, -8, -2, -20}));
System.out.println(g.sucheMax(new int[] {10000, -10000, 1, 2, 33}));
}
}

View File

@ -0,0 +1,25 @@
package pr2.auffrischung.taschenrechner;
public class Taschenrechner {
public double rechne(double o1, char op, double o2) {
return switch (op) {
case '+' -> o1 + o2;
case '-' -> o1 - o2;
case '*' -> o1 * o2;
case '/' -> o1 / o2;
case '^' -> Math.pow(o1, o2);
default -> Double.NaN;
};
// return 0.0;
}
public static void main(String[] args) {
Taschenrechner t = new Taschenrechner();
System.out.println(t.rechne(1, '+', 2));
System.out.println(t.rechne(1, '-', 2));
System.out.println(t.rechne(2, '*', 2));
System.out.println(t.rechne(4, '/', 2));
System.out.println(t.rechne(2, '^', 32));
}
}