11 Commits

Author SHA1 Message Date
8f6320e069 Finish Übungstest 6 2022-06-02 14:37:11 +02:00
d718ba90e3 Fix AB6, code style 2022-06-02 14:28:43 +02:00
eab0db8e40 Aufgabenblatt 8 2022-05-30 13:18:56 +00:00
406c9b02f7 Aufgabenblatt 8 2022-05-30 14:58:17 +02:00
05c52cd3f5 Abgabe Übungstest 5 2022-05-19 14:38:54 +02:00
088fa3cdeb Implement AB6 2022-05-17 19:43:03 +02:00
f801a331c2 Implement AB6, Aufgabe 1+2 2022-05-17 19:31:19 +02:00
b89fc15602 Refactor AB6 Angabe 2022-05-17 17:28:26 +02:00
2e14e45bb0 Aufgabenblatt 6 2022-05-16 18:39:22 +00:00
e311ef3c6b Aufgabenblatt 6 2022-05-16 20:27:14 +02:00
a62ef91a8a Implement Übungstest 4 2022-05-12 14:35:07 +02:00
33 changed files with 1102 additions and 14 deletions

75
angabe/Aufgabenblatt6.md Normal file
View File

@ -0,0 +1,75 @@
# Aufgabenblatt 6
## Allgemeine Anmerkungen
Ihre Lösung für dieses Aufgabenblatt ist bis Montag, 23.5. 11h durch `git commit` und `git push`
abzugeben. Mit der Angabe werden die Dateien `MassiveIterable.java`, `MassiveIterator.java`,
`MassiveSet.java`, `MassiveForceTreeMap.java`, `Simulation6.java` und `Aufgabe6Test.java`
mitgeliefert.
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 Anwendung der Konzepte: Iterator, Kopie vs. Sichtweise, Sortieren
(siehe Skriptum Seite 91-109).
## Beschreibung der gegebenen Dateien
- [MassiveIterable](../src/MassiveIterable.java) ist ein Interface, das iterierbare Objekte mit
Elementen vom Typ `Massive` spezifiziert. Verändern Sie diese Datei bitte nicht.
- [MassiveIterator](../src/MassiveIterator.java) ist ein Interface, das einen Iterator über
Elemente vom Typ `Massive` spezifiziert. Verändern Sie diese Datei bitte nicht.
- [MassiveSet](../src/MassiveSet.java) ist ein Interface, das iterierbare Mengen mit
`Massive`-Elementen spezifiziert. Verändern Sie diese Datei bitte nicht.
- [MassiveForceTreeMap](../src/MassiveForceTreeMap.java) ist das Gerüst für eine Implementierung
einer assoziativen Datenstruktur, die ein `Massive`-Objekt mit der auf das Objekt wirkenden Kraft
assoziiert.
- [Simulation6](../src/Simulation6.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).
- [Aufgabe6Test](../src/Aufgabe6Test.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. Entfernen Sie die Kommentarzeichen, um diese Klasse verwenden zu können. Sie
müssen diese Klasse nicht weiter verändern, können aber eigene Testfälle hinzufügen.
## Aufgaben
Ihre Aufgaben sind folgende:
**1. Implementieren Sie die Klasse `MassiveForceTreeMap`.**
Implementieren Sie die Klasse `MassiveForceTreeMap`. `MassiveForceTreeMap` ist wie
`BodyForceTreeMap` aufgebaut, mit dem Unterschied, dass der Typ des Schlüssels statt `Body` nun
der Typ `Massive` ist. Weiters soll die Klasse Methode `getKeys()` zur Verfügung stellen,
die eine `MassiveSet`-Sichtweise auf die Menge der Schlüssel liefert. Änderungen an dem
zurückgelieferten `MassiveSet`-Objekt wirken sich auf das zugrunde
liegende `MassiveForceTreeMap`-Objekt aus. Die Methode `toList()` liefert dagegen eine
unabhängige Liste (Kopie) mit allen Schlüsseln der Map. Für die Implementierung von
`MassiveSet` können Sie einen eigenen Klassennamen beginnend mit `My` wählen. Die Definition kann
in einer eigenen Datei oder in der Datei `MassiveForceTreeMap.java` erfolgen.
**2. Adaptieren Sie die Klasse `HierarchicalSystem`:**
Die Klasse `HierarchicalSystem` soll so geändert werden, dass sie das gegebene
Interface `MassiveIterable` implementiert. Die Reihenfolge der vom Iterator gelieferten
Elemente ist nicht festgelegt. Sie dürfen für die Implementierung bei Bedarf Ihren Klassen
`NamedBodyForcePair` und `HierarchicalSystem` neue, nicht angegebene Methoden hinzufügen.
Die Verwendung von Klassen des Java-Collection-Frameworks (z.B. java.util.Stack) ist erlaubt
(aber nicht notwendig).
**3. Implementierung von `Simulation6`:**
Implementieren Sie die Simulationsschleife unter Verwendung eines Objekts vom Typ
`MassiveForceTreeMap`. Die Methode `getKeys()` hilft beim Iterieren der gespeicherten Schlüssel.
Kollisionen von Himmelskörpern müssen in dieser Simulation nicht berücksichtigt werden.
#### _Punkteaufteilung_
- Implementierung von `MassiveForceTreeMap`: 3 Punkte
- Implementierung von `MassiveIterable` in `HierarchicalSystem`: 1.5 Punkte
- Implementierung von `Simulation6`: 0.5 Punkte
- Gesamt: 5 Punkte

152
angabe/Aufgabenblatt8.md Normal file
View File

@ -0,0 +1,152 @@
# Aufgabenblatt 8
## Allgemeine Anmerkungen
Ihre Lösung für dieses Aufgabenblatt ist bis Dienstag, 13.6., 11h durch `git commit` und `git push`
abzugeben.
Wenn Sie zusätzlich zu den gefragten Klassen und Interfaces weitere Klassen oder
Interfaces definieren, achten Sie darauf, dass die Klassennamen mit `My` beginnen, um Konflikte
mit späteren Aufgabenblättern zu vermeiden.
Weiters werden die Dateien `StateFileNotFoundException.java`,
`StateFileFormatException.java`, `ReadDataUtil.java`, `Simulation8.java`, `Simulation9.java`,
`Aufgabe8Test.java` und das Verzeichnis `states` mit `txt`-Dateien mitgeliefert.
## Ziel
Ziel der Aufgabe ist das Verständnis und die Anwendung der Konzepte: Java-Collections, Eingabe mit
Validierung, Exceptions (S. 110-123).
## Beschreibung der gegebenen Dateien
- [states](../states) ist ein Verzeichnis, in dem mehrere Dateien mit der Endung `.txt`
mitgeliefert werden. Diese enthalten Daten von je einem Himmelskörper sowie dessen Positionen und
Geschwindigkeitsvektoren für alle Tage der Jahre 2019-2021. Die Angaben sind wie gewohnt in
kartesischen Koordinaten, wobei die Sonne den Urspung des Koordinatensystems bildet und die
Ekliptik die x-y-Ebene darstellt. Die Daten stammen von [https://ssd.jpl.nasa.gov/horizons.cgi#top](https://ssd.jpl.nasa.gov/horizons.cgi#top).
**ACHTUNG**: Die Werte sind in km bzw. km/sec angegeben!
- [StateFileNotFoundException](../src/StateFileNotFoundException.java) enthält die Definition
der Klasse `StateFileNotFoundException`. Diese sollen Sie vervollständigen.
- [StateFileFormatException](../src/StateFileFormatException.java) enthält die Definition
der Klasse `StateFileFormatException`. Diese sollen Sie vervollständigen.
- [ReadDataUtil](../src/ReadDataUtil.java) ist eine Klasse mit einer statischen Methode zum
Einlesen von Position- und Bewegungsvektoren von Himmelskörpern aus Dateien.
Diese sollen Sie vervollständigen.
- [Simulation8](../src/Simulation8.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).
- [Simulation9](../src/Simulation9.java) ist eine leere Datei. Hier soll eine weitere Simulation
implementiert werden, die so wie `Simulation8` funktioniert, mit dem Unterschied, dass anstelle
der selbst implementierten Datenstrukturen nur vorgefertigte Klassen aus dem
Java-Collection-Framework benutzt werden sollen.
- [Aufgabe8Test](../src/Aufgabe8Test.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. Entfernen Sie die Kommentarzeichen, um diese Klasse verwenden zu können. Sie
müssen diese Klasse nicht weiter verändern, können aber eigene Testfälle hinzufügen.
## Aufgaben
1. Ändern Sie Ihre Implementierung, sodass ein `MassiveIterator` eine Exception
vom Typ `java.util.NoSuchElementException` wirft, falls `next()` aufgerufen wird,
jedoch der Iterator keine weitere Iteration hat (`hasNext()` liefert `false`).
2. Ändern Sie den Iterator von der von `getKeys()` zurückgelieferten
Sichtweise auf `MassiveForceTreeMap` so, dass `remove()` überschrieben wird
([siehe API Dokumentation](https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#remove--)),
sodass der Iterator Einträge in Objekten von `MassiveForceTreeMap` löschen kann. Achten Sie
darauf, dass hier in bestimmten Fällen eine `java.lang.IllegalStateException` geworfen werden
soll. Der Iterator von `HierarchicalSystem` muss nicht geändert werden.
3. Validierung von Eingabedaten:
- Implementieren Sie in der Klasse `ReadDataUtil.java` die Methode `readConfiguration`.
Es soll ein gepufferter Stream zum Einlesen genutzt werden (siehe Skriptum Seite 128).
Erstellen Sie zum Testen auch Varianten der txt-Dateien mit Formatfehlern.
- Fügen Sie der Klasse `NamedBody` bei Bedarf
eine Methode `setState(Vector3 position, Vector3 velocity)` zum Setzen der Position
und des Geschwindigkeitsvektors des Himmelskörpers hinzu.
- Definieren Sie die beiden angegebenen Exceptionklassen in den entsprechenden
mitgelieferten Dateien.
4. Ausnahmebehandlung:
In der Klasse `Simulation8` sollen nun die Himmelskörper mit Daten aus den gegebenen
txt-Dateien initialisiert werden. Dabei sollen zumindest die Sonne sowie die
inneren Planeten
Merkur, Venus, Erde und Mars vorkommen. Sie können weitere Himmelskörper (siehe txt-Dateien)
hinzufügen. Nutzen Sie die Klasse `MassiveForceTreeMap` und ihre Iteratoren, um die
Himmelskörper der Simulation zu verwalten. (Kollisionen von Himmelskörpern müssen nicht
berücksichtigt werden.)
Ändern Sie die Klasse `Simulation8` so, dass sie zwei Kommandozeilenargumente verarbeitet.
Das erste Argument ist ein String mit der Angabe des Pfades zum Verzeichnis, wo die
entsprechenden txt-Dateien (z.B. `Venus.txt`,`Mercury.txt`,`Earth.txt`) mit den Konfigurationen
der Himmelskörper zu finden sind. Die Dateien haben die Namen der Himmelskörper mit Endung `
.txt`. Für die Sonne gibt es keine txt-Datei (es wird die Position (0,0,0) angenommen).
Das zweite Argument ist ein String mit einer Datumsangabe der Form YYYY-MMM-DD, also z.B.
2020-Dec-04, die den Tag der auszulesenden Position und Bewegungsvektor bestimmt. Die Klasse
soll beim Auftreten von Problemen bei der Ausführung entsprechende Fehlermeldungen ausgeben und
die Ausführung in bestimmten Fällen beenden. Beispiele für Aufrufe im Kommandozeileninterpreter
mit entsprechenden Fehlermeldungen (Sie können zum Ausführen das Terminal in IntelliJ nutzen
oder die Programmargumente unter `Edit Configurations` angeben):
```
$ javac Simulation8.java
$ java Simulation8 ../states 2021-May-28
Running simulation ...
$ java Simulation8 ../states
Error: wrong number of arguments.
$ java Simulation8 ../states 2025-Dec-12
Warning: State not available for Earth.
Running simulation without Earth.
Warning: State not available for Venus.
Running simulation without Venus.
...
$ java Simulation8 ../states-altered 2021-May-28
Warning: File ../states-altered/Venus.txt does not have required format.
Running simulation without Venus.
Warning: File ../states-altered/Mars.txt not found.
Running simulation without Mars.
Running simulation ...
$ java Simulation8 ../states -17
Error: State has wrong format (requires YYYY-MM-DD), aborting.
$ java Simulation8 blah 2021-May-28
Warning: File blah/Earth.txt not found.
Running simulation without Earth.
Warning: File blah/Venus.txt not found.
Running simulation without Venus.
...
```
5. Kopieren Sie den Inhalt der Datei `Simulation8.java` in die Datei `Simulation9.java` und bauen
Sie `Simulation9` so um, dass anstelle der selbst implementierten Datenstrukturen nur
vorgefertigte Klassen aus dem Java-Collection-Framework benutzt werden.
6. Freiwillige Zusatzaufgabe (ohne Bewertung):
Ändern Sie die Klasse `Simulation8` so um, dass ein drittes optionales Kommandozeilenargument
verarbeitet werden kann. Dieses gibt an, wieviele Tage simuliert werden sollen. Beispielsweise
kann eine zweite Datumsangabe möglich sein, oder die Anzahl an Tagen.
Sobald dieser Zeitpunkt in der Simulation erreicht wurde, können die aktuellen Positionen der
Himmelskörper mit den in den txt-Dateien angegebenen Positionen verglichen werden (z.B.
durch erneutem Aufruf von `readConfiguration` und `draw`). Wie groß sind die Abweichungen
der von NASA errechneten Positionen zu den Positionen, die Ihre Simulation liefert?
### Denkanstöße (ohne Bewertung)
1. Haben Sie die remove-Methode des Iterators so implementiert, dass der Aufruf keine
zusätzliche Suche nach dem zu löschenden Eintrag benötigt?
2. Wie verhalten sich die von der Methode `toList()` der Klasse `MassiveForceTreeMap`
zurückgelieferten Listen, wenn deren enthaltene Himmelskörper durch `setState` verändert werden?
Werden dadurch die Himmelskörper der ursprünglichen `MassiveForceTreeMap`-Objekte auch geändert?
(Anmerkung: diesbezüglich gibt es im Aufgabenblatt 6 keine Vorgaben).
3. Wie verhalten sich Ihre Iteratoren, wenn Objekte geändert werden?
4. Wie kann man durch Einfügen von Zeichen `,` und newlines (`\n`) aus den `txt`-Dateien eine
"fehlerhafte" Datei machen, die trotzdem von der Methode akzeptiert wird? Kann man solche Probleme
verhindern?
#### _Punkteaufteilung_
- Änderung von `next()` von `MassiveIterator`: 0.5 Punkte
- Implementierung der Methode `remove()` im Iterator der `MassiveSet`-Sichtweise
von `MassiveForceTreeMap`: 2 Punkte
- Implementierung der Exceptionklassen und Implementierung von `readConfiguration`
in `ReadDataUtil`: 1.5 Punkte
- Implementierung von `Simulation8` und `Simulation9`: 1 Punkt

View File

@ -78,4 +78,19 @@ public class Aufgabe5Test {
assertEquals(hashCode2, map.hashCode());
assertNotEquals(hashCode1, hashCode2);
}
@Test
public void testDelKey() {
MassiveForceHashMap map = new MassiveForceHashMap();
NamedBody sun1 = new NamedBody(SolSystem4.SUN_NAMED);
NamedBody earth1 = new NamedBody(SolSystem4.EARTH_NAMED);
NamedBody moon1 = new NamedBody(SolSystem4.MOON_NAMED);
map.put(sun1, new Vector3());
map.put(earth1, new Vector3());
map.put(moon1, new Vector3());
assertNotNull(map.get(sun1));
assertNotNull(map.delete(sun1));
assertNull(map.get(sun1));
assertNull(map.delete(sun1));
}
}

133
src/Aufgabe6Test.java Normal file
View File

@ -0,0 +1,133 @@
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import static org.junit.jupiter.api.Assertions.*;
public class Aufgabe6Test {
@Test
public void testEP2() {
NamedBody sun1, mercury1, venus1, earth1, moon1, mars1, deimos1, phobos1, vesta1, pallas1, hygiea1, ceres1;
// create the same 12 named body-force pairs
sun1 = new NamedBody(SolSystem4.SUN_NAMED);
earth1 = new NamedBody(SolSystem4.EARTH_NAMED);
moon1 = new NamedBody(SolSystem4.MOON_NAMED);
mars1 = new NamedBody(SolSystem4.MARS_NAMED);
deimos1 = new NamedBody(SolSystem4.DEIMOS_NAMED);
phobos1 = new NamedBody(SolSystem4.PHOBOS_NAMED);
mercury1 = new NamedBody(SolSystem4.MERCURY_NAMED);
venus1 = new NamedBody(SolSystem4.VENUS_NAMED);
vesta1 = new NamedBody(SolSystem4.VESTA_NAMED);
pallas1 = new NamedBody(SolSystem4.PALLAS_NAMED);
hygiea1 = new NamedBody(SolSystem4.HYGIEA_NAMED);
ceres1 = new NamedBody(SolSystem4.CERES_NAMED);
// check basic functions of 'MassiveForceHashMap'
MassiveForceTreeMap map = new MassiveForceTreeMap();
map.put(sun1, new Vector3(0, 0, 0));
map.put(mercury1, new Vector3(0, 0, 0));
map.put(venus1, new Vector3(0, 0, 0));
map.put(earth1, new Vector3(0, 0, 0));
map.put(moon1, new Vector3(0, 0, 0));
map.put(mars1, new Vector3(0, 0, 0));
map.put(deimos1, new Vector3(0, 0, 0));
map.put(phobos1, new Vector3(0, 0, 0));
map.put(vesta1, new Vector3(0, 0, 0));
map.put(pallas1, new Vector3(0, 0, 0));
map.put(hygiea1, new Vector3(0, 0, 0));
map.put(ceres1, new Vector3(0, 0, 0));
map.put(mars1, new Vector3(0, 0, 0)); // inserted twice
HashSet<Massive> set1 = new HashSet<>();
set1.add(sun1);
set1.add(mercury1);
set1.add(venus1);
set1.add(earth1);
set1.add(moon1);
set1.add(mars1);
set1.add(deimos1);
set1.add(phobos1);
set1.add(vesta1);
set1.add(pallas1);
set1.add(hygiea1);
set1.add(ceres1);
assertTrue(map.toString().contains("Mars"));
assertTrue(map.toString().contains("Deimos"));
assertTrue(map.toString().contains("Moon"));
assertTrue(map.toString().contains("Earth"));
assertEquals(12, map.getKeys().size());
assertTrue(map.getKeys().contains(mars1));
assertTrue(map.getKeys().contains(new NamedBody("Mars", 6.41712E23, new Vector3(0, 0, 0), new Vector3(0, 0, 0))));
assertFalse(map.getKeys().contains(new Body(6.41712E23, new Vector3(0, 0, 0), new Vector3(0, 0, 0))));
HashSet<Massive> set2 = new HashSet<>();
for (Massive m : map.getKeys()) {
set2.add(m);
}
assertEquals(set1, set2);
MassiveLinkedList list = map.getKeys().toList();
while (list.size() > 0) {
set1.remove(list.pollLast());
}
assertTrue(set1.isEmpty());
map.getKeys().remove(mars1);
assertFalse(map.containsKey(mars1));
assertEquals(11, map.getKeys().size());
map.getKeys().clear();
assertEquals(0, map.getKeys().size());
NamedBodyForcePair sun2, mercury2, venus2, earth2, moon2, mars2, deimos2, phobos2, vesta2, pallas2, hygiea2, ceres2;
//test classes NamedBody and MassiveForceHashMap
// create 12 named bodies
// create the same 12 named body-force pairs
sun2 = new NamedBodyForcePair(SolSystem4.SUN_NAMED);
earth2 = new NamedBodyForcePair(SolSystem4.EARTH_NAMED);
moon2 = new NamedBodyForcePair(SolSystem4.MOON_NAMED);
mars2 = new NamedBodyForcePair(SolSystem4.MARS_NAMED);
deimos2 = new NamedBodyForcePair(SolSystem4.DEIMOS_NAMED);
phobos2 = new NamedBodyForcePair(SolSystem4.PHOBOS_NAMED);
mercury2 = new NamedBodyForcePair(SolSystem4.MERCURY_NAMED);
venus2 = new NamedBodyForcePair(SolSystem4.VENUS_NAMED);
vesta2 = new NamedBodyForcePair(SolSystem4.VESTA_NAMED);
pallas2 = new NamedBodyForcePair(SolSystem4.PALLAS_NAMED);
hygiea2 = new NamedBodyForcePair(SolSystem4.HYGIEA_NAMED);
ceres2 = new NamedBodyForcePair(SolSystem4.CERES_NAMED);
CosmicSystem earthSystem = new HierarchicalSystem(earth2, moon2);
CosmicSystem marsSystem = new HierarchicalSystem(mars2, deimos2, phobos2);
HierarchicalSystem solarSystem = new HierarchicalSystem(sun2, mercury2, venus2, earthSystem, marsSystem, vesta2, pallas2, hygiea2, ceres2);
int count = 0;
for (Massive b : solarSystem) {
count++;
}
assertEquals(12, count);
}
@Test
public void testIterator() {
NamedBody sun1, mercury1, venus1;
sun1 = new NamedBody(SolSystem4.SUN_NAMED);
mercury1 = new NamedBody(SolSystem4.MERCURY_NAMED);
venus1 = new NamedBody(SolSystem4.VENUS_NAMED);
MassiveForceTreeMap map = new MassiveForceTreeMap();
map.put(sun1, new Vector3());
map.put(mercury1, new Vector3());
map.put(venus1, new Vector3());
for (Massive m : map.getKeys().toList()) {
System.out.println(m);
}
}
}

100
src/Aufgabe8Test.java Normal file
View File

@ -0,0 +1,100 @@
import java.util.HashSet;
import java.util.NoSuchElementException;
public class Aufgabe8Test {
public static void main(String[] args) {
/* //TODO: uncomment for testing
MassiveForceTreeMap map = new MassiveForceTreeMap();
NamedBody mars;
map.put(new NamedBody("Oumuamua", 8e6, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Earth", 5.972E24, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Moon", 7.349E22, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(mars = new NamedBody("Mars", 6.41712E23, new Vector3(0, 0, 0),
new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Deimos", 1.8E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Phobos", 1.08E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Mercury", 3.301E23, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Venus", 4.86747E24, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Vesta", 2.5908E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Pallas", 2.14E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Hygiea", 8.32E19, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Ceres", 9.394E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
System.out.println("Test1:");
MassiveIterator iterator = map.getKeys().iterator();
int count = 0;
while(iterator.hasNext()) {
if (iterator.next().equals(mars)) {
iterator.remove();
}
count++;
}
testValue(count, 12);
testValue(map.getKeys().size(), 11);
testValue(map.getKeys().contains(mars), false);
System.out.println("Test2:");
try {
iterator.next();
// this statement must not be reached
testValue(true, false);
} catch (NoSuchElementException e) {
testValue(true, true);
}
System.out.println("Test3:");
iterator = map.getKeys().iterator();
while(iterator.hasNext()) {
iterator.next();
iterator.remove();
}
testValue(map.getKeys().size(),0);
*/ //TODO: uncomment
}
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);
}
}
}

View File

@ -103,10 +103,10 @@ public class BodyForceTreeMap {
*/
@Override
public String toString() {
return toString(root);
return (root != null) ? toString(root) : "";
}
private class Item {
private static class Item {
private final Body key;
private Vector3 value;
private Item parent;

View File

@ -222,7 +222,7 @@ public class BodyLinkedList implements Iterable<Body> {
};
}
private class Item {
private static class Item {
private final Body body;
private Item prev;
private Item next;

View File

@ -5,11 +5,11 @@ import codedraw.CodeDraw;
* and an arbitrary number of subsystems (of type 'CosmicSystem') in its orbit.
* This class implements 'CosmicSystem'.
*/
public class HierarchicalSystem implements CosmicSystem {
public class HierarchicalSystem implements CosmicSystem, MassiveIterable {
private final NamedBodyForcePair central;
private final CosmicSystem[] orbit;
private final CosmicSystem[] all;
private CosmicSystem[] orbit;
private CosmicSystem[] all;
/**
* Initializes this system with a name and a central body.
@ -110,4 +110,68 @@ public class HierarchicalSystem implements CosmicSystem {
return sb.toString();
}
/**
* Puts the system 'cs' at the first place in the orbit of this system.
* Precondition: cs != null
*/
public boolean putFirst(CosmicSystem cs) {
CosmicSystem[] old = orbit;
orbit = new CosmicSystem[old.length + 1];
all = new CosmicSystem[old.length + 2];
orbit[0] = cs;
System.arraycopy(old, 0, orbit, 1, old.length);
all[0] = central;
System.arraycopy(orbit, 0, all, 1, orbit.length);
return true;
}
@Override
public MassiveIterator iterator() {
return new MassiveIterator() {
private int i = 0;
private MassiveIterator cur = null;
@Override
public Massive next() {
if (cur != null && cur.hasNext()) return cur.next();
while (i < all.length) {
CosmicSystem sys = all[i++];
if (sys instanceof NamedBodyForcePair m) {
return m.getBody();
} else if (sys instanceof HierarchicalSystem hs) {
cur = hs.iterator();
if (cur.hasNext()) {
return cur.next();
}
}
}
return null;
}
@Override
public boolean hasNext() {
if (cur != null && cur.hasNext()) return true;
while (i < all.length) {
CosmicSystem sys = all[i];
if (sys instanceof NamedBodyForcePair) return true;
i++;
if (sys instanceof HierarchicalSystem hs) {
cur = hs.iterator();
if (cur.hasNext()) {
return true;
}
}
}
return false;
}
};
}
}

View File

@ -65,16 +65,36 @@ public class MassiveForceHashMap {
* Precondition: key != null.
*/
public Vector3 get(Massive key) {
int pos = find(key);
return (pos == -1) ? null : values[pos];
}
private int find(Massive key) {
int idx = ((key.hashCode() % keys.length) + keys.length) % keys.length;
for (int i = 0; i < keys.length; i++) {
int pos = (idx + i) % keys.length;
if (keys[pos] == null) {
break;
} else if (keys[pos].equals(key)) {
return values[pos];
return pos;
}
}
return null;
return -1;
}
/**
* Deletes the mapping for the specified key from this map if present.
* Returns the previous value associated with key, or null if there was
* no mapping for key.
* Precondition: key != null
*/
public Vector3 delete(Massive key) {
int pos = find(key);
if (pos == -1) return null;
Vector3 val = values[pos];
values[pos] = null;
return val;
}
/**

View File

@ -0,0 +1,234 @@
import codedraw.CodeDraw;
/**
* A map that associates an object of 'Massive' with a Vector3. The number of key-value pairs
* is not limited.
*/
public class MassiveForceTreeMap implements MassiveSet {
private int size = 0;
private Item root;
/**
* 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(Massive key, Vector3 value) {
if (root == null) {
root = new Item(key, value);
size++;
return null;
}
Item item = root;
while (item != null) {
if (item.key.equals(key)) {
Vector3 old = item.value;
item.value = value;
return old;
} else if (item.key.mass() > key.mass()) {
if (item.left != null) {
item = item.left;
} else {
item.setLeft(new Item(key, value));
size++;
break;
}
} else {
if (item.right != null) {
item = item.right;
} else{
item.setRight(new Item(key, value));
size++;
break;
}
}
}
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(Massive key) {
Item item = root;
while (item != null) {
if (item.key.equals(key)) {
return item.value;
} else if (item.key.mass() > key.mass()) {
item = item.left;
} else {
item = item.right;
}
}
return null;
}
/**
* Returns 'true' if this map contains a mapping for the specified key.
* Precondition: key != null
*/
public boolean containsKey(Massive key) {
Item item = root;
while (item != null) {
if (item.key.equals(key)) {
return true;
} else if (item.key.mass() > key.mass()) {
item = item.left;
} else {
item = item.right;
}
}
return false;
}
private String toString(Item item) {
String s = "";
if (item == null) {
return s;
}
s += this.toString(item.right);
s += String.format("{%s: %s}\n", item.key, item.value);
s += this.toString(item.left);
return s;
}
/**
* Returns a readable representation of this map, in which key-value pairs are ordered
* descending according to 'key.getMass()'.
*/
public String toString() {
return (root != null) ? toString(root) : "";
}
/**
* Returns a `MassiveSet` view of the keys contained in this tree map. Changing the
* elements of the returned `MassiveSet` object also affects the keys in this tree map.
*/
public MassiveSet getKeys() {
return this;
}
@Override
public void draw(CodeDraw cd) {
}
@Override
public MassiveIterator iterator() {
return new MassiveIterator() {
private Item next = root.getLeftLeaf();
@Override
public Massive next() {
if (next == null) return null;
Massive m = next.key;
Item newNext = (next.right != null) ? next.right.getLeftLeaf() : next.parent;
while (newNext != null && newNext.right == next) {
next = newNext;
newNext = newNext.parent;
}
next = newNext;
return m;
}
@Override
public boolean hasNext() {
return next != null;
}
};
}
@Override
public boolean contains(Massive element) {
return containsKey(element);
}
@Override
public void remove(Massive element) {
Item item = root;
while (item != null) {
if (item.key.equals(element)) {
Item newP = null;
if (item.left != null) {
newP = item.left.getRightLeaf();
} else if (item.right != null) {
newP = item.right.getLeftLeaf();
}
if (item.parent.left == item) {
item.parent.setLeft(newP);
} else {
item.parent.setRight(newP);
}
size--;
return;
} else if (item.key.mass() > element.mass()) {
item = item.left;
} else {
item = item.right;
}
}
}
@Override
public void clear() {
size = 0;
root = null;
}
@Override
public int size() {
return size;
}
@Override
public MassiveLinkedList toList() {
MassiveLinkedList list = new MassiveLinkedList();
for (Massive m : this) {
list.addLast(m);
}
return list;
}
private static class Item {
private final Massive key;
private Vector3 value;
private Item parent;
private Item left;
private Item right;
public Item(Massive key, Vector3 value) {
this.key = key;
this.value = value;
}
public void setLeft(Item left) {
this.left = left;
if (left != null) left.parent = this;
}
public void setRight(Item right) {
this.right = right;
if (right != null) right.parent = this;
}
public Item getLeftLeaf() {
Item cur = this;
while (cur.left != null) {
cur = cur.left;
}
return cur;
}
public Item getRightLeaf() {
Item cur = this;
while (cur.right != null) {
cur = cur.right;
}
return cur;
}
}
}

11
src/MassiveIterable.java Normal file
View File

@ -0,0 +1,11 @@
/**
* Iterable objects with 'Massive' elements.
*/
public interface MassiveIterable extends Iterable<Massive> {
/**
* Returns an iterator over elements of 'Massive'.
*/
@Override
MassiveIterator iterator();
}

20
src/MassiveIterator.java Normal file
View File

@ -0,0 +1,20 @@
import java.util.Iterator;
/**
* An iterator over elements of 'Massive'.
*/
public interface MassiveIterator extends Iterator<Massive> {
/**
* Returns the next element in the iteration.
* (Returns 'null' if the iteration has no more elements.)
*/
@Override
Massive next();
/**
* Returns 'true' if the iteration has more elements.
*/
@Override
boolean hasNext();
}

View File

@ -4,7 +4,7 @@ import java.util.Iterator;
* A list of massive objects implemented as a linked list.
* The number of elements of the list is not limited.
*/
public class MassiveLinkedList implements Iterable<Massive> {
public class MassiveLinkedList implements MassiveIterable {
private int size = 0;
private Item first;
private Item last;
@ -98,7 +98,7 @@ public class MassiveLinkedList implements Iterable<Massive> {
return null;
}
Massive m = last.body;
last = last.next;
last = last.prev;
if (last != null) last.setNext(null);
size--;
return m;
@ -172,8 +172,8 @@ public class MassiveLinkedList implements Iterable<Massive> {
}
@Override
public Iterator<Massive> iterator() {
return new Iterator<>() {
public MassiveIterator iterator() {
return new MassiveIterator() {
Item ptr = first;
boolean yieldedFirst = false;
@ -194,7 +194,7 @@ public class MassiveLinkedList implements Iterable<Massive> {
};
}
private class Item {
private static class Item {
private final Massive body;
private Item prev;
private Item next;

31
src/MassiveSet.java Normal file
View File

@ -0,0 +1,31 @@
/**
* A collection of 'Massive' objects in which there are no duplicates.
*/
public interface MassiveSet extends MassiveIterable, Drawable {
/**
* Returns 'true' if the set has the specified element (i.e., has an element equal to the
* specified element).
*/
boolean contains(Massive element);
/**
* Removes the specified element from the set.
*/
void remove(Massive element);
/**
* Removes all elements from the set.
*/
void clear();
/**
* Returns the number of elements in the set.
*/
int size();
/**
* Returns an object of 'MassiveLinkedList' with all elements of 'this'.
*/
MassiveLinkedList toList();
}

27
src/ReadDataUtil.java Normal file
View File

@ -0,0 +1,27 @@
import java.io.*;
public class ReadDataUtil {
// Reads the position and velocity vector on the specified 'day' from the file with the
// specified 'path', and sets position and current velocity of 'b' accordingly. If
// successful the method returns 'true'. If the specified 'day' was not found in the file,
// 'b' is unchanged and the method returns 'false'.
// The file format is validated before reading the state.
// Lines before the line "$$SOE" and after the line "$$EOE" the are ignored. Each line of the
// file between the line "$$SOE" and the line "$$EOE" is required to have the following format:
// JDTDB, TIME, X, Y, Z, VX, VY, VZ
// where JDTDB is interpretable as a 'double' value, TIME is a string and X, Y, Z, VX, VY and
// VZ are interpretable as 'double' values. JDTDB can be ignored. The character ',' must only
// be used as field separator. If the file is not found, an exception of the class
// 'StateFileNotFoundException' is thrown. If it does not comply with the format described
// above, the method throws an exception of the class 'StateFileFormatException'. Both
// exceptions are subtypes of 'IOException'.
// Precondition: b != null, path != null, day != null and has the format YYYY-MM-DD.
public static boolean readConfiguration(NamedBody b, String path, String day)
throws IOException {
// TODO: implement this method.
return false;
}
}

View File

@ -35,7 +35,6 @@ public class Simulation5 {
* The main simulation method using instances of other classes.
*/
public static void main(String[] args) {
// simulation
CodeDraw cd = new CodeDraw();
@ -52,6 +51,7 @@ public class Simulation5 {
NamedBody pallas = new NamedBody(SolSystem4.PALLAS_NAMED);
NamedBody hygiea = new NamedBody(SolSystem4.HYGIEA_NAMED);
NamedBody ceres = new NamedBody(SolSystem4.CERES_NAMED);
// create some additional bodies
Body[] bodies = new Body[NUMBER_OF_BODIES];

113
src/Simulation6.java Normal file
View File

@ -0,0 +1,113 @@
import codedraw.CodeDraw;
import java.awt.*;
import java.util.Random;
/**
* Simulates the formation of a massive solar system.
*/
public class Simulation6 {
// gravitational constant
public static final double G = 6.6743e-11;
// one astronomical unit (AU) is the average distance of earth to the sun.
public static final double AU = 150e9; // meters
// one light year
public static final double LY = 9.461e15; // meters
// some further constants needed in the simulation
public static final double SUN_MASS = 1.989e30; // kilograms
public static final double SUN_RADIUS = 696340e3; // meters
public static final double EARTH_MASS = 5.972e24; // kilograms
public static final double EARTH_RADIUS = 6371e3; // meters
// set some system parameters
public static final double SECTION_SIZE = 10 * AU; // the size of the square region in space
public static final int NUMBER_OF_BODIES = 22;
public static final double OVERALL_SYSTEM_MASS = 20 * SUN_MASS; // kilograms
// all quantities are based on units of kilogram respectively second and meter.
/**
* The main simulation method using instances of other classes.
*/
public static void main(String[] args) {
// simulation
CodeDraw cd = new CodeDraw();
// create solar system with 12 bodies
NamedBody sun = new NamedBody(SolSystem4.SUN_NAMED);
NamedBody earth = new NamedBody(SolSystem4.EARTH_NAMED);
NamedBody moon = new NamedBody(SolSystem4.MOON_NAMED);
NamedBody mars = new NamedBody(SolSystem4.MARS_NAMED);
NamedBody deimos = new NamedBody(SolSystem4.DEIMOS_NAMED);
NamedBody phobos = new NamedBody(SolSystem4.PHOBOS_NAMED);
NamedBody mercury = new NamedBody(SolSystem4.MERCURY_NAMED);
NamedBody venus = new NamedBody(SolSystem4.VENUS_NAMED);
NamedBody vesta = new NamedBody(SolSystem4.VESTA_NAMED);
NamedBody pallas = new NamedBody(SolSystem4.PALLAS_NAMED);
NamedBody hygiea = new NamedBody(SolSystem4.HYGIEA_NAMED);
NamedBody ceres = new NamedBody(SolSystem4.CERES_NAMED);
// create some additional bodies
Body[] bodies = new Body[NUMBER_OF_BODIES];
Random random = new Random(2022);
for (int i = 0; i < bodies.length; i++) {
bodies[i] = new Body(
Math.abs(random.nextGaussian()) * OVERALL_SYSTEM_MASS / bodies.length,
new Vector3(0.2 * random.nextGaussian() * AU, 0.2 * random.nextGaussian() * AU, 0.2 * random.nextGaussian() * AU),
new Vector3(0 + random.nextGaussian() * 5e3, 0 + random.nextGaussian() * 5e3, 0 + random.nextGaussian() * 5e3)
);
}
MassiveForceTreeMap forceOnBody = new MassiveForceTreeMap();
forceOnBody.put(sun, new Vector3());
forceOnBody.put(earth, new Vector3());
forceOnBody.put(moon, new Vector3());
forceOnBody.put(mars, new Vector3());
forceOnBody.put(deimos, new Vector3());
forceOnBody.put(phobos, new Vector3());
forceOnBody.put(mercury, new Vector3());
forceOnBody.put(venus, new Vector3());
forceOnBody.put(vesta, new Vector3());
forceOnBody.put(pallas, new Vector3());
forceOnBody.put(hygiea, new Vector3());
forceOnBody.put(ceres, new Vector3());
for (Body b : bodies) {
forceOnBody.put(b, new Vector3());
}
long seconds = 0;
while (true) {
seconds++;
for (Massive b1 : forceOnBody.getKeys()) {
Vector3 force = new Vector3();
for (Massive b2 : forceOnBody.getKeys()) {
if (b1 != b2) {
force = force.plus(b1.gravitationalForce(b2));
}
}
forceOnBody.put(b1, force);
}
for (Massive body : forceOnBody.getKeys()) {
body.move(forceOnBody.get(body));
}
if ((seconds % 3600) == 0) {
cd.clear(Color.BLACK);
for (Massive body : forceOnBody.getKeys()) {
body.draw(cd);
}
cd.show();
}
}
}
}

60
src/Simulation8.java Normal file
View File

@ -0,0 +1,60 @@
import codedraw.CodeDraw;
// Simulates the solar system.
//
public class Simulation8 {
// gravitational constant
public static final double G = 6.6743e-11;
// one astronomical unit (AU) is the average distance of earth to the sun.
public static final double AU = 150e9; // meters
// set some system parameters
public static final double SECTION_SIZE = 10 * AU; // the size of the square region in space
// all quantities are based on units of kilogram respectively second and meter.
// The main simulation method using instances of other classes.
public static void main(String[] args) {
// simulation
CodeDraw cd = new CodeDraw();
// create solar system with 13 bodies
MassiveForceTreeMap map = new MassiveForceTreeMap();
map.put(new NamedBody("Oumuamua", 8e6, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Earth", 5.972E24, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Moon", 7.349E22, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Mars1", 6.41712E23, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Deimos", 1.8E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Phobos", 1.08E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Mercury", 3.301E23, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Venus", 4.86747E24, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Vesta", 2.5908E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Pallas", 2.14E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Hygiea", 8.32E19, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
map.put(new NamedBody("Ceres1", 9.394E20, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
//TODO: implementation of this method according to 'Aufgabenblatt8.md'.
// add sun after states have been read from files.
map.put(new NamedBody("Sun", 1.989E30, new Vector3(0, 0, 0), new Vector3(0, 0, 0)),
new Vector3(0, 0, 0));
}
}

7
src/Simulation9.java Normal file
View File

@ -0,0 +1,7 @@
// Simulates the solar system.
//
public class Simulation9 {
// TODO: Implement the simulation using the Java-Collections framework.
}

View File

@ -0,0 +1,7 @@
import java.io.IOException;
public class StateFileFormatException extends IOException {
// TODO: implement class
}

View File

@ -0,0 +1,7 @@
import java.io.IOException;
public class StateFileNotFoundException extends IOException {
// TODO: implement class
}

1
states/Ceres.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Deimos.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Earth.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Hygiea.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Mars.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Mercury.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Moon.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Oumuamua.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Pallas.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Phobos.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Venus.txt Normal file

File diff suppressed because one or more lines are too long

1
states/Vesta.txt Normal file

File diff suppressed because one or more lines are too long