added Aufgabenblatt 11

This commit is contained in:
Sven Vogel 2023-05-26 08:58:21 +02:00
parent 0902417234
commit d70c95bea9
25 changed files with 759 additions and 112 deletions

View File

@ -3,6 +3,7 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt10/AufgabenBlatt10.iml" filepath="$PROJECT_DIR$/AufgabenBlatt10/AufgabenBlatt10.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt10/AufgabenBlatt10.iml" filepath="$PROJECT_DIR$/AufgabenBlatt10/AufgabenBlatt10.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt11/AufgabenBlatt11.iml" filepath="$PROJECT_DIR$/AufgabenBlatt11/AufgabenBlatt11.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" filepath="$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" filepath="$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" filepath="$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" filepath="$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" filepath="$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" filepath="$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" />

29
AufgabenBlatt11/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="openjdk-19" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@ -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");
}
}

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,6 @@
package aufgabe3;
public interface Adressierbar {
public void setEmfaenger( String adresse);
public String getEmfaenger();
}

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,7 @@
package aufgabe3;
public interface Versendbar extends Adressierbar {
public void setAbsender(String absender);
public String getAbsender();
public int getGewicht();
}

View File

@ -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);
}
}

View File

@ -0,0 +1,6 @@
package aufgabe4;
public interface Chiffrierung {
char chiffrieren(char zeichen);
char dechiffrieren(char zeichen);
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}