public class A3Program { /* * this program reads in pairs of nodes on separate lines * and creates a directed graph where the pair of nodes are the * edges and all nodes have some edge */ public void start() { Graph graph = inputGraph(); dotifyGraph(graph); } private void dotifyGraph(Graph graph) { NodeSet nodes = graph.getNodeSet(); EdgeSet edges = graph.getEdgeSet(); EdgeSet outgoingEdges; System.out.println("digraph G {"); for (int i = 0; i < nodes.length(); i++) { outgoingEdges = graph.getOutgoingEdges(nodes.getNode(i)); System.out.print(outgoingEdges.toString()); } System.out.println("}"); System.out.println("# nodes = " + nodes.length()); System.out.println("# edges = " + edges.length()); } private Graph inputGraph() { Graph graph = new Graph(); for (Edge edge = inputEdge(); edge != null; edge = inputEdge()) { graph.addEdge(edge); } return graph; } private Edge inputEdge() { Node source; Node target; Edge edge; source = inputNode(); if (source == null) { return null; // no more edges } target = inputNode(); if (target == null) { // error: odd number of nodes System.out.println("Error: odd number of nodes"); System.out.println("Error: last node was: " + source.toString()); return null; } return new Edge(source, target); } private Node inputNode() { String id; String input = Keyboard.readInput(); while (input != null) { id = input.trim(); if (id != null && id.length() > 0) { return(new Node(id)); } input = Keyboard.readInput(); } return null; } }