/* public class NodeSet API: constructor: public NodeSet() accessors: public Node getNode(int index) mutators: public boolean addNode(Node node) misc: public int length( ) */ public class NodeSet { private Node [ ] nodes; private int nodeCount; private static final int MAX_NODES = 20; public NodeSet(){ nodes = new Node [MAX_NODES]; nodeCount = 0; } public Node getNode(int index) { if (index < nodeCount) { return nodes[index]; } System.out.println("** Error: NodeSet:getNode index out of bounds"); return null; } public int length( ) { return nodeCount; } public String toString( ) { String string = "Nodes: "; for (int i = 0; i < nodeCount; i++) { string = string + nodes[i].toString(); if (i < nodeCount - 1) { string = string + ", "; } } string = string + "."; return string; } public void addNode(Node node) { if (alreadyPresent(node)) { return; } if (nodeCount < MAX_NODES) { nodes[nodeCount] = node; nodeCount++; return; } System.out.println("Error: NodeSet capacity " + MAX_NODES + ") exceeded"); System.out.println("Error: cannot add node " + node.getName()); } private boolean alreadyPresent(Node node) { for(int i = 0; i < nodeCount; i++) { if (node.equals(nodes[i])) { return true; } } return false; } }