Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 05c52cd3f5 | |||
|
088fa3cdeb
|
|||
|
f801a331c2
|
|||
|
b89fc15602
|
@@ -73,8 +73,3 @@ Ihre Aufgaben sind folgende:
|
||||
- Implementierung von `Simulation6`: 0.5 Punkte
|
||||
|
||||
- Gesamt: 5 Punkte
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -78,4 +78,19 @@ public class Aufgabe5Test {
|
||||
assertEquals(hashCode2, map.hashCode());
|
||||
assertNotEquals(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelKey() {
|
||||
MassiveForceHashMap map = new MassiveForceHashMap();
|
||||
NamedBody sun1 = new NamedBody(SolSystem4.SUN_NAMED);
|
||||
NamedBody earth1 = new NamedBody(SolSystem4.EARTH_NAMED);
|
||||
NamedBody moon1 = new NamedBody(SolSystem4.MOON_NAMED);
|
||||
map.put(sun1, new Vector3());
|
||||
map.put(earth1, new Vector3());
|
||||
map.put(moon1, new Vector3());
|
||||
assertNotNull(map.get(sun1));
|
||||
assertNotNull(map.delete(sun1));
|
||||
assertNull(map.get(sun1));
|
||||
assertNull(map.delete(sun1));
|
||||
}
|
||||
}
|
||||
|
||||
+62
-126
@@ -1,54 +1,44 @@
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class Aufgabe6Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
/* //TODO: uncomment for testing
|
||||
NamedBody sun1, mercury1, venus1, earth1, moon1, mars1, deimos1, phobos1, vesta1,
|
||||
pallas1, hygiea1, ceres1;
|
||||
@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("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);
|
||||
|
||||
// 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
|
||||
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);
|
||||
@@ -64,117 +54,63 @@ public class Aufgabe6Test {
|
||||
set1.add(hygiea1);
|
||||
set1.add(ceres1);
|
||||
|
||||
System.out.println("Test1:");
|
||||
testValue(map.toString().contains("Mars"), true);
|
||||
testValue(map.toString().contains("Deimos"), true);
|
||||
testValue(map.toString().contains("Moon"), true);
|
||||
testValue(map.toString().contains("Earth"), true);
|
||||
assertTrue(map.toString().contains("Mars"));
|
||||
assertTrue(map.toString().contains("Deimos"));
|
||||
assertTrue(map.toString().contains("Moon"));
|
||||
assertTrue(map.toString().contains("Earth"));
|
||||
|
||||
System.out.println("Test2:");
|
||||
testValue(map.getKeys().size(), 12);
|
||||
assertEquals(12, map.getKeys().size());
|
||||
|
||||
System.out.println("Test3:");
|
||||
testValue(map.getKeys().contains(mars1), true);
|
||||
testValue(map.getKeys().contains(new NamedBody("Mars",6.41712E23,
|
||||
new Vector3(0,0,0),
|
||||
new Vector3(0,0,0))), true);
|
||||
testValue(map.getKeys().contains(new Body(6.41712E23,
|
||||
new Vector3(0,0,0),
|
||||
new Vector3(0,0,0))), false);
|
||||
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))));
|
||||
|
||||
System.out.println("Test4:");
|
||||
HashSet<Massive> set2 = new HashSet<>();
|
||||
for(Massive m: map.getKeys()) {
|
||||
for (Massive m : map.getKeys()) {
|
||||
set2.add(m);
|
||||
}
|
||||
|
||||
testValue(set1.equals(set2), true);
|
||||
assertEquals(set1, set2);
|
||||
|
||||
System.out.println("Test5:");
|
||||
MassiveLinkedList list = map.getKeys().toList();
|
||||
while (list.size() > 0) {
|
||||
set1.remove(list.pollLast());
|
||||
}
|
||||
testValue(set1.isEmpty(), true);
|
||||
assertTrue(set1.isEmpty());
|
||||
|
||||
System.out.println("Test6:");
|
||||
map.getKeys().remove(mars1);
|
||||
testValue(map.containsKey(mars1), false);
|
||||
testValue(map.getKeys().size(), 11);
|
||||
assertFalse(map.containsKey(mars1));
|
||||
assertEquals(11, map.getKeys().size());
|
||||
map.getKeys().clear();
|
||||
testValue(map.getKeys().size(), 0);
|
||||
assertEquals(0, map.getKeys().size());
|
||||
|
||||
System.out.println("Test7:");
|
||||
NamedBodyForcePair sun2, mercury2, venus2, earth2, moon2, mars2, deimos2, phobos2, vesta2,
|
||||
pallas2, hygiea2, ceres2;
|
||||
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("Sun",1.989E30, new Vector3(0.0,0.0,0.0)
|
||||
, new Vector3(0.0,0.0,0.0));
|
||||
earth2 = new NamedBodyForcePair("Earth",5.972E24,
|
||||
new Vector3(-6.13135922534815E10,-1.383789852227691E11,2.719682263474911E7), new Vector3(26832.720535473603,-11948.23168764519,1.9948243075997851));
|
||||
moon2 = new NamedBodyForcePair("Moon",7.349E22,
|
||||
new Vector3(-6.132484773775896E10,-1.387394951280871E11,1.701046736294776E7), new Vector3(27916.62329282941,-12020.39526008238,-94.89703264508708));
|
||||
mars2 = new NamedBodyForcePair("Mars",6.41712E23,
|
||||
new Vector3(-1.7923193702925848E11,1.726665823982123E11,7.991673845249474E9), new Vector3(-15925.78496403673,-15381.16179928219,68.67560910598857));
|
||||
deimos2 = new NamedBodyForcePair("Deimos",1.8E20,
|
||||
new Vector3(-1.792255010450533E11,1.726891122683271E11,7.990659337380297E9), new Vector3(-17100.476719804457,-15020.348656808,631.2927851249581));
|
||||
phobos2 = new NamedBodyForcePair("Phobos",1.08E20,
|
||||
new Vector3(-1.792253482539647E11,1.72661109673625E11,7.987848354800322E9), new Vector3(-14738.203714241401,-13671.17675223948,-411.0012490555253));
|
||||
mercury2 = new NamedBodyForcePair("Mercury",3.301E23,
|
||||
new Vector3(-5.167375560011926E10,-4.217574885682655E10,1.14808913958168E9), new Vector3(21580.25398577148,-34951.03632847389,-4835.225596525241));
|
||||
venus2 = new NamedBodyForcePair("Venus",4.86747E24,
|
||||
new Vector3(-3.123150865740532E10,1.0395568504115701E11,3.173401325838074E9), new Vector3(-33748.180519629335,-10014.25141045021,1809.94488874165));
|
||||
vesta2 = new NamedBodyForcePair("Vesta",2.5908E20,
|
||||
new Vector3(-3.337493557929893E11,-4.7147908276077385E10,4.1923010146878105E10), new Vector3(4440.54247538484,-19718.49074006637,48.06573124543601));
|
||||
pallas2 = new NamedBodyForcePair("Pallas",2.14E20,
|
||||
new Vector3(4.3452066613895575E11,-2.057319365171432E11,1.0549957423213101E11), new Vector3(5058.947582097117,11184.45711782372,-8183.524138259704));
|
||||
hygiea2 = new NamedBodyForcePair("Hygiea",8.32E19,
|
||||
new Vector3(-3.983943433707043E11,2.325833000024021E11,-2.233667695713672E10), new Vector3(-6931.864585548552,-15686.8108598699,-690.5791992347208));
|
||||
ceres2 = new NamedBodyForcePair("Ceres",9.394E20,
|
||||
new Vector3(3.781372641419032E11,1.96718960466285E11,-6.366459168068592E10), new Vector3(-8555.324226752316,14718.33755980907,2040.230135060142));
|
||||
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);
|
||||
HierarchicalSystem solarSystem = new HierarchicalSystem(sun2, mercury2, venus2, earthSystem, marsSystem, vesta2, pallas2, hygiea2, ceres2);
|
||||
|
||||
int count = 0;
|
||||
for (Massive b: solarSystem) {
|
||||
for (Massive b : solarSystem) {
|
||||
count++;
|
||||
}
|
||||
testValue(count, 12);
|
||||
|
||||
*/ //TODO: uncomment
|
||||
assertEquals(12, count);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -103,10 +103,10 @@ public class BodyForceTreeMap {
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString(root);
|
||||
return (root != null) ? toString(root) : "";
|
||||
}
|
||||
|
||||
private class Item {
|
||||
private static class Item {
|
||||
private final Body key;
|
||||
private Vector3 value;
|
||||
private Item parent;
|
||||
|
||||
@@ -222,7 +222,7 @@ public class BodyLinkedList implements Iterable<Body> {
|
||||
};
|
||||
}
|
||||
|
||||
private class Item {
|
||||
private static class Item {
|
||||
private final Body body;
|
||||
private Item prev;
|
||||
private Item next;
|
||||
|
||||
@@ -5,7 +5,7 @@ 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 CosmicSystem[] orbit;
|
||||
@@ -127,4 +127,53 @@ public class HierarchicalSystem implements CosmicSystem {
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,16 +65,36 @@ public class MassiveForceHashMap {
|
||||
* Precondition: key != null.
|
||||
*/
|
||||
public Vector3 get(Massive key) {
|
||||
int pos = find(key);
|
||||
return (pos == -1) ? null : values[pos];
|
||||
}
|
||||
|
||||
private int find(Massive key) {
|
||||
int idx = ((key.hashCode() % keys.length) + keys.length) % keys.length;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
int pos = (idx + i) % keys.length;
|
||||
if (keys[pos] == null) {
|
||||
break;
|
||||
} else if (keys[pos].equals(key)) {
|
||||
return values[pos];
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the mapping for the specified key from this map if present.
|
||||
* Returns the previous value associated with key, or null if there was
|
||||
* no mapping for key.
|
||||
* Precondition: key != null
|
||||
*/
|
||||
public Vector3 delete(Massive key) {
|
||||
int pos = find(key);
|
||||
if (pos == -1) return null;
|
||||
|
||||
Vector3 val = values[pos];
|
||||
values[pos] = null;
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+214
-36
@@ -1,56 +1,234 @@
|
||||
// A map that associates an object of 'Massive' with a Vector3. The number of key-value pairs
|
||||
// is not limited.
|
||||
//
|
||||
// TODO: define further classes and methods for the binary search tree and the implementation
|
||||
// of MassiveSet, if needed.
|
||||
//
|
||||
public class MassiveForceTreeMap {
|
||||
import codedraw.CodeDraw;
|
||||
|
||||
// TODO: define missing parts of this class.
|
||||
/**
|
||||
* 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.
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: implement method.
|
||||
return null;
|
||||
}
|
||||
|
||||
// Returns the value associated with the specified key, i.e. the method returns the force vector
|
||||
// associated with the specified key. Returns 'null' if the key is not contained in this map.
|
||||
// Precondition: key != null.
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
// TODO: implement method.
|
||||
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
|
||||
/**
|
||||
* Returns 'true' if this map contains a mapping for the specified key.
|
||||
* Precondition: key != null
|
||||
*/
|
||||
public boolean containsKey(Massive key) {
|
||||
|
||||
// TODO: implement method.
|
||||
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;
|
||||
}
|
||||
|
||||
// Returns a readable representation of this map, in which key-value pairs are ordered
|
||||
// descending according to 'key.getMass()'.
|
||||
public String toString() {
|
||||
|
||||
// TODO: implement method.
|
||||
return "";
|
||||
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 `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() {
|
||||
/**
|
||||
* 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) : "";
|
||||
}
|
||||
|
||||
// TODO: implement method.
|
||||
return null;
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Define additional class(es) implementing the binary search tree and the implementation
|
||||
// of MassiveSet (either here or in a separate file).
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
// Iterable objects with 'Massive' elements.
|
||||
//
|
||||
/**
|
||||
* Iterable objects with 'Massive' elements.
|
||||
*/
|
||||
public interface MassiveIterable extends Iterable<Massive> {
|
||||
|
||||
/**
|
||||
* Returns an iterator over elements of 'Massive'.
|
||||
*/
|
||||
@Override
|
||||
// Returns an iterator over elements of 'Massive'.
|
||||
MassiveIterator iterator();
|
||||
}
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
import java.util.Iterator;
|
||||
|
||||
// An iterator over elements of 'Massive'.
|
||||
//
|
||||
/**
|
||||
* 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
|
||||
// Returns the next element in the iteration.
|
||||
// (Returns 'null' if the iteration has no more elements.)
|
||||
Massive next();
|
||||
|
||||
/**
|
||||
* Returns 'true' if the iteration has more elements.
|
||||
*/
|
||||
@Override
|
||||
// Returns 'true' if the iteration has more elements.
|
||||
boolean hasNext();
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ public class MassiveLinkedList implements Iterable<Massive> {
|
||||
return null;
|
||||
}
|
||||
Massive m = last.body;
|
||||
last = last.next;
|
||||
last = last.prev;
|
||||
if (last != null) last.setNext(null);
|
||||
size--;
|
||||
return m;
|
||||
@@ -194,7 +194,7 @@ public class MassiveLinkedList implements Iterable<Massive> {
|
||||
};
|
||||
}
|
||||
|
||||
private class Item {
|
||||
private static class Item {
|
||||
private final Massive body;
|
||||
private Item prev;
|
||||
private Item next;
|
||||
|
||||
+20
-10
@@ -1,21 +1,31 @@
|
||||
// A collection of 'Massive' objects in which there are no duplicates.
|
||||
//
|
||||
/**
|
||||
* 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).
|
||||
/**
|
||||
* 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.
|
||||
/**
|
||||
* Removes the specified element from the set.
|
||||
*/
|
||||
void remove(Massive element);
|
||||
|
||||
// Removes all elements from the set.
|
||||
/**
|
||||
* Removes all elements from the set.
|
||||
*/
|
||||
void clear();
|
||||
|
||||
// Returns the number of elements in the set.
|
||||
/**
|
||||
* Returns the number of elements in the set.
|
||||
*/
|
||||
int size();
|
||||
|
||||
// Returns an object of 'MassiveLinkedList' with all elements of 'this'.
|
||||
/**
|
||||
* Returns an object of 'MassiveLinkedList' with all elements of 'this'.
|
||||
*/
|
||||
MassiveLinkedList toList();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,6 @@ public class Simulation5 {
|
||||
* The main simulation method using instances of other classes.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// simulation
|
||||
CodeDraw cd = new CodeDraw();
|
||||
|
||||
@@ -52,6 +51,7 @@ public class Simulation5 {
|
||||
NamedBody pallas = new NamedBody(SolSystem4.PALLAS_NAMED);
|
||||
NamedBody hygiea = new NamedBody(SolSystem4.HYGIEA_NAMED);
|
||||
NamedBody ceres = new NamedBody(SolSystem4.CERES_NAMED);
|
||||
|
||||
// create some additional bodies
|
||||
Body[] bodies = new Body[NUMBER_OF_BODIES];
|
||||
|
||||
|
||||
+65
-20
@@ -3,8 +3,9 @@ import codedraw.CodeDraw;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
// Simulates the formation of a massive solar system.
|
||||
//
|
||||
/**
|
||||
* Simulates the formation of a massive solar system.
|
||||
*/
|
||||
public class Simulation6 {
|
||||
|
||||
// gravitational constant
|
||||
@@ -30,25 +31,26 @@ public class Simulation6 {
|
||||
|
||||
// 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) {
|
||||
|
||||
// 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];
|
||||
@@ -56,13 +58,56 @@ public class Simulation6 {
|
||||
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 'Aufgabenblatt6.md'.
|
||||
// Add both, NamedBody- and Body-objects, to your simulation.
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user