diff --git a/src/Body.java b/src/Body.java index 1b6685d..d7b3cc3 100644 --- a/src/Body.java +++ b/src/Body.java @@ -106,7 +106,7 @@ public class Body { */ public String toString() { 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() ); } diff --git a/src/BodyForceTreeMap.java b/src/BodyForceTreeMap.java index 11bab25..752861f 100644 --- a/src/BodyForceTreeMap.java +++ b/src/BodyForceTreeMap.java @@ -3,8 +3,8 @@ * The number of key-value pairs is not limited. */ public class BodyForceTreeMap { - - //TODO: declare variables. + private int size = 0; + private BodyForceTreeMapItem root = null; /** * 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. */ 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; } @@ -22,7 +52,16 @@ public class BodyForceTreeMap { * Precondition: key != null. */ 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; } @@ -30,16 +69,38 @@ public class BodyForceTreeMap { * Returns 'true' if this map contains a mapping for the specified 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; } + 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 * descending according to the mass of the bodies. */ public String toString() { - //TODO: implement method. - return null; + return toString(root, ""); } } diff --git a/src/BodyForceTreeMapItem.java b/src/BodyForceTreeMapItem.java new file mode 100644 index 0000000..a7151e2 --- /dev/null +++ b/src/BodyForceTreeMapItem.java @@ -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; + } +} diff --git a/src/Simulation3.java b/src/Simulation3.java index 36d6378..4cdb7c6 100644 --- a/src/Simulation3.java +++ b/src/Simulation3.java @@ -14,7 +14,7 @@ public class Simulation3 { public static void main(String[] args) { CodeDraw cd = new CodeDraw(); BodyLinkedList bodies = new BodyLinkedList(); - BodyForceMap forceOnBody = new BodyForceMap(); + BodyForceTreeMap forceOnBody = new BodyForceTreeMap(); Random random = new Random(2022); diff --git a/src/Vector3.java b/src/Vector3.java index 93a55e8..2a49656 100644 --- a/src/Vector3.java +++ b/src/Vector3.java @@ -94,6 +94,15 @@ public class Vector3 { * in the form "[x,y,z]", e.g., "[1.48E11,0.0,0.0]". */ 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; } }