diff --git a/.idea/modules.xml b/.idea/modules.xml index 77d0d57..355059e 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + diff --git a/AufgabenBlatt11/.gitignore b/AufgabenBlatt11/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/AufgabenBlatt11/.gitignore @@ -0,0 +1,29 @@ +### 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/AufgabenBlatt11/AufgabenBlatt11.iml b/AufgabenBlatt11/AufgabenBlatt11.iml new file mode 100644 index 0000000..1dc35ea --- /dev/null +++ b/AufgabenBlatt11/AufgabenBlatt11.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AufgabenBlatt11/src/aufgabe2/Biene.java b/AufgabenBlatt11/src/aufgabe2/Biene.java new file mode 100644 index 0000000..a3b5517 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe2/Biene.java @@ -0,0 +1,26 @@ +package aufgabe2; + +/** + * Generic test class defining a bee able to fly through Fliegen + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Biene implements Fliegen { + + @Override + public void fliegen() { + System.out.println("Summsumm"); + } +} diff --git a/AufgabenBlatt11/src/aufgabe2/Fliegen.java b/AufgabenBlatt11/src/aufgabe2/Fliegen.java new file mode 100644 index 0000000..442f11b --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe2/Fliegen.java @@ -0,0 +1,26 @@ +package aufgabe2; + +/** + * Generic test class defining the ability to fly + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public interface Fliegen { + + /** + * fly + */ + void fliegen(); +} diff --git a/AufgabenBlatt11/src/aufgabe2/Main.java b/AufgabenBlatt11/src/aufgabe2/Main.java new file mode 100644 index 0000000..b711a1f --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe2/Main.java @@ -0,0 +1,37 @@ +package aufgabe2; + +/** + * Generic test class for testing the Fliegen interface + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public final class Main { + + /** + * Calls fliegen() on the given Object + * @param dickerBrummer the object to call fliegen() on + */ + private static void abflug(Fliegen dickerBrummer) { + dickerBrummer.fliegen(); + } + + public static void main(String[] args) { + var maja = new Biene(); + var gunter = new Taube(); + + abflug(maja); + abflug(gunter); + } +} diff --git a/AufgabenBlatt11/src/aufgabe2/Taube.java b/AufgabenBlatt11/src/aufgabe2/Taube.java new file mode 100644 index 0000000..8308a27 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe2/Taube.java @@ -0,0 +1,26 @@ +package aufgabe2; + +/** + * Generic test class defining a pigeon able to fly through Fliegen + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Taube implements Fliegen { + + @Override + public void fliegen() { + System.out.println("GuuurrrGuurrr"); + } +} diff --git a/AufgabenBlatt11/src/aufgabe2b/Biene.java b/AufgabenBlatt11/src/aufgabe2b/Biene.java new file mode 100644 index 0000000..0be4b68 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe2b/Biene.java @@ -0,0 +1,26 @@ +package aufgabe2b; + +/** + * Generic test class defining a bee able to fly through Fliegen + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Biene extends Fliegen { + + @Override + public void fliegen() { + System.out.println("Summsumm"); + } +} diff --git a/AufgabenBlatt11/src/aufgabe2b/Fliegen.java b/AufgabenBlatt11/src/aufgabe2b/Fliegen.java new file mode 100644 index 0000000..5db3338 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe2b/Fliegen.java @@ -0,0 +1,26 @@ +package aufgabe2b; + +/** + * Generic test class defining the ability to fly + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public abstract class Fliegen { + + /** + * fly + */ + public abstract void fliegen(); +} diff --git a/AufgabenBlatt11/src/aufgabe2b/Main.java b/AufgabenBlatt11/src/aufgabe2b/Main.java new file mode 100644 index 0000000..6d9c8eb --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe2b/Main.java @@ -0,0 +1,37 @@ +package aufgabe2b; + +/** + * Generic test class for testing the Fliegen interface + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public final class Main { + + /** + * Calls fliegen() on the given Object + * @param sehrDickerBrummer the object to call fliegen() on + */ + private static void abflug(Fliegen sehrDickerBrummer) { + sehrDickerBrummer.fliegen(); + } + + public static void main(String[] args) { + var maja = new Biene(); + var gunter = new Taube(); + + abflug(maja); + abflug(gunter); + } +} diff --git a/AufgabenBlatt11/src/aufgabe2b/Taube.java b/AufgabenBlatt11/src/aufgabe2b/Taube.java new file mode 100644 index 0000000..602597f --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe2b/Taube.java @@ -0,0 +1,26 @@ +package aufgabe2b; + +/** + * Generic test class defining a pigeon able to fly through Fliegen + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Taube extends Fliegen { + + @Override + public void fliegen() { + System.out.println("GuuurrrGuurrr"); + } +} diff --git a/AufgabenBlatt11/src/aufgabe3/Adressierbar.java b/AufgabenBlatt11/src/aufgabe3/Adressierbar.java new file mode 100644 index 0000000..2618fab --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe3/Adressierbar.java @@ -0,0 +1,6 @@ +package aufgabe3; + +public interface Adressierbar { + public void setEmfaenger( String adresse); + public String getEmfaenger(); +} \ No newline at end of file diff --git a/AufgabenBlatt11/src/aufgabe3/PostPacket.java b/AufgabenBlatt11/src/aufgabe3/PostPacket.java new file mode 100644 index 0000000..6e4d16c --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe3/PostPacket.java @@ -0,0 +1,65 @@ +package aufgabe3; + +/** + * Generic test class defining metadata for sending a packet + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class PostPacket implements Adressierbar, Versendbar { + + /** + * From who the packet was send + */ + private String from; + /** + * Who should receive the packet + */ + private String to; + /** + * How much the package weighs + */ + private int weight; + + public PostPacket(String from, String to, int weight) { + this.from = from; + this.to = to; + this.weight = weight; + } + + @Override + public void setEmfaenger(String adresse) { + this.to = adresse; + } + + @Override + public String getEmfaenger() { + return this.to; + } + + @Override + public void setAbsender(String absender) { + this.from = absender; + } + + @Override + public String getAbsender() { + return this.from; + } + + @Override + public int getGewicht() { + return this.weight; + } +} \ No newline at end of file diff --git a/AufgabenBlatt11/src/aufgabe3/Postamt.java b/AufgabenBlatt11/src/aufgabe3/Postamt.java new file mode 100644 index 0000000..ec39c0d --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe3/Postamt.java @@ -0,0 +1,32 @@ +package aufgabe3; + +/** + * Generic test class defining a post office ready to send packages + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public final class Postamt { + + /** + * Does not try to send a packet send by send with destination to. + * But it does print some information to System.out. + * @param packet the packet not to send + */ + public void versende(Versendbar packet) { + System.out.println("Sendung wurde entgegengenommen und wird jetzt versandt."); + System.out.println("Absender: " + packet.getAbsender()); + System.out.println("Empfänger: " + packet.getEmfaenger()); + } +} diff --git a/AufgabenBlatt11/src/aufgabe3/Start.java b/AufgabenBlatt11/src/aufgabe3/Start.java new file mode 100644 index 0000000..1cd1575 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe3/Start.java @@ -0,0 +1,13 @@ +package aufgabe3; + +public class Start { + public static void main(String[] args) { + int gewicht = 10; + String an = "Horst Schlammer, 77077 Grevenbroich, Am Acker 8"; + String von = "Gisela Brandt, 77077 Grevenbroich, Am Acker 10"; + + PostPacket packet = new PostPacket(von, an, gewicht); + Postamt post = new Postamt(); + post.versende(packet); + } +} \ No newline at end of file diff --git a/AufgabenBlatt11/src/aufgabe3/Versendbar.java b/AufgabenBlatt11/src/aufgabe3/Versendbar.java new file mode 100644 index 0000000..d0e4763 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe3/Versendbar.java @@ -0,0 +1,7 @@ +package aufgabe3; + +public interface Versendbar extends Adressierbar { + public void setAbsender(String absender); + public String getAbsender(); + public int getGewicht(); +} \ No newline at end of file diff --git a/AufgabenBlatt11/src/aufgabe4/CaesarChiffre.java b/AufgabenBlatt11/src/aufgabe4/CaesarChiffre.java new file mode 100644 index 0000000..9d8a48e --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe4/CaesarChiffre.java @@ -0,0 +1,67 @@ +package aufgabe4; + +/** + * Generic test class defining the caesar chiffre + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class CaesarChiffre implements Chiffrierung { + /** + * How much to shift every character + */ + private final int shiftOffset; + + public CaesarChiffre(int shiftOffset) { + this.shiftOffset = shiftOffset; + } + + /** + * Shift a character by a specified amount away from its UTF-8 representation + * Performs a wraparound for lower case letters and return all other characters unchanged + * @param ch the character to shift + * @param off shift offset + * @return the shifted character + */ + private char shiftCharacter(char ch, int off) { + + if (Character.isLowerCase(ch)) { + return (char) wrapAddAround(ch, off, 'a', 'z'); + } + + return ch; + } + + /** + * Sums k onto x and wraps the result around the domain of [min, max] + * @param x lhs of sum + * @param k rhs of sum + * @param min minimum value to wrap around + * @param max maximum value to wrap around + * @return the result + */ + public static int wrapAddAround(int x, int k, int min, int max) { + return Math.floorMod(Math.abs(x - min) + k, max - min + 1) + min; + } + + @Override + public char chiffrieren(char zeichen) { + return shiftCharacter(zeichen, shiftOffset); + } + + @Override + public char dechiffrieren(char zeichen) { + return shiftCharacter(zeichen, -shiftOffset); + } +} diff --git a/AufgabenBlatt11/src/aufgabe4/Chiffrierung.java b/AufgabenBlatt11/src/aufgabe4/Chiffrierung.java new file mode 100644 index 0000000..b486451 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe4/Chiffrierung.java @@ -0,0 +1,6 @@ +package aufgabe4; + +public interface Chiffrierung { + char chiffrieren(char zeichen); + char dechiffrieren(char zeichen); +} \ No newline at end of file diff --git a/AufgabenBlatt11/src/aufgabe4/GeheimtextalphabetVerschiebung.java b/AufgabenBlatt11/src/aufgabe4/GeheimtextalphabetVerschiebung.java new file mode 100644 index 0000000..7dd9391 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe4/GeheimtextalphabetVerschiebung.java @@ -0,0 +1,50 @@ +package aufgabe4; + +/** + * Generic test class defining a simple encryption by association + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class GeheimtextalphabetVerschiebung implements Chiffrierung { + + private final char[] map; + + public GeheimtextalphabetVerschiebung(char[] map) { + if (map.length != 26) + throw new IllegalArgumentException("Character map must be 26 characters long"); + + this.map = map; + } + + @Override + public char chiffrieren(char zeichen) { + if (Character.isLowerCase(zeichen)) { + return this.map[zeichen - 'a']; + } + return zeichen; + } + + @Override + public char dechiffrieren(char zeichen) { + if (Character.isLowerCase(zeichen)) { + for (int i = 0; i < map.length; i++) { + if (map[i] == zeichen) { + return (char) ('a' + i); + } + } + } + return zeichen; + } +} diff --git a/AufgabenBlatt11/src/aufgabe4/TestCaesarChiffre.java b/AufgabenBlatt11/src/aufgabe4/TestCaesarChiffre.java new file mode 100644 index 0000000..6221c8b --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe4/TestCaesarChiffre.java @@ -0,0 +1,39 @@ +package aufgabe4; + +import org.junit.Test; + +import static aufgabe4.Verschluesselung.entschluesseln; +import static aufgabe4.Verschluesselung.verschluesseln; + +/** + * Generic test class testing the caesar chiffre + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class TestCaesarChiffre { + + @Test + public void test() { + var text = "abcdefghijklmnopqrstuvwxyz"; + + var chiffre = new CaesarChiffre(1); + + var encrypted = verschluesseln(text, chiffre); + System.out.println(encrypted); + + var decrypted = entschluesseln(encrypted, chiffre); + System.out.println(decrypted); + } +} diff --git a/AufgabenBlatt11/src/aufgabe4/TestGeheimtextVerschiebung.java b/AufgabenBlatt11/src/aufgabe4/TestGeheimtextVerschiebung.java new file mode 100644 index 0000000..a6b0045 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe4/TestGeheimtextVerschiebung.java @@ -0,0 +1,40 @@ +package aufgabe4; + +import org.junit.Test; + +import static aufgabe4.Verschluesselung.entschluesseln; +import static aufgabe4.Verschluesselung.verschluesseln; + +/** + * Generic test class testing + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class TestGeheimtextVerschiebung { + + @Test + public void test() { + var text = "IchBinEinText"; + var map = new char[] {'v', 'u', 'b', 'y', 'd', 'f', 'k', 'g', 'h', 'i', 'j', 'c', 'q', 'l', 'm', 'n', 'o', 'p', 'r', 's', 'e', 't', 'a', 'w', 'x', 'z'}; + + var chiffre = new GeheimtextalphabetVerschiebung(map); + + var encrypted = verschluesseln(text, chiffre); + System.out.println(encrypted); + + var decrypted = entschluesseln(encrypted, chiffre); + System.out.println(decrypted); + } +} diff --git a/AufgabenBlatt11/src/aufgabe4/Verschluesselung.java b/AufgabenBlatt11/src/aufgabe4/Verschluesselung.java new file mode 100644 index 0000000..603acd2 --- /dev/null +++ b/AufgabenBlatt11/src/aufgabe4/Verschluesselung.java @@ -0,0 +1,36 @@ +package aufgabe4; + +public final class Verschluesselung { + + /** + * Encrypt all characters in a string with a specified encryption + * @param text text to encrypt + * @param chiffrierung algorithm to use + * @return the encrypted text + */ + public static String verschluesseln(String text, Chiffrierung chiffrierung) { + var builder = new StringBuilder(); + + for (int i = 0; i < text.length(); i++) { + builder.append(chiffrierung.chiffrieren(text.charAt(i))); + } + + return builder.toString(); + } + + /** + * Decrypt all characters in a string with a specified encryption + * @param text text to decrypt + * @param chiffrierung algorithm to use + * @return the decrypted text + */ + public static String entschluesseln(String text, Chiffrierung chiffrierung) { + var builder = new StringBuilder(); + + for (int i = 0; i < text.length(); i++) { + builder.append(chiffrierung.dechiffrieren(text.charAt(i))); + } + + return builder.toString(); + } +} diff --git a/AufgabenBlatt9/src/Aufgabe3.java b/AufgabenBlatt9/src/Aufgabe3.java deleted file mode 100644 index 8a82074..0000000 --- a/AufgabenBlatt9/src/Aufgabe3.java +++ /dev/null @@ -1,112 +0,0 @@ -import org.junit.Test; - -import java.math.BigDecimal; -import java.math.RoundingMode; - -import static org.junit.Assert.assertEquals; - -/** - * Generic test class for implementing and testing a very basic rational number class - * _ _ _ _ - * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ - * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | - * \ V V /| | | | |_| || __/ | | | | |_) | |_| | - * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | - * |___/ - * ____ __ __ _ - * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | - * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | - * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | - * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| - * |___/ - * Licensed under the GPLv2 License, Version 2.0 (the "License"); - * Copyright (c) Sven Vogel - */ -public class Aufgabe3 { - - /** - * A 64-bit signed rational number. The nominator and denominator are of fixed size of 32-bit. - * Both can be signed. The sign of the number is given by sign(nominator) * sign(denominator) - * Zero can be represented as the nominator being zero and the denominator being anything but 0. - * Since division by 0 is undefined, all rational numbers with a denominator of zero are to be treated as - * invalid numbers. - * @param nominator - * @param denominator - */ - private record Rational(int nominator, int denominator) { - - /** - * Construct a rational number. Throws a {@link IllegalArgumentException} exception if the denominator is zero. - * @param nominator the nominator - * @param denominator the denominator (any non-zero value) - * @apiNote nominator and denominator are GCD reduced to avoid overflows - */ - private Rational(int nominator, int denominator) { - if (denominator == 0) - throw new IllegalArgumentException("denominator must be non-negative"); - - // GCD reduction - var gcd = gcd(denominator, nominator); - - this.nominator = nominator / gcd; - this.denominator = denominator / gcd; - } - - /** - * 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 - */ - private static int gcd(int x, int y) { - if (y == 0) - return x; - - return gcd(y, y % x); - } - - /** - * Multiply this number by rhs. Yields the result as a new number - * @param rhs the right hand side of the multiplication - * @return the result of this * rhs - * @apiNote nominator and denominator are GCD reduced to avoid overflows - */ - public Rational multiply(Rational rhs) { - return new Rational(this.nominator * rhs.nominator(), this.denominator * rhs.denominator()); - } - - /** - * Add this number to rhs. Yields the result as a new number - * @param rhs the right hand side of the sum - * @return the result of this + rhs - * @apiNote nominator and denominator are GCD reduced to avoid overflows - */ - public Rational add(Rational rhs) { - return new Rational(this.nominator * rhs.denominator() + this.denominator * rhs.nominator(), this.denominator * rhs.denominator()); - } - - /** - * Returns an approximation of the represented number by converting the fraction to BigDecimals and performing - * the conversion to single number by division. - * @param scale the number of digits to approximate - * @return the approximated value as BigDecimal - */ - public BigDecimal toBigDecimal(int scale) { - var nominator = BigDecimal.valueOf(this.nominator, scale); - var denominator = BigDecimal.valueOf(this.denominator, scale); - - return nominator.divide(denominator, RoundingMode.HALF_EVEN); - } - } - - @Test - public void test() { - var a = new Rational(1, 3); - var b = new Rational(7, 6); - - var c = a.add(b).multiply(a); - - assertEquals(c.toBigDecimal(10).compareTo(BigDecimal.valueOf(0.5)), 0); - } -} \ No newline at end of file diff --git a/AufgabenBlatt9/src/aufgabe3/Rational.java b/AufgabenBlatt9/src/aufgabe3/Rational.java new file mode 100644 index 0000000..355743d --- /dev/null +++ b/AufgabenBlatt9/src/aufgabe3/Rational.java @@ -0,0 +1,80 @@ +package aufgabe3; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +/** + * A 64-bit signed rational number. The nominator and denominator are of fixed size of 32-bit. + * Both can be signed. The sign of the number is given by sign(nominator) * sign(denominator) + * Zero can be represented as the nominator being zero and the denominator being anything but 0. + * Since division by 0 is undefined, all rational numbers with a denominator of zero are to be treated as + * invalid numbers. + * @param nominator + * @param denominator + */ +public record Rational(int nominator, int denominator) { + + /** + * Construct a rational number. Throws a {@link IllegalArgumentException} exception if the denominator is zero. + * @param nominator the nominator + * @param denominator the denominator (any non-zero value) + * @apiNote nominator and denominator are GCD reduced to avoid overflows + */ + public Rational(int nominator, int denominator) { + if (denominator == 0) + throw new IllegalArgumentException("denominator must be non-negative"); + + // GCD reduction + var gcd = gcd(denominator, nominator); + + this.nominator = nominator / gcd; + this.denominator = denominator / gcd; + } + + /** + * 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 + */ + private static int gcd(int x, int y) { + if (y == 0) + return x; + + return gcd(y, y % x); + } + + /** + * Multiply this number by rhs. Yields the result as a new number + * @param rhs the right hand side of the multiplication + * @return the result of this * rhs + * @apiNote nominator and denominator are GCD reduced to avoid overflows + */ + public Rational multiply(Rational rhs) { + return new Rational(this.nominator * rhs.nominator(), this.denominator * rhs.denominator()); + } + + /** + * Add this number to rhs. Yields the result as a new number + * @param rhs the right hand side of the sum + * @return the result of this + rhs + * @apiNote nominator and denominator are GCD reduced to avoid overflows + */ + public Rational add(Rational rhs) { + return new Rational(this.nominator * rhs.denominator() + this.denominator * rhs.nominator(), this.denominator * rhs.denominator()); + } + + /** + * Returns an approximation of the represented number by converting the fraction to BigDecimals and performing + * the conversion to single number by division. + * @param scale the number of digits to approximate + * @return the approximated value as BigDecimal + */ + public BigDecimal toBigDecimal(int scale) { + var nominator = BigDecimal.valueOf(this.nominator, scale); + var denominator = BigDecimal.valueOf(this.denominator, scale); + + return nominator.divide(denominator, RoundingMode.HALF_EVEN); + } +} \ No newline at end of file diff --git a/AufgabenBlatt9/src/aufgabe3/RationalTest.java b/AufgabenBlatt9/src/aufgabe3/RationalTest.java new file mode 100644 index 0000000..ee29ad8 --- /dev/null +++ b/AufgabenBlatt9/src/aufgabe3/RationalTest.java @@ -0,0 +1,37 @@ +package aufgabe3; + +import org.junit.Test; + +import java.math.BigDecimal; + +import static org.junit.Assert.assertEquals; + +/** + * Generic test class for implementing and testing a very basic rational number class + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class RationalTest { + + @Test + public void test() { + var a = new Rational(1, 3); + var b = new Rational(7, 6); + + var c = a.add(b).multiply(a); + + assertEquals(c.toBigDecimal(10).compareTo(BigDecimal.valueOf(0.5)), 0); + } +} \ No newline at end of file