Aufgabenblatt 2

This commit is contained in:
Anton Ertl
2022-03-21 15:51:04 +00:00
4 changed files with 223 additions and 0 deletions

80
src/Aufgabe2Test.java Normal file
View File

@ -0,0 +1,80 @@
public class Aufgabe2Test {
public static void main(String[] args) {
//test classes BodyQueue and BodyForceMap
// create three bodies
Body sun = new Body(1.989e30,new Vector3(0,0,0),new Vector3(0,0,0));
Body earth = new Body(5.972e24,new Vector3(-1.394555e11,5.103346e10,0),new Vector3(-10308.53,-28169.38,0));
Body mercury = new Body(3.301e23,new Vector3(-5.439054e10,9.394878e9,0),new Vector3(-17117.83,-46297.48,-1925.57));
// check basic functions of 'BodyQueue'
System.out.println("Test1:");
BodyQueue bq = new BodyQueue(2);
bq.add(mercury);
bq.add(sun);
bq.add(earth);
testValue(bq.size(), 3);
testValue(bq.poll(), mercury);
testValue(bq.poll(), sun);
testValue(bq.poll(), earth);
testValue(bq.size(), 0);
bq.add(mercury);
bq.add(sun);
testValue(bq.size(), 2);
// check constructor of 'BodyQueue'
BodyQueue bqCopy = new BodyQueue(bq);
testComparison(bq, bqCopy, false);
testComparison(bq.poll(), bqCopy.poll(), true);
bq.add(earth);
testValue(bq.size(), 2);
testValue(bqCopy.size(), 1);
// check basic functions of 'BodyForceMap'
System.out.println("Test2:");
BodyForceMap bfm = new BodyForceMap(5);
bfm.put(earth, earth.gravitationalForce(sun));
bfm.put(sun, sun.gravitationalForce(earth));
testValue(bfm.get(earth).distanceTo(earth.gravitationalForce(sun)),0);
testValue(bfm.get(sun).distanceTo(sun.gravitationalForce(earth)),0);
bfm.put(earth, new Vector3(0,0,0));
testValue(bfm.get(earth).distanceTo(new Vector3(0,0,0)), 0);
testValue(bfm.get(mercury),null);
}
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);
}
}
}

32
src/BodyForceMap.java Normal file
View File

@ -0,0 +1,32 @@
// A map that associates a body with a force exerted on it. The number of
// key-value pairs is not limited.
//
public class BodyForceMap {
//TODO: declare variables.
// Initializes this map with an initial capacity.
// Precondition: initialCapacity > 0.
public BodyForceMap(int initialCapacity) {
//TODO: define constructor.
}
// 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(Body key, Vector3 force) {
//TODO: implement method.
return null;
}
// Returns the value associated with the specified key, i.e. the returns the force vector
// associated with the specified body. Returns 'null' if the key is not contained in this map.
// Precondition: key != null.
public Vector3 get(Body key) {
//TODO: implement method.
return null;
}
}

46
src/BodyQueue.java Normal file
View File

@ -0,0 +1,46 @@
// A queue of bodies. A collection designed for holding bodies prior to processing.
// The bodies of the queue can be accessed in a FIFO (first-in-first-out) manner,
// i.e., the body that was first inserted by 'add' is retrieved first by 'poll'.
// The number of elements of the queue is not limited.
//
public class BodyQueue {
//TODO: declare variables.
// Initializes this queue with an initial capacity.
// Precondition: initialCapacity > 0.
public BodyQueue(int initialCapacity) {
//TODO: define constructor.
}
// Initializes this queue as an independent copy of the specified queue.
// Calling methods of this queue will not affect the specified queue
// and vice versa.
// Precondition: q != null.
public BodyQueue(BodyQueue q) {
//TODO: define constructor.
}
// Adds the specified body 'b' to this queue.
public void add(Body b) {
//TODO: implement method.
}
// Retrieves and removes the head of this queue, or returns 'null'
// if this queue is empty.
public Body poll() {
//TODO: implement method.
return null;
}
// Returns the number of bodies in this queue.
public int size() {
//TODO: implement method.
return -1;
}
}