Refactored for AB5

This commit is contained in:
2022-05-06 22:42:28 +02:00
parent e84bf3bf4a
commit 6b1f1ecc2a
6 changed files with 176 additions and 199 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)
beschreibt. `Massive` ist der gemeinsame Obertyp für verschiedene Klassen von Himmelkörpern. Die
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.
- [NamedBody](../src/NamedBody.java) ist das Gerüst einer Klassendefinition. Die Klasse
repräsentiert Himmelskörper, die einen Namen haben.
@ -100,8 +100,3 @@ Ihre Aufgaben sind folgende:
- Implementierung von `Simulation5`: 1 Punkte
- Gesamt: 5 Punkte

View File

@ -1,57 +1,43 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class Aufgabe5Test {
public static void main(String[] args) {
@Test
public void testEP2() {
/* //TODO: uncomment for testing
//test classes NamedBody and MassiveForceHashMap
// create 12 named bodies
NamedBody sun1, mercury1, venus1, earth1, moon1, mars1, deimos1, phobos1, vesta1,
pallas1, hygiea1, ceres1;
NamedBody sun1, mercury1, venus1, earth1, moon1, mars1, deimos1, phobos1, vesta1, pallas1, hygiea1, ceres1;
// create a nameless body
Body earth2 = new Body(5.972E24, new Vector3(-6.13135922534815E10,-1.383789852227691E11,
2.719682263474911E7), new Vector3(26832.720535473603,-11948.23168764519,1.9948243075997851));
Body earth2 = new Body(5.972E24, new Vector3(-6.13135922534815E10, -1.383789852227691E11, 2.719682263474911E7), new Vector3(26832.720535473603, -11948.23168764519, 1.9948243075997851));
// 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("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));
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), new Vector3(0.0, 0.0, 0.0));
NamedBody earth3 = new NamedBody("Earth", 1, new Vector3(0, 0, 0), new Vector3(0, 0, 0));
testValue(sun1.equals(sun2), true);
testValue(sun1.hashCode(), sun2.hashCode());
testValue(earth1.equals(earth3), true);
testValue(earth1.hashCode(), earth3.hashCode());
assertEquals(sun1, sun2);
assertEquals(sun2.hashCode(), sun1.hashCode());
assertEquals(earth1, earth3);
assertEquals(earth3.hashCode(), earth1.hashCode());
// check basic functions of 'MassiveForceHashMap'
System.out.println("Test2:");
MassiveForceHashMap map = new MassiveForceHashMap();
map.put(sun1, new Vector3(0, 0, 0));
map.put(mercury1, new Vector3(0, 0, 0));
@ -66,60 +52,27 @@ public class Aufgabe5Test {
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
testValue(map.keyList().size(), 12);
assertEquals(12, map.keyList().size());
System.out.println("Test3:");
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("Test4:");
MassiveLinkedList bl = map.keyList();
boolean allThere = true;
while (bl.size() > 0) {
allThere &= map.containsKey(bl.pollFirst());
}
testValue(allThere, true);
testValue(map.containsKey(new Body(0,new Vector3(0,0,0), new Vector3(0,0,0))),
false);
testValue(map.containsKey(new NamedBody("Omuamua",0,new Vector3(0,0,0), new Vector3(0,0,
0))),
false);
assertTrue(allThere);
assertFalse(map.containsKey(new Body(0, new Vector3(0, 0, 0), new Vector3(0, 0, 0))));
assertFalse(map.containsKey(new NamedBody("Omuamua", 0, new Vector3(0, 0, 0), new Vector3(0, 0, 0))));
System.out.println("Test5:");
Vector3 f = new Vector3(5, 5, 5);
map.put(earth3, f);
testValue(map.get(earth1), f);
testValue(map.get(earth2), null);
assertEquals(f, map.get(earth1));
assertNull(map.get(earth2));
*/ //TODO: uncomment
}
public static void testComparison(Object first, Object second, boolean expected) {
boolean real = first == second;
if (real == expected) {
System.out.println("Successful comparison");
} else {
System.out.println("Comparison NOT successful! Expected value: " + expected + " / Given value: " + real);
}
}
public static void testValue(Object given, Object expected) {
if (given == expected) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
}
public static void testValue(double given, double expected) {
if (given < expected + (expected + 1) / 1e12 && given > expected - (expected + 1) / 1e12) {
System.out.println("Successful test");
} else {
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
}
}
}

View File

@ -1,54 +1,62 @@
// 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 {
// Returns the mass.
/**
* Returns the mass.
*/
default double mass() {
return getMass();
}
// Returns the mass center.
/**
* Returns the mass center.
*/
default Vector3 massCenter() {
return getMassCenter();
}
// Returns the mass.
/**
* Returns the mass.
*/
default double getMass() {
return mass();
}
// Returns the mass center.
/**
* Returns the mass center.
*/
default Vector3 getMassCenter() {
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,
// where m and r measured in solar units.)
/**
* 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,
* where m and r measured in solar units.)
*/
default double getRadius() {
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,
// where m and r measured in solar units.)
/**
* 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,
* where m and r measured in solar units.)
*/
default double radius() {
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
// masses of the objects interacting, r being the distance between the centers of the masses
// and G being the gravitational constant.
/**
* 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
* 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) {
Vector3 direction = b.massCenter().minus(this.massCenter());
double distance = direction.length();
direction.normalize();
@ -56,9 +64,10 @@ public interface Massive extends Drawable {
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.
// (Velocity depends on the mass of 'this', its current velocity and the exerted 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.
* (Velocity depends on the mass of 'this', its current velocity and the exerted force.)
*/
void move(Vector3 force);
}

View File

@ -1,94 +1,108 @@
// A list of massive objects implemented as a linked list.
// The number of elements of the list is not limited.
/**
* A list of massive objects implemented as a linked list.
* The number of elements of the list is not limited.
*/
public class MassiveLinkedList {
//TODO: declare variables.
// Initializes 'this' as an empty list.
/**
* Initializes 'this' as an empty list.
*/
public MassiveLinkedList() {
//TODO: define constructor.
}
// Initializes 'this' as an independent copy of the specified list 'list'.
// Calling methods of this list will not affect the specified list 'list'
// and vice versa.
// Precondition: list != null.
/**
* Initializes 'this' as an independent copy of the specified list 'list'.
* Calling methods of this list will not affect the specified list 'list'
* and vice versa.
* Precondition: list != null.
*/
public MassiveLinkedList(BodyLinkedList list) {
//TODO: define constructor.
}
// 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) {
//TODO: implement method.
}
// 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) {
//TODO: implement method.
}
// 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() {
//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() {
//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() {
//TODO: implement method.
return null;
}
// 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() {
//TODO: implement method.
return null;
}
// 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) {
//TODO: implement method.
}
// 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) {
//TODO: implement method.
return null;
}
// 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) {
//TODO: implement method.
return -2;
}
// Returns the number of elements in this list.
/**
* Returns the number of elements in this list.
*/
public int size() {
//TODO: implement method.
return -1;
}

View File

@ -1,42 +1,46 @@
public class NamedBody /* TODO: add clause(s) */
{
public class NamedBody /* TODO: add clause(s) */ {
// TODO: add missing parts of this class.
// Initializes this with name, mass, current position and movement. The associated force
// is initialized with a zero vector.
/**
* Initializes this with name, mass, current position and movement. The associated force
* is initialized with a zero vector.
*/
public NamedBody(String name, double mass, Vector3 massCenter, Vector3 currentMovement) {
// TODO: implement constructor.
}
// Returns the name of the body.
/**
* Returns the name of the body.
*/
public String getName() {
// TODO: implement method.
return "";
}
// 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.
/**
* 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.
*/
public boolean equals(Object o) {
//TODO: implement method.
return false;
}
// Returns the hashCode of `this`.
/**
* Returns the hashCode of `this`.
*/
public int hashCode() {
//TODO: implement method.
return 0;
}
// Returns a readable representation including the name of this body.
/**
* Returns a readable representation including the name of this body.
*/
public String toString() {
//TODO: implement method.
return "";
}
}

View File

@ -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 Simulation5 {
// gravitational constant
@ -30,7 +31,9 @@ public class Simulation5 {
// 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
@ -63,6 +66,5 @@ public class Simulation5 {
//TODO: implementation of this method according to 'Aufgabenblatt5.md'.
// Add both, NamedBody- and Body-objects, to your simulation.
}
}