Aufgabenblatt 3

This commit is contained in:
Anton Ertl
2022-03-28 13:45:26 +00:00
5 changed files with 314 additions and 0 deletions

50
angabe/Aufgabenblatt3.md Normal file
View File

@ -0,0 +1,50 @@
# Aufgabenblatt 3
## Allgemeine Anmerkungen
Ihre Lösung für dieses Aufgabenblatt ist bis Montag, 4.4. 11h durch `git commit` und `push` abzugeben. Mit der Angabe werden folgende Dateien mitgeliefert: [Simulation3](../src/Simulation3.java), [BodyLinkedList](../src/BodyLinkedList.java), [BodyForceTreeMap](../src/BodyForceTreeMap.java) und [Aufgabe3Test](../src/Aufgabe3Test.java).
Zusätzliche Klassen, Interfaces, Methoden und Variablen dürfen aber eingefügt werden. Wenn Sie zusätzlich zu den gefragten Klassen weitere Klassen definieren, achten Sie darauf, dass die Klassennamen mit `My` beginnen, um Konflikte mit späteren Aufgabenblättern zu vermeiden.
## Ziel
Ziel der Aufgabe ist die Implementierung einer Liste für eine lineare und eines Baums für eine assoziative Datenstruktur (siehe Skriptum Seiten 60-69).
## Beschreibung der gegebenen Dateien
- [BodyLinkedList](../src/BodyLinkedList.java) ist das Gerüst für eine Implementierung einer linearen Datenstruktur zur
Verwaltung von Objekten des Typs `Body`.
- [BodyForceTreeMap](../src/BodyForceTreeMap.java) ist das Gerüst für eine Implementierung einer assoziativen Datenstruktur, die einen Himmelskörper mit der auf ihn wirkenden Kraft assoziiert.
- [Aufgabe3Test](../src/Aufgabe3Test.java) ist eine vorgegebene Klasse, die Sie zum Testen Ihrer Implementierung verwenden sollten.
Bei einer fehlerfreien Implementierung sollten bei der Ausführung dieser Klasse keine Exceptions geworfen werden und alle Tests als erfolgreich ("successful") ausgegeben werden. Sie müssen diese Klasse nicht verändern, können aber eigene Testfälle hinzufügen.
- [Simulation3](../src/Simulation3.java) ist ein Gerüst für eine ausführbare Klasse. Hier soll die Simulation analog
zur Klasse `Simulation` implementiert werden (damit Sie Ihre [ursprüngliche Datei](../src/Simulation.java)
nicht überschreiben müssen).
## Aufgaben
Ihre Aufgaben sind folgende:
1. Vervollständigen Sie die Klassendefinitionen in [BodyLinkedList](../src/BodyLinkedList.java) gemäß der Kommentare in den Dateien. Die Implementierung soll mit Hilfe einer verketteten Liste erfolgen. Sie können selbst entscheiden, ob Sie eine einfach oder doppelt verkettete Liste implementieren wollen. Benutzen Sie keine Arrays oder vorgefertigten Klassen aus dem Java-Collection-Framework!
2. Vervollständigen Sie die Klassendefinition in [BodyForceTreeMap](../src/BodyForceTreeMap.java). Die Implementierung
soll mit Hilfe eines binären Suchbaums erfolgen, in dem die Himmelskörper nach deren Masse sortiert sind. Die eigentlichen Schlüssel sind somit Objekte vom Typ `Body`, die interne Ordnung im Suchbaum erfolgt jedoch durch deren Masse. Benutzen Sie keine Arrays oder vorgefertigten Klassen aus dem Java-Collection-Framework!
3. Vervollständigen Sie die gegebene Klasse [Simulation3](../src/Simulation3.java) unter der Verwendung der Klassen
[BodyLinkedList](../src/BodyLinkedList.java) und [BodyForceTreeMap](../src/BodyForceTreeMap.java), so dass sich diese wie die bereits bestehende Klasse
[Simulation](../src/Simulation.java) verhält. Kollisionen sollen wieder berücksichtigt werden. Die Zugriffe auf die
Himmelskörper der Simulation sollen über Methoden von [BodyLinkedList](../src/BodyLinkedList.java) erfolgen. Die Klasse [BodyForceTreeMap](../src/BodyForceTreeMap.java) soll zur Verwaltung der Kräfte benutzt werden.
Allgemeiner Hinweis: bei einigen Methoden sind Vorbedingungen (_pre-conditions_) angegeben. Diese Vorbedingungen müssen innerhalb der Methode NICHT überprüft werden, sondern stellen Zusicherungen dar, auf die die Methode sich verlassen kann. Diese Regel gilt allgemein auch für zukünftige Aufgabenblätter.
### Denkanstöße (ohne Bewertung)
1. Haben Sie bei der Implementierung darauf geachtet, dass die Zugriffe möglichst effizient
erfolgen können (Z.B. ohne die Liste beim Zugriff wiederholt durchlaufen zu müssen)? Was ist in dem Zusammenhang der Vorteil der verketteten Liste?
2. Wofür eignen sich eher die Queue-Methoden `addFirst`, `addLast`, `pollFirst` bzw.
`pollLast` und wofür eher die List-Methoden `get`?
#### _Punkteaufteilung_
- Implementierung von `BodyLinkedList`: 2 Punkte
- Implementierung von `BodyForceTreeMap`: 2 Punkte
- Implementierung von `Simulation3`: 1 Punkt
- Gesamt: 5 Punkte

110
src/Aufgabe3Test.java Normal file
View File

@ -0,0 +1,110 @@
public class Aufgabe3Test {
public static void main(String[] args) {
//test classes BodyLinkedList and BodyForceTreeMap
// create five bodies
Body sun = new Body(1.989e30, new Vector3(0, 0, 0), new Vector3(0, 0, 0));
Body earth = new Body(5.972e24, new Vector3(-1.394555e11, 5.103346e10, 0), new Vector3(-10308.53, -28169.38, 0));
Body mercury = new Body(3.301e23, new Vector3(-5.439054e10, 9.394878e9, 0), new Vector3(-17117.83, -46297.48, -1925.57));
Body venus = new Body(4.86747e24, new Vector3(-1.707667e10, 1.066132e11, 2.450232e9), new Vector3(-34446.02, -5567.47, 2181.10));
Body mars = new Body(6.41712e23, new Vector3(-1.010178e11, -2.043939e11, -1.591727E9), new Vector3(20651.98, -10186.67, -2302.79));
// check basic functions of 'BodyLinkedList'
System.out.println("Test1:");
BodyLinkedList bl = new BodyLinkedList();
bl.addLast(mercury);
bl.addLast(sun);
bl.addLast(earth);
testValue(bl.size(), 3);
testValue(bl.getFirst(), mercury);
testValue(bl.getLast(), earth);
testValue(bl.get(0), mercury);
testValue(bl.get(1), sun);
testValue(bl.get(2), earth);
System.out.println("Test2:");
testValue(bl.indexOf(earth), 2);
testValue(bl.indexOf(sun), 1);
testValue(bl.indexOf(mercury), 0);
System.out.println("Test3:");
testValue(bl.pollFirst(), mercury);
testValue(bl.pollLast(), earth);
testValue(bl.pollFirst(), sun);
testValue(bl.size(), 0);
testValue(bl.getFirst(), null);
System.out.println("Test4:");
bl.addFirst(earth);
bl.addFirst(venus);
bl.addFirst(sun);
bl.add(1, mercury);
bl.add(4, mars);
testValue(bl.size(), 5);
testValue(bl.get(0), sun);
testValue(bl.get(1), mercury);
testValue(bl.get(2), venus);
testValue(bl.get(3), earth);
testValue(bl.get(4), mars);
// check constructor of 'BodyLinkedList'
BodyLinkedList blCopy = new BodyLinkedList(bl);
testComparison(bl, blCopy, false);
testComparison(bl.pollFirst(), blCopy.pollFirst(), true);
bl.addFirst(sun);
testValue(bl.size(), 5);
testValue(blCopy.size(), 4);
// check basic functions of 'BodyForceTreeMap'
System.out.println("Test5:");
BodyForceTreeMap bfm = new BodyForceTreeMap();
bfm.put(earth, earth.gravitationalForce(sun));
bfm.put(sun, sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus)));
bfm.put(venus, venus.gravitationalForce(sun));
bfm.put(mars, mars.gravitationalForce(sun));
bfm.put(mercury, mercury.gravitationalForce(sun));
testValue(bfm.get(earth).distanceTo(earth.gravitationalForce(sun)), 0);
testValue(bfm.get(sun).distanceTo(sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus))), 0);
testValue(bfm.put(earth, new Vector3(0, 0, 0)).distanceTo(earth.gravitationalForce(sun)), 0);
testValue(bfm.get(earth).distanceTo(new Vector3(0, 0, 0)), 0);
testValue(bfm.get(mercury), mercury.gravitationalForce(sun));
}
public static void testComparison(Object first, Object second, boolean expected) {
boolean real = first == second;
if (real == expected) {
System.out.println("Successful comparison");
} else {
System.out.println("Comparison NOT successful! Expected value: " + expected + " / Given value: " + real);
}
}
public static void testValue(Object given, Object expected) {
if (given == expected) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
}
public static void testValue(double given, double expected) {
if (given < expected + (expected + 1) / 1e12 && given > expected - (expected + 1) / 1e12) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
}
}

40
src/BodyForceTreeMap.java Normal file
View File

@ -0,0 +1,40 @@
// A map that associates a Body with a Vector3 (typically this is the force exerted on the body).
// The number of key-value pairs is not limited.
public class BodyForceTreeMap {
//TODO: declare variables.
// Adds a new key-value association to this map. If the key already exists in this map,
// the value is replaced and the old value is returned. Otherwise 'null' is returned.
// Precondition: key != null.
public Vector3 put(Body key, Vector3 value) {
//TODO: implement method.
return null;
}
// Returns the value associated with the specified key, i.e. the method returns the force vector
// associated with the specified key. Returns 'null' if the key is not contained in this map.
// Precondition: key != null.
public Vector3 get(Body key) {
//TODO: implement method.
return null;
}
// Returns 'true' if this map contains a mapping for the specified key.
public boolean containsKey(Body key) {
//TODO: implement method.
return false;
}
// Returns a readable representation of this map, in which key-value pairs are ordered
// descending according to the mass of the bodies.
public String toString() {
//TODO: implement method.
return null;
}
}

103
src/BodyLinkedList.java Normal file
View File

@ -0,0 +1,103 @@
// A list of bodies implemented as a linked list.
// The number of elements of the list is not limited.
public class BodyLinkedList {
//TODO: declare variables.
// Initializes 'this' as an empty list.
public BodyLinkedList() {
//TODO: define constructor.
}
// Initializes 'this' as an independent copy of the specified list 'list'.
// Calling methods of this list will not affect the specified list 'list'
// and vice versa.
// Precondition: list != null.
public BodyLinkedList(BodyLinkedList list) {
//TODO: define constructor.
}
// Inserts the specified element 'body' at the beginning of this list.
public void addFirst(Body body) {
//TODO: implement method.
}
// Appends the specified element 'body' to the end of this list.
public void addLast(Body body) {
//TODO: implement method.
}
// Returns the last element in this list.
// Returns 'null' if the list is empty.
public Body getLast() {
//TODO: implement method.
return null;
}
// Returns the first element in this list.
// Returns 'null' if the list is empty.
public Body getFirst() {
//TODO: implement method.
return null;
}
// Retrieves and removes the first element in this list.
// Returns 'null' if the list is empty.
public Body pollFirst() {
//TODO: implement method.
return null;
}
// Retrieves and removes the last element in this list.
// Returns 'null' if the list is empty.
public Body pollLast() {
//TODO: implement method.
return null;
}
// Inserts the specified element 'body' at the specified position in this list.
// Precondition: i >= 0 && i <= size().
public void add(int i, Body body) {
//TODO: implement method.
}
// Returns the element at the specified position in this list.
// Precondition: i >= 0 && i < size().
public Body get(int i) {
//TODO: implement method.
return null;
}
// Returns the index of the first occurrence of the specified element in this list, or -1 if
// this list does not contain the element.
public int indexOf(Body body) {
//TODO: implement method.
return -2;
}
// Removes all bodies of this list, which are colliding with the specified
// body. Returns a list with all the removed bodies.
public BodyLinkedList removeCollidingWith(Body body) {
//TODO: implement method.
return null;
}
// Returns the number of bodies in this list.
public int size() {
//TODO: implement method.
return -1;
}
}

11
src/Simulation3.java Normal file
View File

@ -0,0 +1,11 @@
// Simulates the formation of a massive solar system.
//
public class Simulation3 {
// The main simulation method using instances of other classes.
public static void main(String[] args) {
//TODO: change implementation of this method according to 'Aufgabenblatt3.md'.
}
}