6 Commits

Author SHA1 Message Date
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
e7eae474ac Refactor MassiveForceHashMap 2022-05-06 22:45:56 +02:00
6b1f1ecc2a Refactored for AB5 2022-05-06 22:42:28 +02:00
13 changed files with 677 additions and 443 deletions

View File

@@ -19,7 +19,7 @@ Ziel der Aufgabe ist die Anwendung der Konzepte: Gleichheit und Hash-Werte, Hash
- [Massive](../src/Massive.java) ist ein Interface, das Himmelskörper (als kohärente Massen) - [Massive](../src/Massive.java) ist ein Interface, das Himmelskörper (als kohärente Massen)
beschreibt. `Massive` ist der gemeinsame Obertyp für verschiedene Klassen von Himmelkörpern. Die beschreibt. `Massive` ist der gemeinsame Obertyp für verschiedene Klassen von Himmelkörpern. Die
meisten spezifizierten Methoden sind mit einer `default`-Implementierung definiert. Dieser meisten spezifizierten Methoden sind mit einer `default`-Implementierung definiert. Dieser
Programcode wird ausgeführt, falls die entsprechende Klasse (`Body` oder `NamedBody`) über keine Programmcode wird ausgeführt, falls die entsprechende Klasse (`Body` oder `NamedBody`) über keine
eigene Definition der Methode verfügt. Verändern Sie diese Datei bitte nicht. eigene Definition der Methode verfügt. Verändern Sie diese Datei bitte nicht.
- [NamedBody](../src/NamedBody.java) ist das Gerüst einer Klassendefinition. Die Klasse - [NamedBody](../src/NamedBody.java) ist das Gerüst einer Klassendefinition. Die Klasse
repräsentiert Himmelskörper, die einen Namen haben. repräsentiert Himmelskörper, die einen Namen haben.
@@ -100,8 +100,3 @@ Ihre Aufgaben sind folgende:
- Implementierung von `Simulation5`: 1 Punkte - Implementierung von `Simulation5`: 1 Punkte
- Gesamt: 5 Punkte - Gesamt: 5 Punkte

View File

@@ -1,125 +1,81 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class Aufgabe5Test { public class Aufgabe5Test {
public static void main(String[] args) { @Test
public void testEP2() {
/* //TODO: uncomment for testing
//test classes NamedBody and MassiveForceHashMap //test classes NamedBody and MassiveForceHashMap
// create 12 named bodies // create 12 named bodies
NamedBody sun1, mercury1, venus1, earth1, moon1, mars1, deimos1, phobos1, vesta1, NamedBody sun1, mercury1, venus1, earth1, moon1, mars1, deimos1, phobos1, vesta1, pallas1, hygiea1, ceres1;
pallas1, hygiea1, ceres1;
// create a nameless body // create a nameless body
Body earth2 = new Body(5.972E24, new Vector3(-6.13135922534815E10,-1.383789852227691E11, Body earth2 = new Body(SolSystem4.EARTH);
2.719682263474911E7), new Vector3(26832.720535473603,-11948.23168764519,1.9948243075997851));
// create the same 12 named body-force pairs // create the same 12 named body-force pairs
sun1 = new NamedBody("Sun",1.989E30, new Vector3(0.0,0.0,0.0) sun1 = new NamedBody(SolSystem4.SUN_NAMED);
, new Vector3(0.0,0.0,0.0)); earth1 = new NamedBody(SolSystem4.EARTH_NAMED);
earth1 = new NamedBody("Earth",5.972E24, moon1 = new NamedBody(SolSystem4.MOON_NAMED);
new Vector3(-6.13135922534815E10,-1.383789852227691E11,2.719682263474911E7), new Vector3(26832.720535473603,-11948.23168764519,1.9948243075997851)); mars1 = new NamedBody(SolSystem4.MARS_NAMED);
moon1 = new NamedBody("Moon",7.349E22, deimos1 = new NamedBody(SolSystem4.DEIMOS_NAMED);
new Vector3(-6.132484773775896E10,-1.387394951280871E11,1.701046736294776E7), new Vector3(27916.62329282941,-12020.39526008238,-94.89703264508708)); phobos1 = new NamedBody(SolSystem4.PHOBOS_NAMED);
mars1 = new NamedBody("Mars",6.41712E23, mercury1 = new NamedBody(SolSystem4.MERCURY_NAMED);
new Vector3(-1.7923193702925848E11,1.726665823982123E11,7.991673845249474E9), new Vector3(-15925.78496403673,-15381.16179928219,68.67560910598857)); venus1 = new NamedBody(SolSystem4.VENUS_NAMED);
deimos1 = new NamedBody("Deimos",1.8E20, vesta1 = new NamedBody(SolSystem4.VESTA_NAMED);
new Vector3(-1.792255010450533E11,1.726891122683271E11,7.990659337380297E9), new Vector3(-17100.476719804457,-15020.348656808,631.2927851249581)); pallas1 = new NamedBody(SolSystem4.PALLAS_NAMED);
phobos1 = new NamedBody("Phobos",1.08E20, hygiea1 = new NamedBody(SolSystem4.HYGIEA_NAMED);
new Vector3(-1.792253482539647E11,1.72661109673625E11,7.987848354800322E9), new Vector3(-14738.203714241401,-13671.17675223948,-411.0012490555253)); ceres1 = new NamedBody(SolSystem4.CERES_NAMED);
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));
System.out.println("Test1:"); NamedBody sun2 = new NamedBody("Sun", 1.9895E30, new Vector3(0.1, 0.0, 0.0), new Vector3(0.0, 0.0, 0.0));
NamedBody sun2 = new NamedBody("Sun",1.9895E30, new Vector3(0.1,0.0,0.0) NamedBody earth3 = new NamedBody("Earth", 1, new Vector3(0, 0, 0), new Vector3(0, 0, 0));
, new Vector3(0.0,0.0,0.0)); assertEquals(sun1, sun2);
NamedBody earth3 = new NamedBody("Earth", 1, new Vector3(0,0,0), new Vector3(0,0,0)); assertEquals(sun2.hashCode(), sun1.hashCode());
testValue(sun1.equals(sun2), true); assertEquals(earth1, earth3);
testValue(sun1.hashCode(), sun2.hashCode()); assertEquals(earth3.hashCode(), earth1.hashCode());
testValue(earth1.equals(earth3), true);
testValue(earth1.hashCode(), earth3.hashCode());
// check basic functions of 'MassiveForceHashMap' // check basic functions of 'MassiveForceHashMap'
System.out.println("Test2:");
MassiveForceHashMap map = new MassiveForceHashMap(); MassiveForceHashMap map = new MassiveForceHashMap();
map.put(sun1, new Vector3(0,0,0)); map.put(sun1, new Vector3(0, 0, 0));
map.put(mercury1, new Vector3(0,0,0)); map.put(mercury1, new Vector3(0, 0, 0));
map.put(venus1, new Vector3(0,0,0)); map.put(venus1, new Vector3(0, 0, 0));
map.put(earth1, new Vector3(0,0,0)); map.put(earth1, new Vector3(0, 0, 0));
map.put(moon1, new Vector3(0,0,0)); map.put(moon1, new Vector3(0, 0, 0));
map.put(mars1, new Vector3(0,0,0)); map.put(mars1, new Vector3(0, 0, 0));
map.put(deimos1, new Vector3(0,0,0)); map.put(deimos1, new Vector3(0, 0, 0));
map.put(phobos1, new Vector3(0,0,0)); map.put(phobos1, new Vector3(0, 0, 0));
map.put(vesta1, new Vector3(0,0,0)); map.put(vesta1, new Vector3(0, 0, 0));
map.put(pallas1, new Vector3(0,0,0)); map.put(pallas1, new Vector3(0, 0, 0));
map.put(hygiea1, new Vector3(0,0,0)); map.put(hygiea1, new Vector3(0, 0, 0));
map.put(ceres1, new Vector3(0,0,0)); map.put(ceres1, new Vector3(0, 0, 0));
map.put(mars1, new Vector3(0,0,0)); // inserted twice map.put(mars1, new Vector3(0, 0, 0)); // inserted twice
testValue(map.keyList().size(), 12); assertEquals(12, map.keyList().size());
System.out.println("Test3:"); assertTrue(map.toString().contains("Mars"));
testValue(map.toString().contains("Mars"), true); assertTrue(map.toString().contains("Deimos"));
testValue(map.toString().contains("Deimos"), true); assertTrue(map.toString().contains("Moon"));
testValue(map.toString().contains("Moon"), true); assertTrue(map.toString().contains("Earth"));
testValue(map.toString().contains("Earth"), true);
System.out.println("Test4:");
MassiveLinkedList bl = map.keyList(); MassiveLinkedList bl = map.keyList();
boolean allThere = true; boolean allThere = true;
while (bl.size() > 0) { while (bl.size() > 0) {
allThere &= map.containsKey(bl.pollFirst()); allThere &= map.containsKey(bl.pollFirst());
} }
testValue(allThere, true); assertTrue(allThere);
testValue(map.containsKey(new Body(0,new Vector3(0,0,0), new Vector3(0,0,0))), assertFalse(map.containsKey(new Body(0, new Vector3(0, 0, 0), new Vector3(0, 0, 0))));
false); assertFalse(map.containsKey(new NamedBody("Omuamua", 0, new Vector3(0, 0, 0), new Vector3(0, 0, 0))));
testValue(map.containsKey(new NamedBody("Omuamua",0,new Vector3(0,0,0), new Vector3(0,0,
0))),
false);
System.out.println("Test5:"); int hashCode1 = map.hashCode();
Vector3 f = new Vector3(5,5,5);
Vector3 f = new Vector3(5, 5, 5);
map.put(earth3, f); map.put(earth3, f);
testValue(map.get(earth1), f); assertEquals(f, map.get(earth1));
testValue(map.get(earth2), null); assertNull(map.get(earth2));
*/ //TODO: uncomment int hashCode2 = map.hashCode();
assertEquals(map, map);
assertEquals(hashCode2, map.hashCode());
assertNotEquals(hashCode1, hashCode2);
} }
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

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

View File

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

View File

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

View File

@@ -8,8 +8,8 @@ import codedraw.CodeDraw;
public class HierarchicalSystem implements CosmicSystem { public class HierarchicalSystem implements CosmicSystem {
private final NamedBodyForcePair central; private final NamedBodyForcePair central;
private final CosmicSystem[] orbit; private CosmicSystem[] orbit;
private final CosmicSystem[] all; private CosmicSystem[] all;
/** /**
* Initializes this system with a name and a central body. * Initializes this system with a name and a central body.
@@ -110,4 +110,21 @@ public class HierarchicalSystem implements CosmicSystem {
return sb.toString(); 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;
}
} }

View File

@@ -1,64 +1,73 @@
// Represents a coherent mass with a mass center in 3D space. Has two naming schemes for its /**
// methods. Please, do not change this interface definition! * Represents a coherent mass with a mass center in 3D space. Has two naming schemes for its
// * methods. Please, do not change this interface definition!
*/
public interface Massive extends Drawable { public interface Massive extends Drawable {
// Returns the mass. /**
* Returns the mass.
*/
default double mass() { default double mass() {
return getMass(); return getMass();
} }
// Returns the mass center. /**
* Returns the mass center.
*/
default Vector3 massCenter() { default Vector3 massCenter() {
return getMassCenter(); return getMassCenter();
} }
// Returns the mass. /**
* Returns the mass.
*/
default double getMass() { default double getMass() {
return mass(); return mass();
} }
// Returns the mass center. /**
* Returns the mass center.
*/
default Vector3 getMassCenter() { default Vector3 getMassCenter() {
return massCenter(); return massCenter();
} }
// Returns the approximate radius of 'this', assuming it is a coherent round mass. /**
// (It is assumed that the radius r is related to the mass m by r = m ^ 0.5, * Returns the approximate radius of 'this', assuming it is a coherent round mass.
// where m and r measured in solar units.) * (It is assumed that the radius r is related to the mass m by r = m ^ 0.5,
* where m and r measured in solar units.)
*/
default double getRadius() { default double getRadius() {
return radius(); return radius();
} }
// Returns the approximate radius of 'this', assuming it is a coherent round mass. /**
// (It is assumed that the radius r is related to the mass m by r = m ^ 0.5, * Returns the approximate radius of 'this', assuming it is a coherent round mass.
// where m and r measured in solar units.) * (It is assumed that the radius r is related to the mass m by r = m ^ 0.5,
* where m and r measured in solar units.)
*/
default double radius() { default double radius() {
return SpaceDraw.massToRadius(mass()); return SpaceDraw.massToRadius(mass());
} }
// Returns a vector representing the gravitational force exerted by 'b' on this mass. /**
// The gravitational Force F is calculated by F = G*(m1*m2)/(r*r), with m1 and m2 being the * Returns a vector representing the gravitational force exerted by 'b' on this mass.
// masses of the objects interacting, r being the distance between the centers of the masses * The gravitational Force F is calculated by F = G*(m1*m2)/(r*r), with m1 and m2 being the
// and G being the gravitational constant. * masses of the objects interacting, r being the distance between the centers of the masses
* and G being the gravitational constant.
*/
default Vector3 gravitationalForce(Massive b) { default Vector3 gravitationalForce(Massive b) {
Vector3 direction = b.massCenter().minus(this.massCenter()); Vector3 direction = b.massCenter().minus(this.massCenter());
double distance = direction.length(); double distance = direction.length();
direction.normalize(); direction.normalize();
double force = Simulation.G*this.mass()*b.mass()/(distance * distance); double force = Simulation.G * this.mass() * b.mass() / (distance * distance);
return direction.times(force); return direction.times(force);
} }
// Centers this mass at a new position, according to the specified force vector 'force' exerted /**
// on it, and updates the current velocity vector accordingly. * Centers this mass at a new position, according to the specified force vector 'force' exerted
// (Velocity depends on the mass of 'this', its current velocity and the exerted force.) * on it, and updates the current velocity vector accordingly.
* (Velocity depends on the mass of 'this', its current velocity and the exerted force.)
*/
void move(Vector3 force); void move(Vector3 force);
} }

View File

@@ -1,72 +1,161 @@
// A hash map that associates a 'Massive'-object with a Vector3 (typically this is the force /**
// exerted on the object). The number of key-value pairs is not limited. * A hash map that associates a 'Massive'-object with a Vector3 (typically this is the force
// * exerted on the object). The number of key-value pairs is not limited.
*/
public class MassiveForceHashMap { public class MassiveForceHashMap {
private int size;
private Massive[] keys;
private Vector3[] values;
// TODO: define missing parts of this class. /**
* Initializes 'this' as an empty map.
// Initializes 'this' as an empty map. */
public MassiveForceHashMap() { public MassiveForceHashMap() {
this(16);
// TODO: implement constructor.
} }
// Adds a new key-value association to this map. If the key already exists in this map, public MassiveForceHashMap(int capacity) {
// the value is replaced and the old value is returned. Otherwise 'null' is returned. this.size = 0;
// Precondition: key != null. this.keys = new Massive[capacity];
this.values = new Vector3[capacity];
}
/**
* 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) { public Vector3 put(Massive key, Vector3 value) {
if (size > keys.length / 2) doubleCapacity();
// TODO: implement method. int idx = ((key.hashCode() % keys.length) + keys.length) % keys.length;
return null; 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();
} }
// Returns the value associated with the specified key, i.e. the method returns the force vector private void doubleCapacity() {
// associated with the specified key. Returns 'null' if the key is not contained in this map. Massive[] oldKeys = keys;
// Precondition: key != null. 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);
}
}
/**
* 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) { public Vector3 get(Massive key) {
int idx = ((key.hashCode() % keys.length) + keys.length) % keys.length;
// TODO: implement method. 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 null; return null;
} }
// Returns 'true' if this map contains a mapping for the specified key. /**
* Returns 'true' if this map contains a mapping for the specified key.
*/
public boolean containsKey(Massive key) { public boolean containsKey(Massive key) {
return this.get(key) != null;
// TODO: implement method.
return false;
} }
// Returns a readable representation of this map, with all key-value pairs. Their order is not /**
// defined. * Returns a readable representation of this map, with all key-value pairs. Their order is not
* defined.
*/
@Override
public String toString() { public String toString() {
if (size == 0) return "{}";
// TODO: implement method. StringBuilder sb = new StringBuilder("{");
return ""; 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();
} }
// Compares `this` with the specified object for equality. Returns `true` if the specified /**
// `o` is not `null` and is of type `MassiveForceHashMap` and both `this` and `o` have equal * Compares `this` with the specified object for equality. Returns `true` if the specified
// key-value pairs, i.e. the number of key-value pairs is the same in both maps and every * `o` is not `null` and is of type `MassiveForceHashMap` and both `this` and `o` have equal
// key-value pair in `this` equals one key-value pair in `o`. Two key-value pairs are * key-value pairs, i.e. the number of key-value pairs is the same in both maps and every
// equal if the two keys are equal and the two values are equal. Otherwise `false` is returned. * 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) { public boolean equals(Object o) {
if (!(o instanceof MassiveForceHashMap map) || map.size != size)
return false;
// TODO: implement method. for (int i = 0; i < keys.length; i++) {
return false; 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`. /**
* Returns the hashCode of `this`.
*/
@Override
public int hashCode() { public int hashCode() {
int hash = 0;
//TODO: implement method. for (int i = 0; i < keys.length; i++) {
return 0; 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. /**
* Returns a list of all the keys in no specified order.
*/
public MassiveLinkedList keyList() { public MassiveLinkedList keyList() {
MassiveLinkedList list = new MassiveLinkedList();
// TODO: implement method. for (int i = 0; i < keys.length; i++) {
return null; if (keys[i] != null && values[i] != null)
list.addLast(keys[i]);
}
return list;
} }
}
}

View File

@@ -1,95 +1,218 @@
// A list of massive objects implemented as a linked list. import java.util.Iterator;
// The number of elements of the list is not limited.
public class MassiveLinkedList {
//TODO: declare variables. /**
* 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> {
private int size = 0;
private Item first;
private Item last;
// Initializes 'this' as an empty list. /**
* Initializes 'this' as an empty list.
*/
public MassiveLinkedList() { public MassiveLinkedList() {
first = null;
//TODO: define constructor. last = null;
} }
// Initializes 'this' as an independent copy of the specified list 'list'. /**
// Calling methods of this list will not affect the specified list 'list' * Initializes 'this' as an independent copy of the specified list 'list'.
// and vice versa. * Calling methods of this list will not affect the specified list 'list'
// Precondition: list != null. * and vice versa.
* Precondition: list != null.
*/
public MassiveLinkedList(BodyLinkedList list) { public MassiveLinkedList(BodyLinkedList list) {
this.size = 0;
//TODO: define constructor. for (Body b : list) {
this.addLast(b);
}
} }
// Inserts the specified element 'body' at the beginning of this list. /**
* Inserts the specified element 'body' at the beginning of this list.
*/
public void addFirst(Massive body) { public void addFirst(Massive body) {
if (first == null) {
//TODO: implement method. 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. /**
* Appends the specified element 'body' to the end of this list.
*/
public void addLast(Massive body) { public void addLast(Massive body) {
if (last == null) {
//TODO: implement method. last = new Item(body);
first = last;
} else {
last.setNext(new Item(body));
last = last.next;
}
size++;
} }
// Returns the last element in this list. /**
// Returns 'null' if the list is empty. * Returns the last element in this list.
* Returns 'null' if the list is empty.
*/
public Massive getLast() { public Massive getLast() {
return (last != null) ? last.body : null;
//TODO: implement method.
return null;
} }
// Returns the first element in this list. /**
// Returns 'null' if the list is empty. * Returns the first element in this list.
* Returns 'null' if the list is empty.
*/
public Massive getFirst() { public Massive getFirst() {
return (first != null) ? first.body : null;
//TODO: implement method.
return null;
} }
// Retrieves and removes the first element in this list. /**
// Returns 'null' if the list is empty. * Retrieves and removes the first element in this list.
* Returns 'null' if the list is empty.
*/
public Massive pollFirst() { public Massive pollFirst() {
if (first == null) {
//TODO: implement method. return null;
return null; }
Massive m = first.body;
first = first.next;
if (first != null) first.setPrev(null);
size--;
return m;
} }
// Retrieves and removes the last element in this list. /**
// Returns 'null' if the list is empty. * Retrieves and removes the last element in this list.
* Returns 'null' if the list is empty.
*/
public Massive pollLast() { public Massive pollLast() {
if (last == null) {
//TODO: implement method. return null;
return null; }
Massive m = last.body;
last = last.next;
if (last != null) last.setNext(null);
size--;
return m;
} }
// Inserts the specified element at the specified position in this list. /**
// Precondition: i >= 0 && i <= size(). * Inserts the specified element at the specified position in this list.
* Precondition: i >= 0 && i <= size().
*/
public void add(int i, Massive m) { public void add(int i, Massive m) {
if (first == null || i == 0) {
addFirst(m);
return;
} else if (i == size) {
addLast(m);
return;
}
//TODO: implement method. 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++;
} }
// Returns the element at the specified position in this list. /**
// Precondition: i >= 0 && i < size(). * Returns the element at the specified position in this list.
* Precondition: i >= 0 && i < size().
*/
public Massive get(int i) { public Massive get(int i) {
Item item;
//TODO: implement method. if (i < size / 2) {
return null; 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;
} }
// Returns the index of the first occurrence of the specified element in this list, or -1 if /**
// this list does not contain the element. * Returns the index of the first occurrence of the specified element in this list, or -1 if
* this list does not contain the element.
*/
public int indexOf(Massive m) { public int indexOf(Massive m) {
if (first == null) {
return -1;
}
//TODO: implement method. Item item = first;
return -2; for (int i = 0; i < size; i++, item = item.next) {
} if (item.body.equals(m)) return i;
}
// Returns the number of elements in this list.
public int size() {
//TODO: implement method.
return -1; return -1;
} }
/**
* Returns the number of elements in this list.
*/
public int size() {
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 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;
}
}
} }

View File

@@ -1,42 +1,79 @@
public class NamedBody /* TODO: add clause(s) */ import codedraw.CodeDraw;
{
// TODO: add missing parts of this class. public class NamedBody implements Massive {
// Initializes this with name, mass, current position and movement. The associated force private final String name;
// is initialized with a zero vector. private final Body body;
/**
* Initializes this with name, mass, current position and movement.
*/
public NamedBody(String name, double mass, Vector3 massCenter, Vector3 currentMovement) { public NamedBody(String name, double mass, Vector3 massCenter, Vector3 currentMovement) {
// TODO: implement constructor. this(name, new Body(mass, massCenter, currentMovement));
} }
// Returns the name of the body. 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() { public String getName() {
// TODO: implement method. return name;
return "";
} }
// Compares `this` with the specified object. Returns `true` if the specified `o` is not public Body getBody() {
// `null` and is of type `NamedBody` and both `this` and `o` have equal names. return body;
// Otherwise `false` is returned. }
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.
*/
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
//TODO: implement method. if (!(o instanceof NamedBody b)) return false;
return false; return this.name.equals(b.name);
} }
// Returns the hashCode of `this`. /**
* Returns the hashCode of `this`.
*/
@Override
public int hashCode() { public int hashCode() {
//TODO: implement method. return this.name.hashCode();
return 0;
} }
// Returns a readable representation including the name of this body. /**
* Returns a readable representation including the name of this body.
*/
@Override
public String toString() { public String toString() {
//TODO: implement method. return this.getName();
return ""; }
@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 { public class NamedBodyForcePair implements CosmicSystem {
private final String name; private final NamedBody body;
private final Body body;
private final Vector3 force = new Vector3(); private final Vector3 force = new Vector3();
/** /**
@@ -15,27 +14,30 @@ public class NamedBodyForcePair implements CosmicSystem {
* is initialized with a zero vector. * is initialized with a zero vector.
*/ */
public NamedBodyForcePair(String name, double mass, Vector3 massCenter, Vector3 currentMovement) { 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) { public NamedBodyForcePair(String name, Body body) {
this.body = b; this(new NamedBody(name, body));
this.name = name; }
public NamedBodyForcePair(NamedBody body) {
this.body = body;
} }
public NamedBodyForcePair(NamedBodyForcePair other) { public NamedBodyForcePair(NamedBodyForcePair other) {
this(other.name, new Body(other.body)); this(new NamedBody(other.body));
} }
public Body getBody() { public Body getBody() {
return body; return body.getBody();
} }
/** /**
* Returns the name of the body. * Returns the name of the body.
*/ */
public String getName() { public String getName() {
return name; return body.getName();
} }
@Override @Override
@@ -70,13 +72,13 @@ public class NamedBodyForcePair implements CosmicSystem {
@Override @Override
public void addForceTo(CosmicSystem cs) { public void addForceTo(CosmicSystem cs) {
cs.addForceFrom(body); cs.addForceFrom(body.getBody());
} }
@Override @Override
public BodyLinkedList getBodies() { public BodyLinkedList getBodies() {
BodyLinkedList list = new BodyLinkedList(); BodyLinkedList list = new BodyLinkedList();
list.addFirst(body); list.addFirst(body.getBody());
return list; return list;
} }

View File

@@ -3,8 +3,9 @@ import codedraw.CodeDraw;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
// Simulates the formation of a massive solar system. /**
// * Simulates the formation of a massive solar system.
*/
public class Simulation5 { public class Simulation5 {
// gravitational constant // gravitational constant
@@ -30,39 +31,83 @@ public class Simulation5 {
// all quantities are based on units of kilogram respectively second and meter. // all quantities are based on units of kilogram respectively second and meter.
// The main simulation method using instances of other classes. /**
* The main simulation method using instances of other classes.
*/
public static void main(String[] args) { public static void main(String[] args) {
// simulation // simulation
CodeDraw cd = new CodeDraw(); CodeDraw cd = new CodeDraw();
// create solar system with 12 bodies // 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 sun = new NamedBody(SolSystem4.SUN_NAMED);
NamedBody earth = new NamedBody("Earth",5.972E24, new Vector3(-6.13135922534815E10,-1.383789852227691E11,2.719682263474911E7), new Vector3(26832.720535473603,-11948.23168764519,1.9948243075997851)); NamedBody earth = new NamedBody(SolSystem4.EARTH_NAMED);
NamedBody moon = new NamedBody("Moon",7.349E22, new Vector3(-6.132484773775896E10,-1.387394951280871E11,1.701046736294776E7), new Vector3(27916.62329282941,-12020.39526008238,-94.89703264508708)); NamedBody moon = new NamedBody(SolSystem4.MOON_NAMED);
NamedBody mars = new NamedBody("Mars",6.41712E23, new Vector3(-1.7923193702925848E11,1.726665823982123E11,7.991673845249474E9), new Vector3(-15925.78496403673,-15381.16179928219,68.67560910598857)); NamedBody mars = new NamedBody(SolSystem4.MARS_NAMED);
NamedBody deimos = new NamedBody("Deimos",1.8E20, new Vector3(-1.792255010450533E11,1.726891122683271E11,7.990659337380297E9), new Vector3(-17100.476719804457,-15020.348656808,631.2927851249581)); NamedBody deimos = new NamedBody(SolSystem4.DEIMOS_NAMED);
NamedBody phobos = new NamedBody("Phobos",1.08E20, new Vector3(-1.792253482539647E11,1.72661109673625E11,7.987848354800322E9), new Vector3(-14738.203714241401,-13671.17675223948,-411.0012490555253)); NamedBody phobos = new NamedBody(SolSystem4.PHOBOS_NAMED);
NamedBody mercury = new NamedBody("Mercury",3.301E23, new Vector3(-5.167375560011926E10,-4.217574885682655E10,1.14808913958168E9), new Vector3(21580.25398577148,-34951.03632847389,-4835.225596525241)); NamedBody mercury = new NamedBody(SolSystem4.MERCURY_NAMED);
NamedBody venus = new NamedBody("Venus",4.86747E24, new Vector3(-3.123150865740532E10,1.0395568504115701E11,3.173401325838074E9), new Vector3(-33748.180519629335,-10014.25141045021,1809.94488874165)); NamedBody venus = new NamedBody(SolSystem4.VENUS_NAMED);
NamedBody vesta = new NamedBody("Vesta",2.5908E20, new Vector3(-3.337493557929893E11,-4.7147908276077385E10,4.1923010146878105E10), new Vector3(4440.54247538484,-19718.49074006637,48.06573124543601)); NamedBody vesta = new NamedBody(SolSystem4.VESTA_NAMED);
NamedBody pallas = new NamedBody("Pallas",2.14E20, new Vector3(4.3452066613895575E11,-2.057319365171432E11,1.0549957423213101E11), new Vector3(5058.947582097117,11184.45711782372,-8183.524138259704)); NamedBody pallas = new NamedBody(SolSystem4.PALLAS_NAMED);
NamedBody hygiea = new NamedBody("Hygiea",8.32E19, new Vector3(-3.983943433707043E11,2.325833000024021E11,-2.233667695713672E10), new Vector3(-6931.864585548552,-15686.8108598699,-690.5791992347208)); NamedBody hygiea = new NamedBody(SolSystem4.HYGIEA_NAMED);
NamedBody ceres = new NamedBody("Ceres",9.394E20, new Vector3(3.781372641419032E11,1.96718960466285E11,-6.366459168068592E10), new Vector3(-8555.324226752316,14718.33755980907,2040.230135060142)); NamedBody ceres = new NamedBody(SolSystem4.CERES_NAMED);
// create some additional bodies // create some additional bodies
Body[] bodies = new Body[NUMBER_OF_BODIES]; Body[] bodies = new Body[NUMBER_OF_BODIES];
Random random = new Random(2022); Random random = new Random(2022);
for (int i = 0; i < bodies.length; i++) { 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.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'. MassiveForceHashMap forceOnBody = new MassiveForceHashMap();
// Add both, NamedBody- and Body-objects, to your simulation. 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();
}
}
} }
} }

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 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 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 NamedBody SUN_NAMED = new NamedBody("Sun", SUN);
public static final NamedBodyForcePair EARTH_NAMED = new NamedBodyForcePair("Earth", EARTH); public static final NamedBody EARTH_NAMED = new NamedBody("Earth", EARTH);
public static final NamedBodyForcePair MOON_NAMED = new NamedBodyForcePair("Moon", MOON); public static final NamedBody MOON_NAMED = new NamedBody("Moon", MOON);
public static final NamedBodyForcePair MARS_NAMED = new NamedBodyForcePair("Mars", MARS); public static final NamedBody MARS_NAMED = new NamedBody("Mars", MARS);
public static final NamedBodyForcePair DEIMOS_NAMED = new NamedBodyForcePair("Deimos", DEIMOS); public static final NamedBody DEIMOS_NAMED = new NamedBody("Deimos", DEIMOS);
public static final NamedBodyForcePair PHOBOS_NAMED = new NamedBodyForcePair("Phobos", PHOBOS); public static final NamedBody PHOBOS_NAMED = new NamedBody("Phobos", PHOBOS);
public static final NamedBodyForcePair MERCURY_NAMED = new NamedBodyForcePair("Mercury", MERCURY); public static final NamedBody MERCURY_NAMED = new NamedBody("Mercury", MERCURY);
public static final NamedBodyForcePair VENUS_NAMED = new NamedBodyForcePair("Venus", VENUS); public static final NamedBody VENUS_NAMED = new NamedBody("Venus", VENUS);
public static final NamedBodyForcePair VESTA_NAMED = new NamedBodyForcePair("Vesta", VESTA); public static final NamedBody VESTA_NAMED = new NamedBody("Vesta", VESTA);
public static final NamedBodyForcePair PALLAS_NAMED = new NamedBodyForcePair("Pallas", PALLAS); public static final NamedBody PALLAS_NAMED = new NamedBody("Pallas", PALLAS);
public static final NamedBodyForcePair HYGIEA_NAMED = new NamedBodyForcePair("Hygiea", HYGIEA); public static final NamedBody HYGIEA_NAMED = new NamedBody("Hygiea", HYGIEA);
public static final NamedBodyForcePair CERES_NAMED = new NamedBodyForcePair("Ceres", CERES); public static final NamedBody CERES_NAMED = new NamedBody("Ceres", CERES);
} }