import java.util.*; public class AmoebaFamily { private Amoeba myRoot = null; // A constructor that starts an Amoeba family with an amoeba // with the given name. public AmoebaFamily (String name) { myRoot = new Amoeba (name, null); } // Add a new amoeba named childName as the youngest child // of the amoeba named parentName. // Precondition: the amoeba family contains an amoeba named parentName. public void addChild (String parentName, String childName) { // You supply this code. } // Print the names of all amoebas in the family. // Names should appear in preorder, with children's names // printed oldest first. // Members of the family constructed with the main program above // should be printed in the following sequence: // Amos McCoy, mom/dad, me, Mike, Bart, Lisa, Homer, Marge, // Bill, Hilary, Fred, Wilma public void print ( ) { // You supply this code. } // Return an iterator of the amoeba family. public Iterator allAmoebas ( ) { return new AmoebaIterator ( ); } public static void main (String [ ] args) { AmoebaFamily family = new AmoebaFamily ("Amos McCoy"); family.addChild ("Amos McCoy", "mom/dad"); family.addChild ("Amos McCoy", "auntie"); family.addChild ("mom/dad", "me"); family.addChild ("mom/dad", "Fred"); family.addChild ("mom/dad", "Wilma"); family.addChild ("me", "Mike"); family.addChild ("me", "Homer"); family.addChild ("me", "Marge"); family.addChild ("Mike", "Bart"); family.addChild ("Mike", "Lisa"); family.addChild ("Marge", "Bill"); family.addChild ("Marge", "Hilary"); System.out.println ("Here's the family:"); family.print ( ); /* System.out.println (""); System.out.println ("Here it is again!"); Iterator iter = family.allAmoebas ( ); while (iter.hasNext ( )) { System.out.println (iter.next ( )); } */ } public class AmoebaIterator implements Iterator { // Amoebas in the family are enumerated in preorder, // with children enumerated oldest first. // Members of the family constructed with the main program above // should be enumerated in the following sequence: // Amos McCoy, mom/dad, me, Mike, Bart, Lisa, Homer, Marge, // Bill, Hilary, Fred, Wilma // Complete enumeration of a family of N amoebas should take // O(N) operations. // You will supply the details of this class in a future lab. public AmoebaIterator ( ) { } public boolean hasNext ( ) { return false; } public Amoeba next ( ) { return null; } public void remove ( ) { // not used } } // end of AmoebaIterator nested class private static class Amoeba { public String myName; // amoeba's name public Amoeba myParent; // amoeba's parent public ArrayList myChildren; // amoeba's children public Amoeba (String name, Amoeba parent) { myName = name; myParent = parent; myChildren = new ArrayList ( ); } public String toString ( ) { return myName; } public Amoeba parent ( ) { return myParent; } // Add an amoeba with the given name as this amoeba's youngest child. public void addChild (String childName) { Amoeba child = new Amoeba (childName, this); myChildren.add (child); } } } // closes definition of AmoebaFamily class