AB3: BodyForceTreeMap working
This commit is contained in:
@ -106,7 +106,7 @@ public class Body {
|
|||||||
*/
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format(
|
return String.format(
|
||||||
"%f kg, position: %s m, movement: %s m/s.",
|
"%g kg, position: %s m, movement: %s m/s.",
|
||||||
mass, massCenter.toString(), currentMovement.toString()
|
mass, massCenter.toString(), currentMovement.toString()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
* The number of key-value pairs is not limited.
|
* The number of key-value pairs is not limited.
|
||||||
*/
|
*/
|
||||||
public class BodyForceTreeMap {
|
public class BodyForceTreeMap {
|
||||||
|
private int size = 0;
|
||||||
//TODO: declare variables.
|
private BodyForceTreeMapItem root = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new key-value association to this map. If the key already exists in this map,
|
* Adds a new key-value association to this map. If the key already exists in this map,
|
||||||
@ -12,7 +12,37 @@ public class BodyForceTreeMap {
|
|||||||
* Precondition: key != null.
|
* Precondition: key != null.
|
||||||
*/
|
*/
|
||||||
public Vector3 put(Body key, Vector3 value) {
|
public Vector3 put(Body key, Vector3 value) {
|
||||||
//TODO: implement method.
|
if (root == null) {
|
||||||
|
root = new BodyForceTreeMapItem(key, value);
|
||||||
|
size++;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
BodyForceTreeMapItem item = root;
|
||||||
|
while (item != null) {
|
||||||
|
if (item.key() == key) {
|
||||||
|
Vector3 old = item.value();
|
||||||
|
item.setValue(value);
|
||||||
|
return old;
|
||||||
|
} else if (item.key().mass() > key.mass()) {
|
||||||
|
if (item.left() != null) {
|
||||||
|
item = item.left();
|
||||||
|
} else {
|
||||||
|
item.setLeft(new BodyForceTreeMapItem(key, value));
|
||||||
|
size++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (item.right() != null) {
|
||||||
|
item = item.right();
|
||||||
|
} else{
|
||||||
|
item.setRight(new BodyForceTreeMapItem(key, value));
|
||||||
|
size++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,7 +52,16 @@ public class BodyForceTreeMap {
|
|||||||
* Precondition: key != null.
|
* Precondition: key != null.
|
||||||
*/
|
*/
|
||||||
public Vector3 get(Body key) {
|
public Vector3 get(Body key) {
|
||||||
//TODO: implement method.
|
BodyForceTreeMapItem item = root;
|
||||||
|
while (item != null) {
|
||||||
|
if (item.key() == key) {
|
||||||
|
return item.value();
|
||||||
|
} else if (item.key().mass() > key.mass()) {
|
||||||
|
item = item.left();
|
||||||
|
} else {
|
||||||
|
item = item.right();
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,16 +69,38 @@ public class BodyForceTreeMap {
|
|||||||
* Returns 'true' if this map contains a mapping for the specified key.
|
* Returns 'true' if this map contains a mapping for the specified key.
|
||||||
*/
|
*/
|
||||||
public boolean containsKey(Body key) {
|
public boolean containsKey(Body key) {
|
||||||
//TODO: implement method.
|
BodyForceTreeMapItem item = root;
|
||||||
|
while (item != null) {
|
||||||
|
if (item.key() == key) {
|
||||||
|
return true;
|
||||||
|
} else if (item.key().mass() > key.mass()) {
|
||||||
|
item = item.left();
|
||||||
|
} else {
|
||||||
|
item = item.right();
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return this.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String toString(BodyForceTreeMapItem item, String indent) {
|
||||||
|
if (item == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
String s = String.format("%s{%s: %s}\n", indent, item.key(), item.value());
|
||||||
|
s += this.toString(item.left(), indent + "| ");
|
||||||
|
s += this.toString(item.right(), indent + "| ");
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a readable representation of this map, in which key-value pairs are ordered
|
* Returns a readable representation of this map, in which key-value pairs are ordered
|
||||||
* descending according to the mass of the bodies.
|
* descending according to the mass of the bodies.
|
||||||
*/
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
//TODO: implement method.
|
return toString(root, "");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
46
src/BodyForceTreeMapItem.java
Normal file
46
src/BodyForceTreeMapItem.java
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
class BodyForceTreeMapItem {
|
||||||
|
private final Body key;
|
||||||
|
private Vector3 value;
|
||||||
|
private BodyForceTreeMapItem parent;
|
||||||
|
private BodyForceTreeMapItem left;
|
||||||
|
private BodyForceTreeMapItem right;
|
||||||
|
|
||||||
|
public BodyForceTreeMapItem(Body key, Vector3 value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Body key() {
|
||||||
|
return this.key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(Vector3 value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector3 value() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BodyForceTreeMapItem left() {
|
||||||
|
return this.left;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BodyForceTreeMapItem right() {
|
||||||
|
return this.right;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BodyForceTreeMapItem parent() {
|
||||||
|
return this.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLeft(BodyForceTreeMapItem left) {
|
||||||
|
this.left = left;
|
||||||
|
if (left != null) left.parent = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRight(BodyForceTreeMapItem right) {
|
||||||
|
this.right = right;
|
||||||
|
if (right != null) right.parent = this;
|
||||||
|
}
|
||||||
|
}
|
@ -14,7 +14,7 @@ public class Simulation3 {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CodeDraw cd = new CodeDraw();
|
CodeDraw cd = new CodeDraw();
|
||||||
BodyLinkedList bodies = new BodyLinkedList();
|
BodyLinkedList bodies = new BodyLinkedList();
|
||||||
BodyForceMap forceOnBody = new BodyForceMap();
|
BodyForceTreeMap forceOnBody = new BodyForceTreeMap();
|
||||||
|
|
||||||
Random random = new Random(2022);
|
Random random = new Random(2022);
|
||||||
|
|
||||||
|
@ -94,6 +94,15 @@ public class Vector3 {
|
|||||||
* in the form "[x,y,z]", e.g., "[1.48E11,0.0,0.0]".
|
* in the form "[x,y,z]", e.g., "[1.48E11,0.0,0.0]".
|
||||||
*/
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("[%f,%f,%f]", x, y, z);
|
return String.format("[%g,%g,%g]", x, y, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (other.getClass() != Vector3.class) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Vector3 v = (Vector3) other;
|
||||||
|
return this.x == v.x && this.y == v.y && this.z == v.z;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user