From e311ef3c6b1f8d316479373de74686df1def9aac Mon Sep 17 00:00:00 2001 From: Anton Ertl Date: Mon, 16 May 2022 20:27:14 +0200 Subject: [PATCH] Aufgabenblatt 6 --- angabe/Aufgabenblatt6.md | 80 ++++++++++++++++ src/Aufgabe6Test.java | 180 +++++++++++++++++++++++++++++++++++ src/MassiveForceTreeMap.java | 56 +++++++++++ src/MassiveIterable.java | 8 ++ src/MassiveIterator.java | 15 +++ src/MassiveSet.java | 21 ++++ src/Simulation6.java | 68 +++++++++++++ 7 files changed, 428 insertions(+) create mode 100644 angabe/Aufgabenblatt6.md create mode 100644 src/Aufgabe6Test.java create mode 100644 src/MassiveForceTreeMap.java create mode 100644 src/MassiveIterable.java create mode 100644 src/MassiveIterator.java create mode 100644 src/MassiveSet.java create mode 100644 src/Simulation6.java diff --git a/angabe/Aufgabenblatt6.md b/angabe/Aufgabenblatt6.md new file mode 100644 index 0000000..85b5224 --- /dev/null +++ b/angabe/Aufgabenblatt6.md @@ -0,0 +1,80 @@ +# 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 + + + + + diff --git a/src/Aufgabe6Test.java b/src/Aufgabe6Test.java new file mode 100644 index 0000000..3198e73 --- /dev/null +++ b/src/Aufgabe6Test.java @@ -0,0 +1,180 @@ +import java.util.HashSet; + +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; + + // 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)); + + // 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 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); + + 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); + + System.out.println("Test2:"); + testValue(map.getKeys().size(), 12); + + 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); + + System.out.println("Test4:"); + HashSet set2 = new HashSet<>(); + for(Massive m: map.getKeys()) { + set2.add(m); + } + + testValue(set1.equals(set2), true); + + System.out.println("Test5:"); + MassiveLinkedList list = map.getKeys().toList(); + while (list.size() > 0) { + set1.remove(list.pollLast()); + } + testValue(set1.isEmpty(), true); + + System.out.println("Test6:"); + map.getKeys().remove(mars1); + testValue(map.containsKey(mars1), false); + testValue(map.getKeys().size(), 11); + map.getKeys().clear(); + testValue(map.getKeys().size(), 0); + + System.out.println("Test7:"); + 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)); + + 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++; + } + testValue(count, 12); + + */ //TODO: uncomment + } + + public static void testComparison(Object first, Object second, boolean expected) { + boolean real = first == second; + + if (real == expected) { + System.out.println("Successful comparison"); + } else { + System.out.println("Comparison NOT successful! Expected value: " + expected + " / Given value: " + real); + } + } + + public static void testValue(Object given, Object expected) { + if (given == expected) { + System.out.println("Successful test"); + } else { + System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given); + } + } + + public static void testValue(double given, double expected) { + if (given < expected + (expected + 1) / 1e12 && given > expected - (expected + 1) / 1e12) { + System.out.println("Successful test"); + } else { + System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given); + } + } + +} diff --git a/src/MassiveForceTreeMap.java b/src/MassiveForceTreeMap.java new file mode 100644 index 0000000..7cc5e48 --- /dev/null +++ b/src/MassiveForceTreeMap.java @@ -0,0 +1,56 @@ +// 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 { + + // TODO: define missing parts of this class. + + // 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) { + + // TODO: implement method. + return null; + } + + // Returns the value associated with the specified key, i.e. the method returns the force vector + // associated with the specified key. Returns 'null' if the key is not contained in this map. + // Precondition: key != null. + public Vector3 get(Massive key) { + + // TODO: implement method. + return null; + } + + // Returns 'true' if this map contains a mapping for the specified key. + //Precondition: key != null + public boolean containsKey(Massive key) { + + // TODO: implement method. + 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 ""; + } + + // 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() { + + // TODO: implement method. + return null; + } +} + +//TODO: Define additional class(es) implementing the binary search tree and the implementation +// of MassiveSet (either here or in a separate file). + diff --git a/src/MassiveIterable.java b/src/MassiveIterable.java new file mode 100644 index 0000000..453a82f --- /dev/null +++ b/src/MassiveIterable.java @@ -0,0 +1,8 @@ +// Iterable objects with 'Massive' elements. +// +public interface MassiveIterable extends Iterable { + + @Override + // Returns an iterator over elements of 'Massive'. + MassiveIterator iterator(); +} diff --git a/src/MassiveIterator.java b/src/MassiveIterator.java new file mode 100644 index 0000000..dcd2453 --- /dev/null +++ b/src/MassiveIterator.java @@ -0,0 +1,15 @@ +import java.util.Iterator; + +// An iterator over elements of 'Massive'. +// +public interface MassiveIterator extends Iterator { + + @Override + // Returns the next element in the iteration. + // (Returns 'null' if the iteration has no more elements.) + Massive next(); + + @Override + // Returns 'true' if the iteration has more elements. + boolean hasNext(); +} diff --git a/src/MassiveSet.java b/src/MassiveSet.java new file mode 100644 index 0000000..850e84d --- /dev/null +++ b/src/MassiveSet.java @@ -0,0 +1,21 @@ +// 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(); + +} \ No newline at end of file diff --git a/src/Simulation6.java b/src/Simulation6.java new file mode 100644 index 0000000..f4cbc3f --- /dev/null +++ b/src/Simulation6.java @@ -0,0 +1,68 @@ +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("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)); + + // 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)); + } + + //TODO: implementation of this method according to 'Aufgabenblatt6.md'. + // Add both, NamedBody- and Body-objects, to your simulation. + + } +}