From 083c15bf8b15c2445e4af8fd751ee686f302ab58 Mon Sep 17 00:00:00 2001 From: teridax Date: Wed, 26 Apr 2023 20:36:43 +0200 Subject: [PATCH] added blatt 4 and restructured --- .idea/modules.xml | 7 +- .idea/uiDesigner.xml | 124 +++++++++++++ {GgtUndKgv => AufgabenBlatt3}/.gitignore | 0 AufgabenBlatt3/AufgabenBlatt3.iml | 21 +++ .../src/Aufgabe3.java | 2 +- AufgabenBlatt3/src/Aufgabe4.java | 44 +++++ .../src/Aufgabe6.java | 2 +- .../src/Aufgabe7.java | 4 +- .../src/Aufgabe8.java | 2 +- .../src/aufgabe5}/Main.java | 45 +---- AufgabenBlatt3/src/aufgabe5/Rechnen.java | 44 +++++ {Konto => AufgabenBlatt4}/.gitignore | 0 AufgabenBlatt4/AufgabenBlatt4.iml | 21 +++ AufgabenBlatt4/src/Aufgabe2.java | 40 +++++ AufgabenBlatt4/src/Aufgabe3.java | 58 ++++++ AufgabenBlatt4/src/Aufgabe4.java | 44 +++++ AufgabenBlatt4/src/Aufgabe5.java | 66 +++++++ AufgabenBlatt4/src/Aufgabe6.java | 57 ++++++ AufgabenBlatt4/src/Aufgabe7.java | 169 ++++++++++++++++++ GgtUndKgv/GgtUndKgv.iml | 11 -- Konto/Konto.iml | 11 -- Maximum/.gitignore | 29 --- Maximum/Maximum.iml | 11 -- Verzinsung/.gitignore | 29 --- Verzinsung/Verzinsung.iml | 11 -- Zaubern/.gitignore | 29 --- Zaubern/Zaubern.iml | 11 -- 27 files changed, 697 insertions(+), 195 deletions(-) create mode 100644 .idea/uiDesigner.xml rename {GgtUndKgv => AufgabenBlatt3}/.gitignore (100%) create mode 100644 AufgabenBlatt3/AufgabenBlatt3.iml rename Maximum/src/Main.java => AufgabenBlatt3/src/Aufgabe3.java (98%) create mode 100644 AufgabenBlatt3/src/Aufgabe4.java rename Zaubern/src/Main.java => AufgabenBlatt3/src/Aufgabe6.java (99%) rename Konto/src/Konto.java => AufgabenBlatt3/src/Aufgabe7.java (97%) rename Verzinsung/src/Main.java => AufgabenBlatt3/src/Aufgabe8.java (99%) rename {GgtUndKgv/src => AufgabenBlatt3/src/aufgabe5}/Main.java (64%) create mode 100644 AufgabenBlatt3/src/aufgabe5/Rechnen.java rename {Konto => AufgabenBlatt4}/.gitignore (100%) create mode 100644 AufgabenBlatt4/AufgabenBlatt4.iml create mode 100644 AufgabenBlatt4/src/Aufgabe2.java create mode 100644 AufgabenBlatt4/src/Aufgabe3.java create mode 100644 AufgabenBlatt4/src/Aufgabe4.java create mode 100644 AufgabenBlatt4/src/Aufgabe5.java create mode 100644 AufgabenBlatt4/src/Aufgabe6.java create mode 100644 AufgabenBlatt4/src/Aufgabe7.java delete mode 100644 GgtUndKgv/GgtUndKgv.iml delete mode 100644 Konto/Konto.iml delete mode 100644 Maximum/.gitignore delete mode 100644 Maximum/Maximum.iml delete mode 100644 Verzinsung/.gitignore delete mode 100644 Verzinsung/Verzinsung.iml delete mode 100644 Zaubern/.gitignore delete mode 100644 Zaubern/Zaubern.iml diff --git a/.idea/modules.xml b/.idea/modules.xml index 2ff033c..76cf03a 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,13 +2,10 @@ - + + - - - - \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/GgtUndKgv/.gitignore b/AufgabenBlatt3/.gitignore similarity index 100% rename from GgtUndKgv/.gitignore rename to AufgabenBlatt3/.gitignore diff --git a/AufgabenBlatt3/AufgabenBlatt3.iml b/AufgabenBlatt3/AufgabenBlatt3.iml new file mode 100644 index 0000000..1dc35ea --- /dev/null +++ b/AufgabenBlatt3/AufgabenBlatt3.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Maximum/src/Main.java b/AufgabenBlatt3/src/Aufgabe3.java similarity index 98% rename from Maximum/src/Main.java rename to AufgabenBlatt3/src/Aufgabe3.java index ccbf87d..a29439a 100644 --- a/Maximum/src/Main.java +++ b/AufgabenBlatt3/src/Aufgabe3.java @@ -16,7 +16,7 @@ * Licensed under the GPLv2 License, Version 2.0 (the "License"); * Copyright (c) Sven Vogel */ -public class Main { +public class Aufgabe3 { /** * compute the maximum of a and b diff --git a/AufgabenBlatt3/src/Aufgabe4.java b/AufgabenBlatt3/src/Aufgabe4.java new file mode 100644 index 0000000..3ff580a --- /dev/null +++ b/AufgabenBlatt3/src/Aufgabe4.java @@ -0,0 +1,44 @@ +import org.junit.Test; + +import static junit.framework.TestCase.*; + +/** + * Generic test class for implementing and testing + * a simple boolean majority function + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe4 { + + /** + * Returns true if at least one of the parameters is true + * @param a + * @param b + * @param c + * @return the bitwise OR of the parameters + */ + private static boolean majority(boolean a, boolean b, boolean c) { + return a || b || c; + } + + @Test + public void test() { + assertTrue(majority(true, true, true)); + assertTrue(majority(true, false, true)); + assertTrue(majority(true, true, false)); + assertTrue(majority(true, false, false)); + assertFalse(majority(false, false, false)); + } +} diff --git a/Zaubern/src/Main.java b/AufgabenBlatt3/src/Aufgabe6.java similarity index 99% rename from Zaubern/src/Main.java rename to AufgabenBlatt3/src/Aufgabe6.java index 3c7f972..93bc37a 100644 --- a/Zaubern/src/Main.java +++ b/AufgabenBlatt3/src/Aufgabe6.java @@ -21,7 +21,7 @@ import java.util.Random; * NOTE: following code mostly copied from the teacher, * expect no comments or functionality */ -public class Main { +public class Aufgabe6 { enum Zauberspruch { ENTWAFFNUNGSZAUBER, diff --git a/Konto/src/Konto.java b/AufgabenBlatt3/src/Aufgabe7.java similarity index 97% rename from Konto/src/Konto.java rename to AufgabenBlatt3/src/Aufgabe7.java index 53dcb7d..f7cd2a8 100644 --- a/Konto/src/Konto.java +++ b/AufgabenBlatt3/src/Aufgabe7.java @@ -16,7 +16,7 @@ * Licensed under the GPLv2 License, Version 2.0 (the "License"); * Copyright (c) Sven Vogel */ -public class Konto { +public class Aufgabe7 { private double guthaben; @@ -58,7 +58,7 @@ public class Konto { } public static void main(String[] args) { - var konto = new Konto(); + var konto = new Aufgabe7(); konto.einzahlen(134); konto.einzahlen(0.345); diff --git a/Verzinsung/src/Main.java b/AufgabenBlatt3/src/Aufgabe8.java similarity index 99% rename from Verzinsung/src/Main.java rename to AufgabenBlatt3/src/Aufgabe8.java index 0c42690..72125d1 100644 --- a/Verzinsung/src/Main.java +++ b/AufgabenBlatt3/src/Aufgabe8.java @@ -18,7 +18,7 @@ import javax.swing.*; * Licensed under the GPLv2 License, Version 2.0 (the "License"); * Copyright (c) Sven Vogel */ -public class Main { +public class Aufgabe8 { /** * calculate the linear zins over a span of years diff --git a/GgtUndKgv/src/Main.java b/AufgabenBlatt3/src/aufgabe5/Main.java similarity index 64% rename from GgtUndKgv/src/Main.java rename to AufgabenBlatt3/src/aufgabe5/Main.java index 83a3eda..c7b8c34 100644 --- a/GgtUndKgv/src/Main.java +++ b/AufgabenBlatt3/src/aufgabe5/Main.java @@ -1,48 +1,7 @@ +package aufgabe5; + import javax.swing.*; -/** - * Generic test class for implementing - * ggt and kgv - * _ _ _ _ - * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ - * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | - * \ V V /| | | | |_| || __/ | | | | |_) | |_| | - * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | - * |___/ - * ____ __ __ _ - * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | - * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | - * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | - * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| - * |___/ - * Licensed under the GPLv2 License, Version 2.0 (the "License"); - * Copyright (c) Sven Vogel - */ -class Rechnen { - /** - * compute the greatest common divisor of x and y - * @param x the first number to compute the greatest common divisor of - * @param y the second number to compute the greatest common divisor of - * @return the greatest common divisor of x and y - */ - public static int ggt(int x, int y) { - if (x == 0) - return y; - - return ggt(x, y % x); - } - - /** - * compute the smallest common multiple of x and y - * @param x the first number to compute the smallest common multiple of - * @param y the second number to compute the smallest common multiple of - * @return the smallest common multiple of x and y - */ - public static int kgv(int x, int y) { - return (x * y) / ggt(x, y); - } -} - /** * Generic test class for testing * kgv and ggt diff --git a/AufgabenBlatt3/src/aufgabe5/Rechnen.java b/AufgabenBlatt3/src/aufgabe5/Rechnen.java new file mode 100644 index 0000000..7f6cc35 --- /dev/null +++ b/AufgabenBlatt3/src/aufgabe5/Rechnen.java @@ -0,0 +1,44 @@ +package aufgabe5; + +/** + * Generic test class for implementing + * ggt and kgv + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Rechnen { + /** + * compute the greatest common divisor of x and y + * @param x the first number to compute the greatest common divisor of + * @param y the second number to compute the greatest common divisor of + * @return the greatest common divisor of x and y + */ + public static int ggt(int x, int y) { + if (y == 0) + return x; + + return ggt(y, y % x); + } + + /** + * compute the smallest common multiple of x and y + * @param x the first number to compute the smallest common multiple of + * @param y the second number to compute the smallest common multiple of + * @return the smallest common multiple of x and y + */ + public static int kgv(int x, int y) { + return (x * y) / ggt(x, y); + } +} \ No newline at end of file diff --git a/Konto/.gitignore b/AufgabenBlatt4/.gitignore similarity index 100% rename from Konto/.gitignore rename to AufgabenBlatt4/.gitignore diff --git a/AufgabenBlatt4/AufgabenBlatt4.iml b/AufgabenBlatt4/AufgabenBlatt4.iml new file mode 100644 index 0000000..1dc35ea --- /dev/null +++ b/AufgabenBlatt4/AufgabenBlatt4.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AufgabenBlatt4/src/Aufgabe2.java b/AufgabenBlatt4/src/Aufgabe2.java new file mode 100644 index 0000000..4641169 --- /dev/null +++ b/AufgabenBlatt4/src/Aufgabe2.java @@ -0,0 +1,40 @@ +import org.junit.Test; + +/** + * Generic test class for implementing and testing repeating strings + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe2 { + + /** + * Repeat the supplied character by a given number + * @param symbol the character to repeat + * @param number number of repetitions + * @return the repeated characters as string + */ + private static String repeat(char symbol, int number) { + if (number == 0) { + return String.valueOf(symbol); + } + return symbol + repeat(symbol, number - 1); + } + + @Test + public void test() { + var result = repeat('a', 78); + System.out.println(result); + } +} diff --git a/AufgabenBlatt4/src/Aufgabe3.java b/AufgabenBlatt4/src/Aufgabe3.java new file mode 100644 index 0000000..929beb1 --- /dev/null +++ b/AufgabenBlatt4/src/Aufgabe3.java @@ -0,0 +1,58 @@ +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; + +/** + * Generic test class for implementing and testing fibonacci algorithms + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe3 { + /** + * calculate the fibonacci value of any given element by recursion + * @param x the index of the fibonacci sequence + * @return the fibonacci value for the given index + */ + private static int fibonacciRecursive(int x) { + return x < 2 ? x : fibonacciRecursive(x - 1) + fibonacciRecursive(x - 2); + } + + /** + * calculate the fibonacci value of any given element by iteration + * @param x the index of the fibonacci sequence + * @return the fibonacci value for the given index + */ + private static int fibonacciIterative(int x) { + int sum = 0; + int sum2 = 1; + + for (int y = 0; y < (x - 1); y++) { + int t = sum; + sum = sum2; + sum2 = t + sum; + } + return sum2; + } + + @Test + public void test() { + System.out.println("recursive: " + fibonacciRecursive(7)); + System.out.println("iterative: " + fibonacciIterative(7)); + + assertEquals(fibonacciRecursive(7), fibonacciIterative(7)); + assertEquals(fibonacciRecursive(68), fibonacciIterative(68)); + assertEquals(fibonacciRecursive(13), fibonacciIterative(13)); + } +} diff --git a/AufgabenBlatt4/src/Aufgabe4.java b/AufgabenBlatt4/src/Aufgabe4.java new file mode 100644 index 0000000..cdda44c --- /dev/null +++ b/AufgabenBlatt4/src/Aufgabe4.java @@ -0,0 +1,44 @@ +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; + +/** + * Generic test class for implementing and testing an algorithm to reverse strings + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe4 { + + /** + * Dreht ein übergebenes Wort um und gibt das Ergebnis + * als einen neuen String zurück. + * Beispiel: Bei dem Argument "katze" ist die Rückgabe "eztak". + * + * @pre wort != null und mindestens 1 Zeichen lang. + * @param wort Wort, das umgedreht werden soll. + * @return Umgedrehtes Wort. + */ + public static String dreheWortUm(String wort) { + if (wort.length() < 2) { + return wort; + } + return dreheWortUm(wort.substring(1)) + wort.charAt(0); + } + + @Test + public void test() { + assertEquals(dreheWortUm("abcdefghijklmnopqrstuvwxyz"), "zyxwvutsrqponmlkjihgfedcba"); + } +} diff --git a/AufgabenBlatt4/src/Aufgabe5.java b/AufgabenBlatt4/src/Aufgabe5.java new file mode 100644 index 0000000..64fe7c7 --- /dev/null +++ b/AufgabenBlatt4/src/Aufgabe5.java @@ -0,0 +1,66 @@ +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; + +/** + * Generic test class for implementing and testing a sequence in recursive and iterative form + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe5 { + + /** + * Calculate some recursive sequence + * @param n the number to calculate the sequence of + * @return the value for the given number of the sequence + */ + private static int recursiveFunction(int n) { + if (n <= 0) { + return 1; + } else if (n == 1) { + return 2; + } + + return 4 * recursiveFunction(n - 2); + } + + /** + * Calculate some iterative sequence + * @param n the number to calculate the sequence of + * @return the value for the given number of the sequence + */ + private static int iterativeFunction(int n) { + if (n <= 0) { + return 1; + } else if (n == 1) { + return 2; + } + + int sum = 1; + for (int i = 2; i < n; i += 2) { + sum *= 4; + } + + return sum; + } + + @Test + public void test() { + assertEquals(recursiveFunction(0), 1); + assertEquals(recursiveFunction(1), 2); + System.out.println("greater than 1: " + recursiveFunction(12)); + assertEquals(recursiveFunction(67), iterativeFunction(67)); + } +} diff --git a/AufgabenBlatt4/src/Aufgabe6.java b/AufgabenBlatt4/src/Aufgabe6.java new file mode 100644 index 0000000..585c510 --- /dev/null +++ b/AufgabenBlatt4/src/Aufgabe6.java @@ -0,0 +1,57 @@ +import java.util.Scanner; + +/** + * Generic test class for implementing and testing listing all permutations of a given number of characters + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe6 { + + /** + * Print all permutations of every character from the beginning of the alphabet to the N-th character + * @param n the index of the highest character to use + */ + private static void printPermutations(int n) { + if (n < 0 || n > 26) { + throw new IllegalArgumentException("N must be between 0 and 26"); + } + + final var LOOKUP = "abcdefghijklmnopqrstuvwxyz"; + + for (int i = 0; i < n; i++) { + + var builder = new StringBuilder(); + // rotate characters + for (int k = 0; k < n; k++) { + var idx = (i + k) % n; + builder.append(LOOKUP.charAt(idx)); + } + System.out.println(builder); + + // reverse rotated characters + builder.reverse(); + + System.out.println(builder); + } + } + + public static void main(String[] args) { + try(var scanner = new Scanner(System.in)) { + var n = scanner.nextInt(10); + + printPermutations(n); + } + } +} diff --git a/AufgabenBlatt4/src/Aufgabe7.java b/AufgabenBlatt4/src/Aufgabe7.java new file mode 100644 index 0000000..11295fd --- /dev/null +++ b/AufgabenBlatt4/src/Aufgabe7.java @@ -0,0 +1,169 @@ +import org.junit.Test; + +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; + +/** + * Generic test class for implementing and testing palindrome detection + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe7 { + + /** + * Tests whether the supplied string is a palindrome or not using iterations + * + * @implNote returns true if the supplied string is smaller than 2 or blank + * @param text the string to test + * @return true if the supplied string is a palindrome, false otherwise + */ + private static boolean isPalindrome(String text) { + if (text.isBlank() || text.length() < 2) + return true; + + for (int i = 0; i < text.length() / 2; i++) { + if (text.charAt(i) != text.charAt(text.length() - i - 1)) + return false; + } + + return true; + } + + /** + * Tests whether the supplied string is a palindrome or not using recursion + * + * @implNote returns true if the supplied string is smaller than 2 or blank + * @param text the string to test + * @return true if the supplied string is a palindrome, false otherwise + */ + private static boolean isPalindromeRecursive(String text) { + if (text.isBlank() || text.length() < 2) + return true; + + if (text.charAt(0) != text.charAt(text.length() - 1)) + return false; + + return isPalindromeRecursive(text.substring(1, text.length() - 1)); + } + + /** + * Tests whether the supplied string is a palindrome or not by iteration + * also ignores case + * + * @implNote returns true if the supplied string is smaller than 2 or blank + * @param text the string to test + * @return true if the supplied string is a palindrome, false otherwise + */ + private static boolean isPalindromeIgnoreCase(String text) { + if (text.isBlank() || text.length() < 2) + return true; + + var lowerCase = text.toLowerCase(); + + for (int i = 0; i < text.length() / 2; i++) { + if (lowerCase.charAt(i) != lowerCase.charAt(text.length() - i - 1)) + return false; + } + + return true; + } + + /** + * Test iterative palindrome detection + */ + @Test + public void testLowercaseIterative() { + var PALINDROMES = new String[]{ + "otto", + "121", + "regallager", + "reliefpfeiler" + }; + + // palindromes + for (var palindrome : PALINDROMES) { + assertTrue(isPalindrome(palindrome)); + } + + var NO_PALINDROMES = new String[]{ + "abcdefghijklmnopqrstuvwxyz", + "sven vogel", + "koeffizient" + }; + + // no palindromes + for (var noPalindrome : NO_PALINDROMES) { + assertFalse(isPalindrome(noPalindrome)); + } + } + + /** + * Test recursive palindrome detection + */ + @Test + public void testLowercaseRecursive() { + var PALINDROMES = new String[]{ + "otto", + "121", + "regallager", + "reliefpfeiler" + }; + + // palindromes + for (var palindrome : PALINDROMES) { + assertTrue(isPalindromeRecursive(palindrome)); + } + + var NO_PALINDROMES = new String[]{ + "abcdefghijklmnopqrstuvwxyz", + "sven vogel", + "koeffizient" + }; + + // no palindromes + for (var noPalindrome : NO_PALINDROMES) { + assertFalse(isPalindromeRecursive(noPalindrome)); + } + } + + /** + * Test iterative palindrome detection with ignore case + */ + @Test + public void testIgnoreCaseIterative() { + var PALINDROMES = new String[]{ + "OtTo", + "121", + "ReGaLlAgeR", + "reLiEfPfeIler" + }; + + // palindromes + for (var palindrome : PALINDROMES) { + assertTrue(isPalindromeIgnoreCase(palindrome)); + } + + var NO_PALINDROMES = new String[]{ + "AbcdeFghijKlmnOpqRstUvwxyz", + "sVeN vOgeL", + "kOefFiZiEnt" + }; + + // no palindromes + for (var noPalindrome : NO_PALINDROMES) { + assertFalse(isPalindromeIgnoreCase(noPalindrome)); + } + } +} diff --git a/GgtUndKgv/GgtUndKgv.iml b/GgtUndKgv/GgtUndKgv.iml deleted file mode 100644 index 9c8aa59..0000000 --- a/GgtUndKgv/GgtUndKgv.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/Konto/Konto.iml b/Konto/Konto.iml deleted file mode 100644 index 9c8aa59..0000000 --- a/Konto/Konto.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/Maximum/.gitignore b/Maximum/.gitignore deleted file mode 100644 index f68d109..0000000 --- a/Maximum/.gitignore +++ /dev/null @@ -1,29 +0,0 @@ -### IntelliJ IDEA ### -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store \ No newline at end of file diff --git a/Maximum/Maximum.iml b/Maximum/Maximum.iml deleted file mode 100644 index 9c8aa59..0000000 --- a/Maximum/Maximum.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/Verzinsung/.gitignore b/Verzinsung/.gitignore deleted file mode 100644 index f68d109..0000000 --- a/Verzinsung/.gitignore +++ /dev/null @@ -1,29 +0,0 @@ -### IntelliJ IDEA ### -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store \ No newline at end of file diff --git a/Verzinsung/Verzinsung.iml b/Verzinsung/Verzinsung.iml deleted file mode 100644 index 9c8aa59..0000000 --- a/Verzinsung/Verzinsung.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/Zaubern/.gitignore b/Zaubern/.gitignore deleted file mode 100644 index f68d109..0000000 --- a/Zaubern/.gitignore +++ /dev/null @@ -1,29 +0,0 @@ -### IntelliJ IDEA ### -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store \ No newline at end of file diff --git a/Zaubern/Zaubern.iml b/Zaubern/Zaubern.iml deleted file mode 100644 index 9c8aa59..0000000 --- a/Zaubern/Zaubern.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file