Files
Testate/WS2526/Testat 3b/Main.java
T

127 lines
5.5 KiB
Java

import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
public class Main {
public static void main(String[] args) {
System.out.println();
System.out.println("=========================================");
System.out.println("Aufgabe 1 - Baum");
System.out.println("=========================================");
System.out.println();
System.out.println("Creating tree with 4 nodes: 2, 1, 3, 1");
Tree tree = new Tree();
tree.insert(2);
tree.insert(1);
tree.insert(3);
tree.insert(1);
TreeVisitor v = new TreeVisitor();
// post-order traversal
tree.traverse(v);
System.out.println("Visiting all nodes in post-order.");
System.out.println("Expected output: [3.0, 1.0, 1.0, 2.0]");
System.out.println("Actual output: " + v);
// count
System.out.println("Node count of 2 should be 1 and was " + tree.count(2));
System.out.println("Node count of 1 should be 2 and was " + tree.count(1));
// height
System.out.println("Height of the tree should be 3 and was " + tree.height());
System.out.println();
System.out.println("=========================================");
System.out.println("Aufgabe 2 - I/O-Serialisierung von Listen");
System.out.println("=========================================");
System.out.println();
// Create a StringListSerializer to test our implementation
StringListSerializer serializer = new StringListSerializer();
Path erlkingPath = InMemoryPathFactory.createPath("/test/erlking.txt");
List<String> erlkingData = Arrays.asList(
"Who rides so late through night and wind?",
"It is the father with his child.",
"He holds the boy tightly in his arms,",
"He holds him safely, he keeps him warm.",
"My son, why do you hide your face so anxiously?",
"Father, do you not see the Erlking?",
"The Erlking with crown and tail?",
"My son, it is a streak of fog.",
"Come, go with me! Many pretty games I play with you;",
"Many colorful flowers are at the shore;",
"My mother has many golden garments.",
"Father, father, do you not hear what the Erlking promises me?",
"Be calm, stay calm, my child;",
"The wind rustles through the dry leaves.",
"Do you not want to come with me?",
"Father, father, do you not see the Erlking's daughter?",
"The Erlking's daughter with her golden garments?",
"My son, my son, I see it clearly:",
"The old willows look so gray and twisted.",
"I love you, I want to play with you,",
"And if you are not willing, I will use force.",
"Father, father, now he has seized me!",
"The Erlking has hurt me!",
"The father shudders, he rides swiftly,",
"He holds in his arms the moaning child,",
"He reaches the farmhouse with great effort;",
"In his arms, the child was dead.",
"- Johann Wolfgang von Goethe, 1782");
// WARNING: Reading depends on writing!
System.out.println("Writing 'Erlking' data to memory path: " + erlkingPath);
serializer.write(erlkingPath, erlkingData);
System.out.println("Wrote 'Erlking' data to memory path.");
System.out.println("");
System.out.println("Reading 'Erlking' data from memory path:");
List<String> erlkingReadData = serializer.read(erlkingPath);
for (String line : erlkingReadData) {
System.out.println(" " + line);
}
System.out.println("");
if (erlkingData.equals(erlkingReadData)) {
System.out.println("Read content matches original content.");
} else {
System.out.println("Read content does NOT match original content.");
}
System.out.println();
System.out.println("=========================================");
System.out.println("Aufgabe 3 - Exceptions");
System.out.println("=========================================");
System.out.println();
Deque deque = new Deque();
// getFirst
System.out.println("Checking if getFirst on empty deque throws NoSuchElementException");
try {
deque.getFirst();
System.out.println("FAILURE: No exception thrown.");
} catch (NoSuchElementException e) {
System.out.println("SUCCESS: NoSuchElementException thrown.");
} catch (Exception e) {
System.out.println("FAILURE: Unexpected exception thrown. Exception thrown: " + e.getClass().getName());
System.out.println(e.getMessage());
}
System.out.println();
// addLast
System.out.println("Checking if addLast(null) throws IllegalArgumentException");
try {
deque.addLast(null);
System.out.println("FAILURE: No exception thrown.");
} catch (IllegalArgumentException e) {
System.out.println("SUCCESS: IllegalArgumentException thrown.");
} catch (Exception e) {
System.out.println("FAILURE: Unexpected exception thrown. Exception thrown: " + e.getClass().getName());
System.out.println(e.getMessage());
}
}
}