Implement AB6, Aufgabe 1+2
This commit is contained in:
		@@ -1,13 +1,13 @@
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
 | 
			
		||||
public class Aufgabe6Test {
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testEP2() {
 | 
			
		||||
 | 
			
		||||
        /* //TODO: uncomment for testing
 | 
			
		||||
        NamedBody sun1, mercury1, venus1, earth1, moon1, mars1, deimos1, phobos1, vesta1, pallas1, hygiea1, ceres1;
 | 
			
		||||
 | 
			
		||||
        // create the same 12 named body-force pairs
 | 
			
		||||
@@ -70,7 +70,7 @@ public class Aufgabe6Test {
 | 
			
		||||
            set2.add(m);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        assertTrue(set1.equals(set2));
 | 
			
		||||
        assertEquals(set1, set2);
 | 
			
		||||
 | 
			
		||||
        MassiveLinkedList list = map.getKeys().toList();
 | 
			
		||||
        while (list.size() > 0) {
 | 
			
		||||
@@ -112,7 +112,5 @@ public class Aufgabe6Test {
 | 
			
		||||
            count++;
 | 
			
		||||
        }
 | 
			
		||||
        assertEquals(12, count);
 | 
			
		||||
 | 
			
		||||
        */ //TODO: uncomment
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -103,10 +103,10 @@ public class BodyForceTreeMap {
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        return toString(root);
 | 
			
		||||
        return (root != null) ? toString(root) : "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private class Item {
 | 
			
		||||
    private static class Item {
 | 
			
		||||
        private final Body key;
 | 
			
		||||
        private Vector3 value;
 | 
			
		||||
        private Item parent;
 | 
			
		||||
 
 | 
			
		||||
@@ -222,7 +222,7 @@ public class BodyLinkedList implements Iterable<Body> {
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private class Item {
 | 
			
		||||
    private static class Item {
 | 
			
		||||
        private final Body body;
 | 
			
		||||
        private Item prev;
 | 
			
		||||
        private Item next;
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ import codedraw.CodeDraw;
 | 
			
		||||
 * and an arbitrary number of subsystems (of type 'CosmicSystem') in its orbit.
 | 
			
		||||
 * This class implements 'CosmicSystem'.
 | 
			
		||||
 */
 | 
			
		||||
public class HierarchicalSystem implements CosmicSystem {
 | 
			
		||||
public class HierarchicalSystem implements CosmicSystem, MassiveIterable {
 | 
			
		||||
 | 
			
		||||
    private final NamedBodyForcePair central;
 | 
			
		||||
    private CosmicSystem[] orbit;
 | 
			
		||||
@@ -127,4 +127,53 @@ public class HierarchicalSystem implements CosmicSystem {
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public MassiveIterator iterator() {
 | 
			
		||||
        return new MassiveIterator() {
 | 
			
		||||
            private int i = 0;
 | 
			
		||||
            private MassiveIterator cur = null;
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public Massive next() {
 | 
			
		||||
                if (cur != null && cur.hasNext()) return cur.next();
 | 
			
		||||
 | 
			
		||||
                for (; i < all.length; i++) {
 | 
			
		||||
                    CosmicSystem sys = all[i];
 | 
			
		||||
                    if (sys instanceof NamedBodyForcePair m) {
 | 
			
		||||
                        i++;
 | 
			
		||||
                        return m.getBody();
 | 
			
		||||
                    } else if (sys instanceof HierarchicalSystem hs) {
 | 
			
		||||
                        cur = hs.iterator();
 | 
			
		||||
                        if (cur.hasNext()) {
 | 
			
		||||
                            i++;
 | 
			
		||||
                            return cur.next();
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public boolean hasNext() {
 | 
			
		||||
                if (cur != null && cur.hasNext()) return true;
 | 
			
		||||
 | 
			
		||||
                for (; i < all.length; i++) {
 | 
			
		||||
                    CosmicSystem sys = all[i];
 | 
			
		||||
                    if (sys instanceof NamedBodyForcePair) {
 | 
			
		||||
                        return true;
 | 
			
		||||
                    } else if (sys instanceof HierarchicalSystem hs) {
 | 
			
		||||
                        cur = hs.iterator();
 | 
			
		||||
                        if (cur.hasNext()) {
 | 
			
		||||
                            i++;
 | 
			
		||||
                            return true;
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,12 @@
 | 
			
		||||
import codedraw.CodeDraw;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A map that associates an object of 'Massive' with a Vector3. The number of key-value pairs
 | 
			
		||||
 * is not limited.
 | 
			
		||||
 */
 | 
			
		||||
// TODO: define further classes and methods for the binary search tree and the implementation
 | 
			
		||||
//  of MassiveSet, if needed.
 | 
			
		||||
public class MassiveForceTreeMap {
 | 
			
		||||
 | 
			
		||||
    // TODO: define missing parts of this class.
 | 
			
		||||
public class MassiveForceTreeMap implements MassiveSet {
 | 
			
		||||
    private int size = 0;
 | 
			
		||||
    private Item root;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Adds a new key-value association to this map. If the key already exists in this map,
 | 
			
		||||
@@ -14,7 +14,37 @@ public class MassiveForceTreeMap {
 | 
			
		||||
     * Precondition: key != null.
 | 
			
		||||
     */
 | 
			
		||||
    public Vector3 put(Massive key, Vector3 value) {
 | 
			
		||||
        // TODO: implement method.
 | 
			
		||||
        if (root == null) {
 | 
			
		||||
            root = new Item(key, value);
 | 
			
		||||
            size++;
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Item item = root;
 | 
			
		||||
        while (item != null) {
 | 
			
		||||
            if (item.key.equals(key)) {
 | 
			
		||||
                Vector3 old = item.value;
 | 
			
		||||
                item.value = value;
 | 
			
		||||
                return old;
 | 
			
		||||
            } else if (item.key.mass() > key.mass()) {
 | 
			
		||||
                if (item.left != null) {
 | 
			
		||||
                    item = item.left;
 | 
			
		||||
                } else {
 | 
			
		||||
                    item.setLeft(new Item(key, value));
 | 
			
		||||
                    size++;
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                if (item.right != null) {
 | 
			
		||||
                    item = item.right;
 | 
			
		||||
                } else{
 | 
			
		||||
                    item.setRight(new Item(key, value));
 | 
			
		||||
                    size++;
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -24,7 +54,16 @@ public class MassiveForceTreeMap {
 | 
			
		||||
     * Precondition: key != null.
 | 
			
		||||
     */
 | 
			
		||||
    public Vector3 get(Massive key) {
 | 
			
		||||
        // TODO: implement method.
 | 
			
		||||
        Item item = root;
 | 
			
		||||
        while (item != null) {
 | 
			
		||||
            if (item.key.equals(key)) {
 | 
			
		||||
                return item.value;
 | 
			
		||||
            } else if (item.key.mass() > key.mass()) {
 | 
			
		||||
                item = item.left;
 | 
			
		||||
            } else {
 | 
			
		||||
                item = item.right;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -33,17 +72,36 @@ public class MassiveForceTreeMap {
 | 
			
		||||
     * Precondition: key != null
 | 
			
		||||
     */
 | 
			
		||||
    public boolean containsKey(Massive key) {
 | 
			
		||||
        // TODO: implement method.
 | 
			
		||||
        Item item = root;
 | 
			
		||||
        while (item != null) {
 | 
			
		||||
            if (item.key.equals(key)) {
 | 
			
		||||
                return true;
 | 
			
		||||
            } else if (item.key.mass() > key.mass()) {
 | 
			
		||||
                item = item.left;
 | 
			
		||||
            } else {
 | 
			
		||||
                item = item.right;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private String toString(Item item) {
 | 
			
		||||
        String s = "";
 | 
			
		||||
        if (item == null) {
 | 
			
		||||
            return s;
 | 
			
		||||
        }
 | 
			
		||||
        s += this.toString(item.right);
 | 
			
		||||
        s += String.format("{%s: %s}\n", item.key, item.value);
 | 
			
		||||
        s += this.toString(item.left);
 | 
			
		||||
        return s;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns a readable representation of this map, in which key-value pairs are ordered
 | 
			
		||||
     * descending according to 'key.getMass()'.
 | 
			
		||||
     */
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        // TODO: implement method.
 | 
			
		||||
        return "";
 | 
			
		||||
        return (root != null) ? toString(root) : "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -51,10 +109,126 @@ public class MassiveForceTreeMap {
 | 
			
		||||
     * elements of the returned `MassiveSet` object also affects the keys in this tree map.
 | 
			
		||||
     */
 | 
			
		||||
    public MassiveSet getKeys() {
 | 
			
		||||
        // TODO: implement method.
 | 
			
		||||
        return null;
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void draw(CodeDraw cd) {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public MassiveIterator iterator() {
 | 
			
		||||
        return new MassiveIterator() {
 | 
			
		||||
            private Item next = root.getLeftLeaf();
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public Massive next() {
 | 
			
		||||
                if (next == null) return null;
 | 
			
		||||
                Massive m = next.key;
 | 
			
		||||
                Item newNext = (next.right != null) ? next.right.getLeftLeaf() : next.parent;
 | 
			
		||||
                while (newNext != null && newNext.right == next) {
 | 
			
		||||
                    next = newNext;
 | 
			
		||||
                    newNext = newNext.parent;
 | 
			
		||||
                }
 | 
			
		||||
                next = newNext;
 | 
			
		||||
                return m;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public boolean hasNext() {
 | 
			
		||||
                return next != null;
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean contains(Massive element) {
 | 
			
		||||
        return containsKey(element);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void remove(Massive element) {
 | 
			
		||||
        Item item = root;
 | 
			
		||||
        while (item != null) {
 | 
			
		||||
            if (item.key.equals(element)) {
 | 
			
		||||
                Item newP = null;
 | 
			
		||||
                if (item.left != null) {
 | 
			
		||||
                    newP = item.left.getRightLeaf();
 | 
			
		||||
                } else if (item.right != null) {
 | 
			
		||||
                    newP = item.right.getLeftLeaf();
 | 
			
		||||
                }
 | 
			
		||||
                if (item.parent.left == item) {
 | 
			
		||||
                    item.parent.setLeft(newP);
 | 
			
		||||
                } else {
 | 
			
		||||
                    item.parent.setRight(newP);
 | 
			
		||||
                }
 | 
			
		||||
                size--;
 | 
			
		||||
                return;
 | 
			
		||||
            } else if (item.key.mass() > element.mass()) {
 | 
			
		||||
                item = item.left;
 | 
			
		||||
            } else {
 | 
			
		||||
                item = item.right;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
//TODO: Define additional class(es) implementing the binary search tree and the implementation
 | 
			
		||||
// of MassiveSet (either here or in a separate file).
 | 
			
		||||
    @Override
 | 
			
		||||
    public void clear() {
 | 
			
		||||
        size = 0;
 | 
			
		||||
        root = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int size() {
 | 
			
		||||
        return size;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public MassiveLinkedList toList() {
 | 
			
		||||
        MassiveLinkedList list = new MassiveLinkedList();
 | 
			
		||||
        for (Massive m : this) {
 | 
			
		||||
            list.addLast(m);
 | 
			
		||||
        }
 | 
			
		||||
        return list;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static class Item {
 | 
			
		||||
        private final Massive key;
 | 
			
		||||
        private Vector3 value;
 | 
			
		||||
        private Item parent;
 | 
			
		||||
        private Item left;
 | 
			
		||||
        private Item right;
 | 
			
		||||
 | 
			
		||||
        public Item(Massive key, Vector3 value) {
 | 
			
		||||
            this.key = key;
 | 
			
		||||
            this.value = value;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void setLeft(Item left) {
 | 
			
		||||
            this.left = left;
 | 
			
		||||
            if (left != null) left.parent = this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void setRight(Item right) {
 | 
			
		||||
            this.right = right;
 | 
			
		||||
            if (right != null) right.parent = this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Item getLeftLeaf() {
 | 
			
		||||
            Item cur = this;
 | 
			
		||||
            while (cur.left != null) {
 | 
			
		||||
                cur = cur.left;
 | 
			
		||||
            }
 | 
			
		||||
            return cur;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Item getRightLeaf() {
 | 
			
		||||
            Item cur = this;
 | 
			
		||||
            while (cur.right != null) {
 | 
			
		||||
                cur = cur.right;
 | 
			
		||||
            }
 | 
			
		||||
            return cur;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -98,7 +98,7 @@ public class MassiveLinkedList implements Iterable<Massive> {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        Massive m = last.body;
 | 
			
		||||
        last = last.next;
 | 
			
		||||
        last = last.prev;
 | 
			
		||||
        if (last != null) last.setNext(null);
 | 
			
		||||
        size--;
 | 
			
		||||
        return m;
 | 
			
		||||
@@ -194,7 +194,7 @@ public class MassiveLinkedList implements Iterable<Massive> {
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private class Item {
 | 
			
		||||
    private static class Item {
 | 
			
		||||
        private final Massive body;
 | 
			
		||||
        private Item prev;
 | 
			
		||||
        private Item next;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user