Finish AB4

This commit is contained in:
2022-04-27 23:29:24 +02:00
parent b3ddcec038
commit 35743c64b6
7 changed files with 284 additions and 88 deletions

View File

@ -1,28 +1,93 @@
import codedraw.CodeDraw;
/**
* A body with a name and an associated force. The leaf node of
* a hierarchical cosmic system. This class implements 'CosmicSystem'.
*/
public class NamedBodyForcePair /* TODO: add clause */ {
public class NamedBodyForcePair implements CosmicSystem {
// TODO: define missing parts of this class.
private final String name;
private final Body body;
private final Vector3 force = new Vector3();
/**
* Initializes this with name, mass, current position and movement. The associated force
* is initialized with a zero vector.
*/
public NamedBodyForcePair(String name, double mass, Vector3 massCenter, Vector3 currentMovement) {
// TODO: implement constructor.
this(name, new Body(mass, massCenter, currentMovement));
}
public NamedBodyForcePair(String name, Body b) {
this.body = b;
this.name = name;
}
public NamedBodyForcePair(NamedBodyForcePair other) {
// TODO
this(other.name, new Body(other.body));
}
public Body getBody() {
return body;
}
/**
* Returns the name of the body.
*/
public String getName() {
// TODO: implement method.
return "";
return name;
}
@Override
public String toString() {
return this.getName();
}
@Override
public Vector3 getMassCenter() {
return body.massCenter();
}
@Override
public double getMass() {
return body.mass();
}
@Override
public int numberOfBodies() {
return 1;
}
@Override
public double distanceTo(CosmicSystem cs) {
return getMassCenter().distanceTo(cs.getMassCenter());
}
@Override
public void addForceFrom(Body b) {
force.add(body.gravitationalForce(b));
}
@Override
public void addForceTo(CosmicSystem cs) {
cs.addForceFrom(body);
}
@Override
public BodyLinkedList getBodies() {
BodyLinkedList list = new BodyLinkedList();
list.addFirst(body);
return list;
}
@Override
public void update() {
body.move(force);
force.set(0);
}
@Override
public void draw(CodeDraw cd) {
body.draw(cd);
}
}