added pages 7-9
This commit is contained in:
parent
39db3138b3
commit
159ea29192
|
@ -5,6 +5,9 @@
|
|||
<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$/AufgabenBlatt6/AufgabenBlatt6.iml" filepath="$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt7/AufgabenBlatt7.iml" filepath="$PROJECT_DIR$/AufgabenBlatt7/AufgabenBlatt7.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt8/AufgabenBlatt8.iml" filepath="$PROJECT_DIR$/AufgabenBlatt8/AufgabenBlatt8.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt9/AufgabenBlatt9.iml" filepath="$PROJECT_DIR$/AufgabenBlatt9/AufgabenBlatt9.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/GregorianischerKalender/GregorianischerKalender.iml" filepath="$PROJECT_DIR$/GregorianischerKalender/GregorianischerKalender.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/Java-Programming.iml" filepath="$PROJECT_DIR$/.idea/Java-Programming.iml" />
|
||||
</modules>
|
||||
|
|
|
@ -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
|
|
@ -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>
|
|
@ -0,0 +1,61 @@
|
|||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Generic test class for implementing and testing finding the largest element^ of an integer array
|
||||
* _ _ _ _
|
||||
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||||
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
||||
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
||||
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
||||
* |___/
|
||||
* ____ __ __ _
|
||||
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
||||
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
||||
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
||||
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
||||
* |___/
|
||||
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
||||
* Copyright (c) Sven Vogel
|
||||
*/
|
||||
public class Aufgabe1 {
|
||||
|
||||
/**
|
||||
* Returns the largest integer value from the supplied array.
|
||||
* @param array the array to search through
|
||||
* @return the largest integer value from the supplied array
|
||||
* @apiNote The largest element is searched in parallel through Stream API.
|
||||
*/
|
||||
public static int findLargestThingy(int[] array) {
|
||||
return Arrays.stream(array).parallel().max().orElseThrow();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
final var ARRAY = new int[] {
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
|
||||
};
|
||||
|
||||
assertEquals(findLargestThingy(ARRAY), 12);
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
// Task b)
|
||||
////////////////////////////////
|
||||
|
||||
private int[] test;
|
||||
private int erg;
|
||||
|
||||
@Test
|
||||
public void findeGroesstes() {
|
||||
test = new int[] { 5, 4, 6, 1 };
|
||||
erg = 6;
|
||||
assertEquals(erg, Aufgabe1.findLargestThingy(test));
|
||||
test = new int[] { 2133231234, 6213123, 1012312, 4353453, 123123127, 4563564 };
|
||||
erg = test[0];
|
||||
assertEquals(erg, Aufgabe1.findLargestThingy(test));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Generic test class for implementing and testing swapping elements of an array
|
||||
* _ _ _ _
|
||||
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||||
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
||||
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
||||
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
||||
* |___/
|
||||
* ____ __ __ _
|
||||
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
||||
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
||||
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
||||
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
||||
* |___/
|
||||
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
||||
* Copyright (c) Sven Vogel
|
||||
*/
|
||||
public class Aufgabe2 {
|
||||
|
||||
/**
|
||||
* Swap the values of the given indices of the given array
|
||||
* @param a the array to swap elements of
|
||||
* @param idx first index
|
||||
* @param idy second index
|
||||
*/
|
||||
private static void swapIndices(int[] a, int idx, int idy) {
|
||||
var tmp = a[idx];
|
||||
a[idx] = a[idy];
|
||||
a[idy] = tmp;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
var array = new int[] {1, 2, 3, 4, 5};
|
||||
|
||||
swapIndices(array, 0, 1);
|
||||
swapIndices(array, 1, 2);
|
||||
swapIndices(array, 2, 0);
|
||||
swapIndices(array, 4, 2);
|
||||
|
||||
assertTrue(Arrays.equals(array, new int[] {1, 3, 5, 4, 2}));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Generic test class for implementing and testing a crude round function
|
||||
* _ _ _ _
|
||||
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||||
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
||||
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
||||
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
||||
* |___/
|
||||
* ____ __ __ _
|
||||
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
||||
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
||||
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
||||
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
||||
* |___/
|
||||
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
||||
* Copyright (c) Sven Vogel
|
||||
*/
|
||||
public class Aufgabe3 {
|
||||
|
||||
/**
|
||||
* Divides the numbers and returns the rounded result
|
||||
* @param numerator the numerator
|
||||
* @param denominator the denominator
|
||||
* @return the rounded result of numerator / denominator
|
||||
*/
|
||||
private static double rounded(double numerator, double denominator) {
|
||||
return Math.floor(numerator / denominator + 0.5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoundUp() {
|
||||
assertEquals(3.0, Aufgabe3.rounded(14.0, 5.0), 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoundDown() {
|
||||
assertEquals(3.0, Aufgabe3.rounded(16.0, 5.0), 0.0);
|
||||
}
|
||||
@Test
|
||||
public void testExact() {
|
||||
assertEquals(3.0, Aufgabe3.rounded(18.0, 6.0), 0.0);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
import org.junit.Test;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Generic test class for implementing and testing the fizzbuzz game
|
||||
* _ _ _ _
|
||||
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||||
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
||||
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
||||
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
||||
* |___/
|
||||
* ____ __ __ _
|
||||
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
||||
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
||||
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
||||
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
||||
* |___/
|
||||
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
||||
* Copyright (c) Sven Vogel
|
||||
*/
|
||||
public class Aufgabe4 {
|
||||
|
||||
/**
|
||||
* Returns "fizz" if x is divisible by 3
|
||||
* and "buzz" if x is divisible by 5
|
||||
* and "fizzbuzz" if x is divisible by 3 and 5
|
||||
* else return x as string
|
||||
* @param x the number to play fizzbuzz with
|
||||
* @return the fizzbuzz string
|
||||
*/
|
||||
private static String fizzbuzz(int x) {
|
||||
var divisibleBy3 = (x % 3) == 0;
|
||||
var divisibleBy5 = (x % 5) == 0;
|
||||
|
||||
if (divisibleBy3 && divisibleBy5) {
|
||||
return "FizzBuzz";
|
||||
} else if (divisibleBy3) {
|
||||
return "Fizz";
|
||||
} else if (divisibleBy5) {
|
||||
return "Buzz";
|
||||
}
|
||||
|
||||
return String.valueOf(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Play fizz buzz.
|
||||
* Reads numbers from System.in and prints the according fizzbuzz string.
|
||||
* @apiNote this function blocks the current thread until 0 is read from System.in
|
||||
*/
|
||||
private static void playFizzBuzz() {
|
||||
try(var scanner = new Scanner(System.in)) {
|
||||
var number = scanner.nextInt();
|
||||
|
||||
if (number == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println(fizzbuzz(number));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFizzBuzz() {
|
||||
playFizzBuzz();
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,11 @@
|
|||
<?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" />
|
||||
</component>
|
||||
</module>
|
|
@ -0,0 +1,88 @@
|
|||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* Generic test class for implementing and testing dynamic string arrays
|
||||
* _ _ _ _
|
||||
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||||
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
||||
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
||||
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
||||
* |___/
|
||||
* ____ __ __ _
|
||||
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
||||
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
||||
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
||||
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
||||
* |___/
|
||||
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
||||
* Copyright (c) Sven Vogel
|
||||
*/
|
||||
public class Aufgabe2 {
|
||||
|
||||
/**
|
||||
* Manages an array of strings.
|
||||
* Reallocate the array if the size is reached.
|
||||
*/
|
||||
private static class DynamicStringArray {
|
||||
// actual number of elements stored in the array
|
||||
private int size = 0;
|
||||
private String[] array = new String[2];
|
||||
|
||||
/**
|
||||
* Adds a new string to the array.
|
||||
* Reallocate the array if the size is reached.
|
||||
* @param next the element to append
|
||||
*/
|
||||
public void add(String next) {
|
||||
// test if we need to reallocate
|
||||
if (array.length == size) {
|
||||
// temporarily store input strings
|
||||
String[] inputStringsTemp = array;
|
||||
|
||||
// double input strings size
|
||||
var capacity = array.length * 2;
|
||||
|
||||
array = new String[capacity];
|
||||
|
||||
// copy original input strings into double sized strings
|
||||
System.arraycopy(inputStringsTemp, 0, array, 0, size);
|
||||
}
|
||||
// store new element into increased array
|
||||
array[size++] = next;
|
||||
}
|
||||
|
||||
/**
|
||||
* the number of elements stored
|
||||
* @return amount of stored strings
|
||||
*/
|
||||
public int length() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch an element at index i
|
||||
* @param i the index to fetch
|
||||
* @return the element at index i
|
||||
*/
|
||||
public String get(int i) {
|
||||
return array[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
var array = new DynamicStringArray();
|
||||
|
||||
while (true) {
|
||||
String next = JOptionPane.showInputDialog("Naechste Eingabe");
|
||||
|
||||
if (next == null)
|
||||
break;
|
||||
|
||||
array.add(next);
|
||||
}
|
||||
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
JOptionPane.showMessageDialog(null, array.get(i));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package aufgabe3;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import static aufgabe3.UserInterface.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
var dict = new TelephoneDirectory(10);
|
||||
|
||||
while (true) {
|
||||
String action = getActionDialog("Eintrag anlegen", "Eintrag abfragen");
|
||||
if (action == null) {
|
||||
break;
|
||||
}
|
||||
if (action.equals("Eintrag anlegen")) {
|
||||
String[] entryData = getEntryDataDialog();
|
||||
if (entryData != null) {
|
||||
dict.addEntry(entryData[0], entryData[1], entryData[2]);
|
||||
}
|
||||
} else if (action.equals("Eintrag abfragen")) {
|
||||
String[] queryData = getQueryDataDialog();
|
||||
if (queryData != null) {
|
||||
String nr = dict.findEntry(queryData[0], queryData[1]);
|
||||
if (nr == null) {
|
||||
JOptionPane.showMessageDialog(null, "Nicht gefunden");
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null,
|
||||
"Name: " + queryData[0] + ", Vornane: " + queryData[1] + ", Nummer:" + nr);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.err.println("Fehler: unbekannte Aktion " + action);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package aufgabe3;
|
||||
|
||||
public class TelephoneDirectory {
|
||||
|
||||
private int maxSize = 10; // maximale Zahl der Eintraege
|
||||
private int actSize = 0; // aktuelle Zahl der Eintraege
|
||||
private String[] name = new String[maxSize]; // alle Namen
|
||||
private String[] firstName = new String[maxSize]; // alle Vornamen
|
||||
private String[] number = new String[maxSize]; // alle Nummern
|
||||
|
||||
public TelephoneDirectory(int maxSize) {
|
||||
this.maxSize = maxSize;
|
||||
this.actSize = 0;
|
||||
|
||||
this.name = new String[maxSize];
|
||||
this.firstName = new String[maxSize];
|
||||
this.number = new String[maxSize];
|
||||
}
|
||||
|
||||
public String findEntry(String nameP, String firstNameP) {
|
||||
for (int i = 0; i < actSize; i++) {
|
||||
if (name[i].equals(nameP) && firstName[i].equals(firstNameP)) {
|
||||
return number[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addEntry(String nameP, String firstNameP, String numberP) {
|
||||
if (actSize == maxSize) {
|
||||
System.err.println("Eintrag ignoriert: Speicher voll");
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < actSize; i++) {
|
||||
if (name[i].equals(nameP) && firstName[i].equals(firstNameP)) {
|
||||
System.err.println("Eintrag ignoriert: Existiert schon");
|
||||
return;
|
||||
}
|
||||
}
|
||||
name[actSize] = nameP;
|
||||
firstName[actSize] = firstNameP;
|
||||
number[actSize] = numberP;
|
||||
actSize = actSize + 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package aufgabe3;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class UserInterface {
|
||||
|
||||
static String getActionDialog(String... actions) {
|
||||
return (String) JOptionPane.showInputDialog(null, "Was soll getan werden?",
|
||||
"Aktions-Wahl",
|
||||
JOptionPane.QUESTION_MESSAGE, null, actions, actions[0]);
|
||||
}
|
||||
|
||||
static String[] getEntryDataDialog() {
|
||||
JTextField nameTF = new JTextField();
|
||||
JTextField firstNameTF = new JTextField();
|
||||
JTextField numberTF = new JTextField();
|
||||
Object[] message = {"Name", nameTF, "Vorname", firstNameTF, "Nummer", numberTF};
|
||||
Object[] options = {"OK", "Abbruch"};
|
||||
int n = JOptionPane.showOptionDialog(null, message, "Eintrag anlegen",
|
||||
JOptionPane.YES_NO_OPTION,
|
||||
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
|
||||
if (n == JOptionPane.OK_OPTION) { // Zustimmung
|
||||
return new String[]{nameTF.getText(), firstNameTF.getText(),
|
||||
numberTF.getText()};
|
||||
} else if (n == JOptionPane.NO_OPTION // Verneinung
|
||||
|| n == JOptionPane.CLOSED_OPTION) { // Dialogfenster geschlossen
|
||||
return null;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static String[] getQueryDataDialog() {
|
||||
JTextField nameTF = new JTextField();
|
||||
JTextField firstNameTF = new JTextField();
|
||||
Object[] message = {"Name", nameTF, "Vorname", firstNameTF};
|
||||
Object[] options = {"OK", "Abbruch"};
|
||||
int n = JOptionPane.showOptionDialog(null, message, "Eintrag abfragen",
|
||||
JOptionPane.YES_NO_OPTION,
|
||||
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
|
||||
if (n == JOptionPane.OK_OPTION) { // Zustimmung
|
||||
return new String[]{nameTF.getText(), firstNameTF.getText()};
|
||||
} else if (n == JOptionPane.NO_OPTION // Verneinung
|
||||
|| n == JOptionPane.CLOSED_OPTION) { // Dialogfenster geschlossen
|
||||
return null;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package aufgabe4;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class Applikation {
|
||||
|
||||
public static void main(String[] args) {
|
||||
var hermes = new Patient("Angela", "Merkel", new GregorianCalendar(1756, Calendar.DECEMBER, 11).getTime());
|
||||
var peter = new Patient("Christoph", "Bullinger", new GregorianCalendar(2003, Calendar.JANUARY, 31).getTime());
|
||||
var jano = new Patient("Jano", "Lorang", new GregorianCalendar(1994, Calendar.SEPTEMBER, 14).getTime());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
package aufgabe4;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.random.RandomGenerator;
|
||||
|
||||
/**
|
||||
* Generic test class for implementing a simple patient system for hospitals
|
||||
* _ _ _ _
|
||||
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||||
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
||||
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
||||
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
||||
* |___/
|
||||
* ____ __ __ _
|
||||
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
||||
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
||||
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
||||
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
||||
* |___/
|
||||
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
||||
* Copyright (c) Sven Vogel
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class Patient {
|
||||
|
||||
private final String name;
|
||||
private final String familyName;
|
||||
private final Date birthday;
|
||||
// date the patient was delivered to the hospital
|
||||
private Date admission;
|
||||
// date the patient was finished
|
||||
private Date release;
|
||||
// universally unique identifier
|
||||
private long uuid;
|
||||
|
||||
/**
|
||||
* Constructs a new Patient
|
||||
* @param name the name of the patient
|
||||
* @param familyName the family name of the patient
|
||||
* @param birthday the date the patient was born
|
||||
*/
|
||||
public Patient(String name, String familyName, Date birthday) {
|
||||
this.name = name;
|
||||
this.familyName = familyName;
|
||||
this.birthday = birthday;
|
||||
|
||||
this.admission = new Date();
|
||||
this.uuid = RandomGenerator.getDefault().nextLong();
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the patient as released. Sets the released date to now
|
||||
*/
|
||||
public void release() {
|
||||
this.release = new Date();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the release date to null and set the admission day to now
|
||||
* the uuid will be regenerated
|
||||
*/
|
||||
public void admission() {
|
||||
this.release = null;
|
||||
this.admission = new Date();
|
||||
this.uuid = RandomGenerator.getDefault().nextLong();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getFamilyName() {
|
||||
return familyName;
|
||||
}
|
||||
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public Date getAdmission() {
|
||||
return admission;
|
||||
}
|
||||
|
||||
public Date getRelease() {
|
||||
return release;
|
||||
}
|
||||
|
||||
public long getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if the other object is a patient with equal name and family name else false
|
||||
* @param obj the other object ot compare to
|
||||
* @return true if the other object is a patient with equal name and family name else false
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Patient other) {
|
||||
return this.name.equals(other.getName()) && this.familyName.equals(other.getFamilyName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("[Patient: %s %s, admission: %s, uuid: %s, released: %s]", name, familyName, admission.toString(), uuid, release.toString());
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -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>
|
|
@ -0,0 +1,112 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package aufgabe4;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Generic test class for implementing and testing receipt class
|
||||
* _ _ _ _
|
||||
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||||
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
||||
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
||||
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
||||
* |___/
|
||||
* ____ __ __ _
|
||||
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
||||
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
||||
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
||||
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
||||
* |___/
|
||||
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
||||
* Copyright (c) Sven Vogel
|
||||
*/
|
||||
public class Receipt {
|
||||
|
||||
private final List<ReceiptItem> items = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Add a new receipt item to the list
|
||||
* @param item the item to add
|
||||
*/
|
||||
public void add(ReceiptItem item) {
|
||||
this.items.add(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print all receipt items in a linear manner.
|
||||
*/
|
||||
public void print() {
|
||||
System.out.println("""
|
||||
╔════════════════════════╗
|
||||
║ SuperShop 042 ║
|
||||
║ Marchstr. 23 ║
|
||||
║ 10227 Mannheim ║
|
||||
║ ☏ 030 314 213 86 ║
|
||||
╚════════════════════════╝""");
|
||||
|
||||
items.forEach(System.out::println);
|
||||
|
||||
var total = items.stream().map(a -> a.getPrice() * a.getAmount()).reduce(Double::sum).orElse(0.0);
|
||||
System.out.format("\n\t\t\t=====\nSumme EUR\t%.2f\n\t\t\t=====", total);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
var r = new Receipt();
|
||||
r.add( new ReceiptItem("15 Fischstäbchen", 3, 1.79) );
|
||||
r.add( new ReceiptItem("Steaks", 5, 3.99) );
|
||||
r.add( new ReceiptItem("Naturelle (1l)", 6, 4.99) );
|
||||
r.add( new ReceiptItem("Magnum Eis Creme", 2, 2.99) );
|
||||
|
||||
// output receipt in a linear fashion
|
||||
r.print();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package aufgabe4;
|
||||
|
||||
/**
|
||||
* Generic test class for implementing and testing receipt item class
|
||||
* _ _ _ _
|
||||
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||||
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
||||
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
||||
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
||||
* |___/
|
||||
* ____ __ __ _
|
||||
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
||||
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
||||
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
||||
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
||||
* |___/
|
||||
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
||||
* Copyright (c) Sven Vogel
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class ReceiptItem {
|
||||
|
||||
private final String name;
|
||||
private int amount;
|
||||
private double price;
|
||||
|
||||
public ReceiptItem(String name, int amount, double price) {
|
||||
this.name = name;
|
||||
this.amount = amount;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the amount
|
||||
* @param amount a non negative non zero number
|
||||
*/
|
||||
public void setAmount(int amount) {
|
||||
if (amount <= 0) {
|
||||
throw new IllegalArgumentException("amount must be greater than zero");
|
||||
}
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the price
|
||||
* @param price a non negative non zero number
|
||||
*/
|
||||
public void setPrice(double price) {
|
||||
if (price <= 0) {
|
||||
throw new IllegalArgumentException("price must be greater than zero");
|
||||
}
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s\n %sx\t%s€", name, amount, price);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue