From 603a8db49f3a5927732c392895e9fb02355825b4 Mon Sep 17 00:00:00 2001 From: teridax Date: Thu, 25 May 2023 12:57:17 +0200 Subject: [PATCH] moved DynamnicStringArray class into its own task package --- AufgabenBlatt8/src/Aufgabe2.java | 88 ------------------- .../src/aufgabe2/DynamicStringArray.java | 64 ++++++++++++++ AufgabenBlatt8/src/aufgabe2/UI.java | 40 +++++++++ .../{Applikation.java => Application.java} | 4 +- AufgabenBlatt8/src/aufgabe4/Patient.java | 4 +- 5 files changed, 109 insertions(+), 91 deletions(-) delete mode 100644 AufgabenBlatt8/src/Aufgabe2.java create mode 100644 AufgabenBlatt8/src/aufgabe2/DynamicStringArray.java create mode 100644 AufgabenBlatt8/src/aufgabe2/UI.java rename AufgabenBlatt8/src/aufgabe4/{Applikation.java => Application.java} (89%) diff --git a/AufgabenBlatt8/src/Aufgabe2.java b/AufgabenBlatt8/src/Aufgabe2.java deleted file mode 100644 index 94e5e3b..0000000 --- a/AufgabenBlatt8/src/Aufgabe2.java +++ /dev/null @@ -1,88 +0,0 @@ -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)); - } - } -} \ No newline at end of file diff --git a/AufgabenBlatt8/src/aufgabe2/DynamicStringArray.java b/AufgabenBlatt8/src/aufgabe2/DynamicStringArray.java new file mode 100644 index 0000000..70a62d2 --- /dev/null +++ b/AufgabenBlatt8/src/aufgabe2/DynamicStringArray.java @@ -0,0 +1,64 @@ +package aufgabe2; + +/** + * Generic test class for implementing dynamic string arrays + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public 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]; + } +} \ No newline at end of file diff --git a/AufgabenBlatt8/src/aufgabe2/UI.java b/AufgabenBlatt8/src/aufgabe2/UI.java new file mode 100644 index 0000000..e482687 --- /dev/null +++ b/AufgabenBlatt8/src/aufgabe2/UI.java @@ -0,0 +1,40 @@ +package aufgabe2; + +import javax.swing.*; + +/** + * Generic test class for testing dynamic string arrays + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class UI { + + 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)); + } + } +} \ No newline at end of file diff --git a/AufgabenBlatt8/src/aufgabe4/Applikation.java b/AufgabenBlatt8/src/aufgabe4/Application.java similarity index 89% rename from AufgabenBlatt8/src/aufgabe4/Applikation.java rename to AufgabenBlatt8/src/aufgabe4/Application.java index d25174c..eaba0ca 100644 --- a/AufgabenBlatt8/src/aufgabe4/Applikation.java +++ b/AufgabenBlatt8/src/aufgabe4/Application.java @@ -4,11 +4,13 @@ import java.util.Calendar; import java.util.GregorianCalendar; @SuppressWarnings("unused") -public class Applikation { +public class Application { 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()); + + System.out.println(jano); } } diff --git a/AufgabenBlatt8/src/aufgabe4/Patient.java b/AufgabenBlatt8/src/aufgabe4/Patient.java index f061ec4..155e6f5 100644 --- a/AufgabenBlatt8/src/aufgabe4/Patient.java +++ b/AufgabenBlatt8/src/aufgabe4/Patient.java @@ -21,7 +21,7 @@ import java.util.random.RandomGenerator; * Copyright (c) Sven Vogel */ @SuppressWarnings("unused") -public class Patient { +public class Patient extends Object { private final String name; private final String familyName; @@ -104,6 +104,6 @@ public class Patient { @Override public String toString() { - return String.format("[Patient: %s %s, admission: %s, uuid: %s, released: %s]", name, familyName, admission.toString(), uuid, release.toString()); + return String.format("[Patient: %s %s, admission: %s, uuid: %s, released: %s]", name, familyName, admission.toString(), uuid, release == null ? "" : release.toString()); } }