moved DynamnicStringArray class into its own task package

This commit is contained in:
Sven Vogel 2023-05-25 12:57:17 +02:00
parent 159ea29192
commit 603a8db49f
5 changed files with 109 additions and 91 deletions

View File

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

View File

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

View File

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

View File

@ -4,11 +4,13 @@ import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class Applikation { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
var hermes = new Patient("Angela", "Merkel", new GregorianCalendar(1756, Calendar.DECEMBER, 11).getTime()); 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 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()); var jano = new Patient("Jano", "Lorang", new GregorianCalendar(1994, Calendar.SEPTEMBER, 14).getTime());
System.out.println(jano);
} }
} }

View File

@ -21,7 +21,7 @@ import java.util.random.RandomGenerator;
* Copyright (c) Sven Vogel * Copyright (c) Sven Vogel
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class Patient { public class Patient extends Object {
private final String name; private final String name;
private final String familyName; private final String familyName;
@ -104,6 +104,6 @@ public class Patient {
@Override @Override
public String toString() { 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());
} }
} }