10 Commits

Author SHA1 Message Date
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
Anton Ertl
2e14e45bb0 Aufgabenblatt 6 2022-05-16 18:39:22 +00:00
Anton Ertl
e311ef3c6b Aufgabenblatt 6 2022-05-16 20:27:14 +02:00
a62ef91a8a Implement Übungstest 4 2022-05-12 14:35:07 +02:00
34c2ac91c6 Finished AB5 2022-05-07 13:20:59 +02:00
a075544fe2 Refactor BodyLinkedList 2022-05-07 12:01:43 +02:00
391b389063 Refactor BodyForceTreeMap 2022-05-07 12:01:22 +02:00
18 changed files with 1191 additions and 258 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

View File

@@ -6,29 +6,27 @@ public class Aufgabe5Test {
@Test
public void testEP2() {
/* //TODO: uncomment for testing
//test classes NamedBody and MassiveForceHashMap
// create 12 named bodies
NamedBody sun1, mercury1, venus1, earth1, moon1, mars1, deimos1, phobos1, vesta1, pallas1, hygiea1, ceres1;
// create a nameless body
Body earth2 = new Body(5.972E24, new Vector3(-6.13135922534815E10, -1.383789852227691E11, 2.719682263474911E7), new Vector3(26832.720535473603, -11948.23168764519, 1.9948243075997851));
Body earth2 = new Body(SolSystem4.EARTH);
// create the same 12 named body-force pairs
sun1 = new NamedBody("Sun", 1.989E30, new Vector3(0.0, 0.0, 0.0), new Vector3(0.0, 0.0, 0.0));
earth1 = new NamedBody("Earth", 5.972E24, new Vector3(-6.13135922534815E10, -1.383789852227691E11, 2.719682263474911E7), new Vector3(26832.720535473603, -11948.23168764519, 1.9948243075997851));
moon1 = new NamedBody("Moon", 7.349E22, new Vector3(-6.132484773775896E10, -1.387394951280871E11, 1.701046736294776E7), new Vector3(27916.62329282941, -12020.39526008238, -94.89703264508708));
mars1 = new NamedBody("Mars", 6.41712E23, new Vector3(-1.7923193702925848E11, 1.726665823982123E11, 7.991673845249474E9), new Vector3(-15925.78496403673, -15381.16179928219, 68.67560910598857));
deimos1 = new NamedBody("Deimos", 1.8E20, new Vector3(-1.792255010450533E11, 1.726891122683271E11, 7.990659337380297E9), new Vector3(-17100.476719804457, -15020.348656808, 631.2927851249581));
phobos1 = new NamedBody("Phobos", 1.08E20, new Vector3(-1.792253482539647E11, 1.72661109673625E11, 7.987848354800322E9), new Vector3(-14738.203714241401, -13671.17675223948, -411.0012490555253));
mercury1 = new NamedBody("Mercury", 3.301E23, new Vector3(-5.167375560011926E10, -4.217574885682655E10, 1.14808913958168E9), new Vector3(21580.25398577148, -34951.03632847389, -4835.225596525241));
venus1 = new NamedBody("Venus", 4.86747E24, new Vector3(-3.123150865740532E10, 1.0395568504115701E11, 3.173401325838074E9), new Vector3(-33748.180519629335, -10014.25141045021, 1809.94488874165));
vesta1 = new NamedBody("Vesta", 2.5908E20, new Vector3(-3.337493557929893E11, -4.7147908276077385E10, 4.1923010146878105E10), new Vector3(4440.54247538484, -19718.49074006637, 48.06573124543601));
pallas1 = new NamedBody("Pallas", 2.14E20, new Vector3(4.3452066613895575E11, -2.057319365171432E11, 1.0549957423213101E11), new Vector3(5058.947582097117, 11184.45711782372, -8183.524138259704));
hygiea1 = new NamedBody("Hygiea", 8.32E19, new Vector3(-3.983943433707043E11, 2.325833000024021E11, -2.233667695713672E10), new Vector3(-6931.864585548552, -15686.8108598699, -690.5791992347208));
ceres1 = new NamedBody("Ceres", 9.394E20, new Vector3(3.781372641419032E11, 1.96718960466285E11, -6.366459168068592E10), new Vector3(-8555.324226752316, 14718.33755980907, 2040.230135060142));
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);
NamedBody sun2 = new NamedBody("Sun", 1.9895E30, new Vector3(0.1, 0.0, 0.0), new Vector3(0.0, 0.0, 0.0));
NamedBody earth3 = new NamedBody("Earth", 1, new Vector3(0, 0, 0), new Vector3(0, 0, 0));
@@ -68,11 +66,31 @@ public class Aufgabe5Test {
assertFalse(map.containsKey(new Body(0, new Vector3(0, 0, 0), new Vector3(0, 0, 0))));
assertFalse(map.containsKey(new NamedBody("Omuamua", 0, new Vector3(0, 0, 0), new Vector3(0, 0, 0))));
int hashCode1 = map.hashCode();
Vector3 f = new Vector3(5, 5, 5);
map.put(earth3, f);
assertEquals(f, map.get(earth1));
assertNull(map.get(earth2));
*/ //TODO: uncomment
int hashCode2 = map.hashCode();
assertEquals(map, map);
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));
}
}

116
src/Aufgabe6Test.java Normal file
View File

@@ -0,0 +1,116 @@
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);
}
}

View File

@@ -3,7 +3,7 @@ import codedraw.CodeDraw;
/**
* This class represents celestial bodies like stars, planets, asteroids, etc...
*/
public class Body {
public class Body implements Massive {
private final double mass;
private Vector3 massCenter; // position of the mass center.
private Vector3 currentMovement;

View File

@@ -4,7 +4,7 @@
*/
public class BodyForceTreeMap {
private int size = 0;
private BodyForceTreeMapItem root = null;
private Item root = null;
/**
* Adds a new key-value association to this map. If the key already exists in this map,
@@ -13,30 +13,30 @@ public class BodyForceTreeMap {
*/
public Vector3 put(Body key, Vector3 value) {
if (root == null) {
root = new BodyForceTreeMapItem(key, value);
root = new Item(key, value);
size++;
return null;
}
BodyForceTreeMapItem item = root;
Item item = root;
while (item != null) {
if (item.key() == key) {
Vector3 old = item.value();
item.setValue(value);
if (item.key == 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 if (item.key.mass() > key.mass()) {
if (item.left != null) {
item = item.left;
} else {
item.setLeft(new BodyForceTreeMapItem(key, value));
item.setLeft(new Item(key, value));
size++;
break;
}
} else {
if (item.right() != null) {
item = item.right();
if (item.right != null) {
item = item.right;
} else{
item.setRight(new BodyForceTreeMapItem(key, value));
item.setRight(new Item(key, value));
size++;
break;
}
@@ -52,14 +52,14 @@ public class BodyForceTreeMap {
* Precondition: key != null.
*/
public Vector3 get(Body key) {
BodyForceTreeMapItem item = root;
Item item = root;
while (item != null) {
if (item.key() == key) {
return item.value();
} else if (item.key().mass() > key.mass()) {
item = item.left();
if (item.key == key) {
return item.value;
} else if (item.key.mass() > key.mass()) {
item = item.left;
} else {
item = item.right();
item = item.right;
}
}
return null;
@@ -69,14 +69,14 @@ public class BodyForceTreeMap {
* Returns 'true' if this map contains a mapping for the specified key.
*/
public boolean containsKey(Body key) {
BodyForceTreeMapItem item = root;
Item item = root;
while (item != null) {
if (item.key() == key) {
if (item.key == key) {
return true;
} else if (item.key().mass() > key.mass()) {
item = item.left();
} else if (item.key.mass() > key.mass()) {
item = item.left;
} else {
item = item.right();
item = item.right;
}
}
return false;
@@ -86,14 +86,14 @@ public class BodyForceTreeMap {
return this.size;
}
private String toString(BodyForceTreeMapItem item) {
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());
s += this.toString(item.right);
s += String.format("{%s: %s}\n", item.key, item.value);
s += this.toString(item.left);
return s;
}
@@ -103,53 +103,29 @@ public class BodyForceTreeMap {
*/
@Override
public String toString() {
return toString(root);
}
}
class BodyForceTreeMapItem {
private final Body key;
private Vector3 value;
private BodyForceTreeMapItem parent;
private BodyForceTreeMapItem left;
private BodyForceTreeMapItem right;
public BodyForceTreeMapItem(Body key, Vector3 value) {
this.key = key;
this.value = value;
}
public Body key() {
return this.key;
}
public void setValue(Vector3 value) {
this.value = value;
}
public Vector3 value() {
return this.value;
}
public BodyForceTreeMapItem left() {
return this.left;
}
public BodyForceTreeMapItem right() {
return this.right;
}
public BodyForceTreeMapItem parent() {
return this.parent;
}
public void setLeft(BodyForceTreeMapItem left) {
this.left = left;
if (left != null) left.parent = this;
}
public void setRight(BodyForceTreeMapItem right) {
this.right = right;
if (right != null) right.parent = this;
return (root != null) ? toString(root) : "";
}
private static class Item {
private final Body key;
private Vector3 value;
private Item parent;
private Item left;
private Item right;
public Item(Body 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;
}
}
}

View File

@@ -6,8 +6,8 @@ import java.util.Iterator;
*/
public class BodyLinkedList implements Iterable<Body> {
private int size = 0;
private BodyLinkedListItem first;
private BodyLinkedListItem last;
private Item first;
private Item last;
/**
* Initializes 'this' as an empty list.
@@ -35,11 +35,11 @@ public class BodyLinkedList implements Iterable<Body> {
*/
public void addFirst(Body body) {
if (first == null) {
first = new BodyLinkedListItem(body);
first = new Item(body);
last = first;
} else {
first.setPrev(new BodyLinkedListItem(body));
first = first.prev();
first.setPrev(new Item(body));
first = first.prev;
}
size++;
}
@@ -49,11 +49,11 @@ public class BodyLinkedList implements Iterable<Body> {
*/
public void addLast(Body body) {
if (last == null) {
last = new BodyLinkedListItem(body);
last = new Item(body);
first = last;
} else {
last.setNext(new BodyLinkedListItem(body));
last = last.next();
last.setNext(new Item(body));
last = last.next;
}
size++;
}
@@ -63,7 +63,7 @@ public class BodyLinkedList implements Iterable<Body> {
* Returns 'null' if the list is empty.
*/
public Body getLast() {
return (last != null) ? last.body() : null;
return (last != null) ? last.body : null;
}
/**
@@ -71,7 +71,7 @@ public class BodyLinkedList implements Iterable<Body> {
* Returns 'null' if the list is empty.
*/
public Body getFirst() {
return (first != null) ? first.body() : null;
return (first != null) ? first.body : null;
}
/**
@@ -82,8 +82,8 @@ public class BodyLinkedList implements Iterable<Body> {
if (first == null) {
return null;
}
Body b = first.body();
first = first.next();
Body b = first.body;
first = first.next;
if (first != null) first.setPrev(null);
size--;
return b;
@@ -97,8 +97,8 @@ public class BodyLinkedList implements Iterable<Body> {
if (last == null) {
return null;
}
Body b = last.body();
last = last.prev();
Body b = last.body;
last = last.prev;
if (last != null) last.setNext(null);
size--;
return b;
@@ -117,28 +117,28 @@ public class BodyLinkedList implements Iterable<Body> {
return;
}
BodyLinkedListItem item = first;
Item item = first;
for (int j = 0; j < i; j++) {
item = item.next();
item = item.next;
}
item.prev().setNext(new BodyLinkedListItem(body));
item.setPrev(item.prev().next());
item.prev.setNext(new Item(body));
item.setPrev(item.prev.next);
size++;
}
private Body removeItem(BodyLinkedListItem item) {
private Body removeItem(Item item) {
if (item == first) {
first = item.next();
first = item.next;
if (first != null) first.setPrev(null);
} else if (item == last) {
last = item.prev();
last = item.prev;
if (last != null) last.setNext(null);
} else {
item.next().setPrev(item.prev());
item.next.setPrev(item.prev);
}
size--;
return item.body();
return item.body;
}
/**
@@ -146,19 +146,19 @@ public class BodyLinkedList implements Iterable<Body> {
* Precondition: i >= 0 && i < size().
*/
public Body get(int i) {
BodyLinkedListItem item;
Item item;
if (i < size / 2) {
item = first;
for (int j = 0; j < i; j++) {
item = item.next();
item = item.next;
}
} else {
item = last;
for (int j = size - 1; j > i; j--) {
item = item.prev();
item = item.prev;
}
}
return item.body();
return item.body;
}
/**
@@ -170,12 +170,9 @@ public class BodyLinkedList implements Iterable<Body> {
return -1;
}
BodyLinkedListItem item = first;
for (int i = 0; i < size; i++) {
if (item.body() == body) {
return i;
}
item = item.next();
Item item = first;
for (int i = 0; i < size; i++, item = item.next) {
if (item.body == body) return i;
}
return -1;
@@ -187,8 +184,8 @@ public class BodyLinkedList implements Iterable<Body> {
*/
public BodyLinkedList removeCollidingWith(Body body) {
BodyLinkedList removed = new BodyLinkedList();
for (BodyLinkedListItem item = first; item != null; item = item.next()) {
if (body != item.body() && body.collidesWith(item.body())) {
for (Item item = first; item != null; item = item.next) {
if (body != item.body && body.collidesWith(item.body)) {
removed.addLast(this.removeItem(item));
}
}
@@ -205,57 +202,45 @@ public class BodyLinkedList implements Iterable<Body> {
@Override
public Iterator<Body> iterator() {
return new Iterator<>() {
BodyLinkedListItem ptr = first;
Item ptr = first;
boolean yieldedFirst = false;
@Override
public boolean hasNext() {
return ptr != null && (!yieldedFirst || ptr.next() != null);
return ptr != null && (!yieldedFirst || ptr.next != null);
}
@Override
public Body next() {
if (!yieldedFirst) {
yieldedFirst = true;
return ptr.body();
} else {
ptr = ptr.next;
}
ptr = ptr.next();
return ptr.body();
return ptr.body;
}
};
}
}
class BodyLinkedListItem {
private final Body body;
private BodyLinkedListItem prev;
private BodyLinkedListItem next;
private static class Item {
private final Body body;
private Item prev;
private Item next;
public BodyLinkedListItem(Body body) {
this.body = body;
this.prev = null;
this.next = null;
}
public Item(Body body) {
this.body = body;
this.prev = null;
this.next = null;
}
public Body body() {
return body;
}
public void setPrev(Item prev) {
this.prev = prev;
if (prev != null) prev.next = this;
}
public BodyLinkedListItem prev() {
return prev;
}
public void setPrev(BodyLinkedListItem prev) {
this.prev = prev;
if (prev != null) prev.next = this;
}
public BodyLinkedListItem next() {
return next;
}
public void setNext(BodyLinkedListItem next) {
this.next = next;
if (next != null) next.prev = this;
public void setNext(Item next) {
this.next = next;
if (next != null) next.prev = this;
}
}
}

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,70 @@ 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();
for (; i < all.length; i++) {
CosmicSystem sys = all[i];
if (sys instanceof NamedBodyForcePair m) {
i++;
return m.getBody();
} else if (sys instanceof HierarchicalSystem hs) {
cur = hs.iterator();
if (cur.hasNext()) {
i++;
return cur.next();
}
}
}
return null;
}
@Override
public boolean hasNext() {
if (cur != null && cur.hasNext()) return true;
for (; i < all.length; i++) {
CosmicSystem sys = all[i];
if (sys instanceof NamedBodyForcePair) {
return true;
} else if (sys instanceof HierarchicalSystem hs) {
cur = hs.iterator();
if (cur.hasNext()) {
i++;
return true;
}
}
}
return false;
}
};
}
}

View File

@@ -3,14 +3,21 @@
* exerted on the object). The number of key-value pairs is not limited.
*/
public class MassiveForceHashMap {
// TODO: define missing parts of this class.
private int size;
private Massive[] keys;
private Vector3[] values;
/**
* Initializes 'this' as an empty map.
*/
public MassiveForceHashMap() {
// TODO: implement constructor.
this(16);
}
public MassiveForceHashMap(int capacity) {
this.size = 0;
this.keys = new Massive[capacity];
this.values = new Vector3[capacity];
}
/**
@@ -19,8 +26,37 @@ public class MassiveForceHashMap {
* Precondition: key != null.
*/
public Vector3 put(Massive key, Vector3 value) {
// TODO: implement method.
return null;
if (size > keys.length / 2) doubleCapacity();
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 (values[pos] == null) {
keys[pos] = key;
values[pos] = value;
size++;
return null;
} else if (keys[pos].equals(key)) {
Vector3 old = values[pos];
values[pos] = value;
return old;
}
}
throw new RuntimeException();
}
private void doubleCapacity() {
Massive[] oldKeys = keys;
Vector3[] oldValues = values;
keys = new Massive[keys.length * 2];
values = new Vector3[values.length * 2];
size = 0;
for (int i = 0; i < oldKeys.length; i++) {
Massive k = oldKeys[i];
Vector3 v = oldValues[i];
if (v != null) put(k, v);
}
}
/**
@@ -29,25 +65,68 @@ public class MassiveForceHashMap {
* Precondition: key != null.
*/
public Vector3 get(Massive key) {
// TODO: implement method.
return null;
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 pos;
}
}
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;
}
/**
* Returns 'true' if this map contains a mapping for the specified key.
*/
public boolean containsKey(Massive key) {
// TODO: implement method.
return false;
return this.get(key) != null;
}
/**
* Returns a readable representation of this map, with all key-value pairs. Their order is not
* defined.
*/
@Override
public String toString() {
// TODO: implement method.
return "";
if (size == 0) return "{}";
StringBuilder sb = new StringBuilder("{");
for (int i = 0; i < keys.length; i++) {
Massive k = keys[i];
Vector3 v = values[i];
if (k != null && v != null) {
sb.append("\n ");
sb.append(k);
sb.append(": ");
sb.append(v);
sb.append(",");
}
}
sb.deleteCharAt(sb.length() - 1);
sb.append("\n}");
return sb.toString();
}
/**
@@ -57,24 +136,46 @@ public class MassiveForceHashMap {
* key-value pair in `this` equals one key-value pair in `o`. Two key-value pairs are
* equal if the two keys are equal and the two values are equal. Otherwise, `false` is returned.
*/
@Override
public boolean equals(Object o) {
// TODO: implement method.
return false;
if (!(o instanceof MassiveForceHashMap map) || map.size != size)
return false;
for (int i = 0; i < keys.length; i++) {
Massive k1 = keys[i];
Vector3 v1 = values[i];
if (k1 == null || v1 == null) continue;
Vector3 v2 = map.get(k1);
if (!v1.equals(v2)) return false;
}
return true;
}
/**
* Returns the hashCode of `this`.
*/
@Override
public int hashCode() {
//TODO: implement method.
return 0;
int hash = 0;
for (int i = 0; i < keys.length; i++) {
Massive k = keys[i];
Vector3 v = values[i];
if (k == null || v == null) continue;
hash ^= k.hashCode();
hash ^= v.hashCode();
}
return hash;
}
/**
* Returns a list of all the keys in no specified order.
*/
public MassiveLinkedList keyList() {
// TODO: implement method.
return null;
MassiveLinkedList list = new MassiveLinkedList();
for (int i = 0; i < keys.length; i++) {
if (keys[i] != null && values[i] != null)
list.addLast(keys[i]);
}
return list;
}
}

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

@@ -1,16 +1,20 @@
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 {
//TODO: declare variables.
public class MassiveLinkedList implements Iterable<Massive> {
private int size = 0;
private Item first;
private Item last;
/**
* Initializes 'this' as an empty list.
*/
public MassiveLinkedList() {
//TODO: define constructor.
first = null;
last = null;
}
/**
@@ -20,21 +24,38 @@ public class MassiveLinkedList {
* Precondition: list != null.
*/
public MassiveLinkedList(BodyLinkedList list) {
//TODO: define constructor.
this.size = 0;
for (Body b : list) {
this.addLast(b);
}
}
/**
* Inserts the specified element 'body' at the beginning of this list.
*/
public void addFirst(Massive body) {
//TODO: implement method.
if (first == null) {
first = new Item(body);
last = first;
} else {
first.setPrev(new Item(body));
first = first.prev;
}
size++;
}
/**
* Appends the specified element 'body' to the end of this list.
*/
public void addLast(Massive body) {
//TODO: implement method.
if (last == null) {
last = new Item(body);
first = last;
} else {
last.setNext(new Item(body));
last = last.next;
}
size++;
}
/**
@@ -42,8 +63,7 @@ public class MassiveLinkedList {
* Returns 'null' if the list is empty.
*/
public Massive getLast() {
//TODO: implement method.
return null;
return (last != null) ? last.body : null;
}
/**
@@ -51,8 +71,7 @@ public class MassiveLinkedList {
* Returns 'null' if the list is empty.
*/
public Massive getFirst() {
//TODO: implement method.
return null;
return (first != null) ? first.body : null;
}
/**
@@ -60,8 +79,14 @@ public class MassiveLinkedList {
* Returns 'null' if the list is empty.
*/
public Massive pollFirst() {
//TODO: implement method.
return null;
if (first == null) {
return null;
}
Massive m = first.body;
first = first.next;
if (first != null) first.setPrev(null);
size--;
return m;
}
/**
@@ -69,8 +94,14 @@ public class MassiveLinkedList {
* Returns 'null' if the list is empty.
*/
public Massive pollLast() {
//TODO: implement method.
return null;
if (last == null) {
return null;
}
Massive m = last.body;
last = last.prev;
if (last != null) last.setNext(null);
size--;
return m;
}
/**
@@ -78,7 +109,22 @@ public class MassiveLinkedList {
* Precondition: i >= 0 && i <= size().
*/
public void add(int i, Massive m) {
//TODO: implement method.
if (first == null || i == 0) {
addFirst(m);
return;
} else if (i == size) {
addLast(m);
return;
}
Item item = first;
for (int j = 0; j < i; j++) {
item = item.next;
}
item.prev.setNext(new Item(m));
item.setPrev(item.prev.next);
size++;
}
/**
@@ -86,8 +132,19 @@ public class MassiveLinkedList {
* Precondition: i >= 0 && i < size().
*/
public Massive get(int i) {
//TODO: implement method.
return null;
Item item;
if (i < size / 2) {
item = first;
for (int j = 0; j < i; j++) {
item = item.next;
}
} else {
item = last;
for (int j = size - 1; j > i; j--) {
item = item.prev;
}
}
return item.body;
}
/**
@@ -95,15 +152,67 @@ public class MassiveLinkedList {
* this list does not contain the element.
*/
public int indexOf(Massive m) {
//TODO: implement method.
return -2;
if (first == null) {
return -1;
}
Item item = first;
for (int i = 0; i < size; i++, item = item.next) {
if (item.body.equals(m)) return i;
}
return -1;
}
/**
* Returns the number of elements in this list.
*/
public int size() {
//TODO: implement method.
return -1;
return size;
}
@Override
public Iterator<Massive> iterator() {
return new Iterator<>() {
Item ptr = first;
boolean yieldedFirst = false;
@Override
public boolean hasNext() {
return ptr != null && (!yieldedFirst || ptr.next != null);
}
@Override
public Massive next() {
if (!yieldedFirst) {
yieldedFirst = true;
} else {
ptr = ptr.next;
}
return ptr.body;
}
};
}
private static class Item {
private final Massive body;
private Item prev;
private Item next;
public Item(Massive body) {
this.body = body;
this.prev = null;
this.next = null;
}
public void setPrev(Item prev) {
this.prev = prev;
if (prev != null) prev.next = this;
}
public void setNext(Item next) {
this.next = next;
if (next != null) next.prev = this;
}
}
}

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

View File

@@ -1,46 +1,79 @@
public class NamedBody /* TODO: add clause(s) */ {
import codedraw.CodeDraw;
// TODO: add missing parts of this class.
public class NamedBody implements Massive {
private final String name;
private final Body body;
/**
* Initializes this with name, mass, current position and movement. The associated force
* is initialized with a zero vector.
* Initializes this with name, mass, current position and movement.
*/
public NamedBody(String name, double mass, Vector3 massCenter, Vector3 currentMovement) {
// TODO: implement constructor.
this(name, new Body(mass, massCenter, currentMovement));
}
public NamedBody(String name, Body body) {
this.name = name;
this.body = body;
}
public NamedBody(NamedBody other) {
this(other.name, new Body(other.body));
}
/**
* Returns the name of the body.
*/
public String getName() {
// TODO: implement method.
return "";
return name;
}
public Body getBody() {
return body;
}
public Vector3 getMassCenter() {
return body.getMassCenter();
}
public double getMass() {
return body.getMass();
}
/**
* Compares `this` with the specified object. Returns `true` if the specified `o` is not
* `null` and is of type `NamedBody` and both `this` and `o` have equal names.
* Otherwise `false` is returned.
* Otherwise, `false` is returned.
*/
@Override
public boolean equals(Object o) {
//TODO: implement method.
return false;
if (!(o instanceof NamedBody b)) return false;
return this.name.equals(b.name);
}
/**
* Returns the hashCode of `this`.
*/
@Override
public int hashCode() {
//TODO: implement method.
return 0;
return this.name.hashCode();
}
/**
* Returns a readable representation including the name of this body.
*/
@Override
public String toString() {
//TODO: implement method.
return "";
return this.getName();
}
@Override
public void move(Vector3 force) {
body.move(force);
}
@Override
public void draw(CodeDraw cd) {
body.draw(cd);
}
}

View File

@@ -6,8 +6,7 @@ import codedraw.CodeDraw;
*/
public class NamedBodyForcePair implements CosmicSystem {
private final String name;
private final Body body;
private final NamedBody body;
private final Vector3 force = new Vector3();
/**
@@ -15,27 +14,30 @@ public class NamedBodyForcePair implements CosmicSystem {
* is initialized with a zero vector.
*/
public NamedBodyForcePair(String name, double mass, Vector3 massCenter, Vector3 currentMovement) {
this(name, new Body(mass, massCenter, currentMovement));
this(new NamedBody(name, mass, massCenter, currentMovement));
}
public NamedBodyForcePair(String name, Body b) {
this.body = b;
this.name = name;
public NamedBodyForcePair(String name, Body body) {
this(new NamedBody(name, body));
}
public NamedBodyForcePair(NamedBody body) {
this.body = body;
}
public NamedBodyForcePair(NamedBodyForcePair other) {
this(other.name, new Body(other.body));
this(new NamedBody(other.body));
}
public Body getBody() {
return body;
return body.getBody();
}
/**
* Returns the name of the body.
*/
public String getName() {
return name;
return body.getName();
}
@Override
@@ -70,13 +72,13 @@ public class NamedBodyForcePair implements CosmicSystem {
@Override
public void addForceTo(CosmicSystem cs) {
cs.addForceFrom(body);
cs.addForceFrom(body.getBody());
}
@Override
public BodyLinkedList getBodies() {
BodyLinkedList list = new BodyLinkedList();
list.addFirst(body);
list.addFirst(body.getBody());
return list;
}

View File

@@ -35,23 +35,22 @@ public class Simulation5 {
* 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("Sun", 1.989E30, new Vector3(0.0, 0.0, 0.0), new Vector3(0.0, 0.0, 0.0));
NamedBody earth = new NamedBody("Earth", 5.972E24, new Vector3(-6.13135922534815E10, -1.383789852227691E11, 2.719682263474911E7), new Vector3(26832.720535473603, -11948.23168764519, 1.9948243075997851));
NamedBody moon = new NamedBody("Moon", 7.349E22, new Vector3(-6.132484773775896E10, -1.387394951280871E11, 1.701046736294776E7), new Vector3(27916.62329282941, -12020.39526008238, -94.89703264508708));
NamedBody mars = new NamedBody("Mars", 6.41712E23, new Vector3(-1.7923193702925848E11, 1.726665823982123E11, 7.991673845249474E9), new Vector3(-15925.78496403673, -15381.16179928219, 68.67560910598857));
NamedBody deimos = new NamedBody("Deimos", 1.8E20, new Vector3(-1.792255010450533E11, 1.726891122683271E11, 7.990659337380297E9), new Vector3(-17100.476719804457, -15020.348656808, 631.2927851249581));
NamedBody phobos = new NamedBody("Phobos", 1.08E20, new Vector3(-1.792253482539647E11, 1.72661109673625E11, 7.987848354800322E9), new Vector3(-14738.203714241401, -13671.17675223948, -411.0012490555253));
NamedBody mercury = new NamedBody("Mercury", 3.301E23, new Vector3(-5.167375560011926E10, -4.217574885682655E10, 1.14808913958168E9), new Vector3(21580.25398577148, -34951.03632847389, -4835.225596525241));
NamedBody venus = new NamedBody("Venus", 4.86747E24, new Vector3(-3.123150865740532E10, 1.0395568504115701E11, 3.173401325838074E9), new Vector3(-33748.180519629335, -10014.25141045021, 1809.94488874165));
NamedBody vesta = new NamedBody("Vesta", 2.5908E20, new Vector3(-3.337493557929893E11, -4.7147908276077385E10, 4.1923010146878105E10), new Vector3(4440.54247538484, -19718.49074006637, 48.06573124543601));
NamedBody pallas = new NamedBody("Pallas", 2.14E20, new Vector3(4.3452066613895575E11, -2.057319365171432E11, 1.0549957423213101E11), new Vector3(5058.947582097117, 11184.45711782372, -8183.524138259704));
NamedBody hygiea = new NamedBody("Hygiea", 8.32E19, new Vector3(-3.983943433707043E11, 2.325833000024021E11, -2.233667695713672E10), new Vector3(-6931.864585548552, -15686.8108598699, -690.5791992347208));
NamedBody ceres = new NamedBody("Ceres", 9.394E20, new Vector3(3.781372641419032E11, 1.96718960466285E11, -6.366459168068592E10), new Vector3(-8555.324226752316, 14718.33755980907, 2040.230135060142));
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];
@@ -59,12 +58,56 @@ public class Simulation5 {
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,
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));
new Vector3(0 + random.nextGaussian() * 5e3, 0 + random.nextGaussian() * 5e3, 0 + random.nextGaussian() * 5e3)
);
}
//TODO: implementation of this method according to 'Aufgabenblatt5.md'.
// Add both, NamedBody- and Body-objects, to your simulation.
MassiveForceHashMap forceOnBody = new MassiveForceHashMap();
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.keyList()) {
Vector3 force = new Vector3();
for (Massive b2 : forceOnBody.keyList()) {
if (b1 != b2) {
force = force.plus(b1.gravitationalForce(b2));
}
}
forceOnBody.put(b1, force);
}
for (Massive body : forceOnBody.keyList()) {
body.move(forceOnBody.get(body));
}
if ((seconds % 3600) == 0) {
cd.clear(Color.BLACK);
for (Massive body : forceOnBody.keyList()) {
body.draw(cd);
}
cd.show();
}
}
}
}

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

View File

@@ -12,16 +12,16 @@ public class SolSystem4 {
public static final Body HYGIEA = new Body(8.32E19, new Vector3(-3.983943433707043E11, 2.325833000024021E11, -2.233667695713672E10), new Vector3(-6931.864585548552, -15686.8108598699, -690.5791992347208));
public static final Body CERES = new Body(9.394E20, new Vector3(3.781372641419032E11, 1.96718960466285E11, -6.366459168068592E10), new Vector3(-8555.324226752316, 14718.33755980907, 2040.230135060142));
public static final NamedBodyForcePair SUN_NAMED = new NamedBodyForcePair("Sun", SUN);
public static final NamedBodyForcePair EARTH_NAMED = new NamedBodyForcePair("Earth", EARTH);
public static final NamedBodyForcePair MOON_NAMED = new NamedBodyForcePair("Moon", MOON);
public static final NamedBodyForcePair MARS_NAMED = new NamedBodyForcePair("Mars", MARS);
public static final NamedBodyForcePair DEIMOS_NAMED = new NamedBodyForcePair("Deimos", DEIMOS);
public static final NamedBodyForcePair PHOBOS_NAMED = new NamedBodyForcePair("Phobos", PHOBOS);
public static final NamedBodyForcePair MERCURY_NAMED = new NamedBodyForcePair("Mercury", MERCURY);
public static final NamedBodyForcePair VENUS_NAMED = new NamedBodyForcePair("Venus", VENUS);
public static final NamedBodyForcePair VESTA_NAMED = new NamedBodyForcePair("Vesta", VESTA);
public static final NamedBodyForcePair PALLAS_NAMED = new NamedBodyForcePair("Pallas", PALLAS);
public static final NamedBodyForcePair HYGIEA_NAMED = new NamedBodyForcePair("Hygiea", HYGIEA);
public static final NamedBodyForcePair CERES_NAMED = new NamedBodyForcePair("Ceres", CERES);
public static final NamedBody SUN_NAMED = new NamedBody("Sun", SUN);
public static final NamedBody EARTH_NAMED = new NamedBody("Earth", EARTH);
public static final NamedBody MOON_NAMED = new NamedBody("Moon", MOON);
public static final NamedBody MARS_NAMED = new NamedBody("Mars", MARS);
public static final NamedBody DEIMOS_NAMED = new NamedBody("Deimos", DEIMOS);
public static final NamedBody PHOBOS_NAMED = new NamedBody("Phobos", PHOBOS);
public static final NamedBody MERCURY_NAMED = new NamedBody("Mercury", MERCURY);
public static final NamedBody VENUS_NAMED = new NamedBody("Venus", VENUS);
public static final NamedBody VESTA_NAMED = new NamedBody("Vesta", VESTA);
public static final NamedBody PALLAS_NAMED = new NamedBody("Pallas", PALLAS);
public static final NamedBody HYGIEA_NAMED = new NamedBody("Hygiea", HYGIEA);
public static final NamedBody CERES_NAMED = new NamedBody("Ceres", CERES);
}