The code below contains some defined methods (things I think you might be unfamiliar with, e.g., Iterators and Maps). Most of the methods contain a
// TO DO
comment, which means you need to replace with the correct implementation!
import java.util.Iterator;import java.util.Map;import java.util.HashMap;import java.util.List;import java.util.ArrayList;/** * Default implementation of a GraphVertex that uses * an adjacency list representation. * @author astjohn * */public class DefaultGraphVertex implements GraphVertex { // static variable to assign unique ids private static int ID_counter = 0; /** * Reset the ID counter to 0. */ public static void resetIDCounter() { ID_counter = 0; } // instance variables private int ID; // ID of this vertex private Map<String,Object> attributes; // map to store attributes private List<GraphVertex> neighbors; // neighbor list, stored in sorted ID order /** * Create a new vertex, assigning it the next id value. */ public DefaultGraphVertex() { // assign this vertex the current ID value ID = ID_counter; // increment for the next vertex ID_counter++; // initialize the attributes map attributes = new HashMap<String,Object>(); // initialize the neighbors neighbors = new ArrayList<GraphVertex>(); } /** * Get the ID of this vertex. */ public int getID() { // TO DO return -1; } /** * Add/update an attribute. */ public void setAttribute( String name, Object value ) { attributes.put( name, value ); } /** * Get the value for an attribute; null if the attribute is * not set. */ public Object getAttribute( String name ) { return attributes.get( name ); } /** * Get all attribute names, as an Iterator<String>. */ public Iterator<String> getAttributeNames() { return attributes.keySet().iterator(); } /** * Add a neighbor to this vertex. */ public void addNeighbor( GraphVertex neighbor ) { // TO DO // walk the list to perform insertion sort } /** * Check if the vertex with the given ID is a neighbor. */ public boolean hasNeighbor( int neighborID ) { // TO DO } /** * Remove vertex with the given ID from neighbors; * does nothing if not a neighbor. */ public void removeNeighbor( int neighborID ) { // TO DO } /** * Get the neighbors for this vertex. */ public Iterator<GraphVertex> getNeighbors() { return neighbors.iterator(); } /** * Get the number of neighbors for this vertex. */ public int numNeighbors() { // TO DO return -1; } /** * Return a String representation of this vertex * (ID, attributes, neighbors), as in * (1,{key1:value1,...},(2,3)) */ public String toString() { String result = "(" + Integer.toString( ID ) + ","; // add attributes result += attributesToString(); // add neighbors result += "," + neighborIDsString(); return result + ")"; } /** * Returns a String of neighbor IDs in increasing order, as in "(0,3)". */ public String neighborIDsString() { // String for neighbor IDs String neighborIDs = "("; // for each neighbor for ( int i = 0; i < neighbors.size(); i++ ) neighborIDs += Integer.toString( neighbors.get(i).getID() ) + ","; // if there was at least one neighbor, remove extra comma if ( neighborIDs.length() > 0 ) neighborIDs = neighborIDs.substring(0,neighborIDs.length() - 1); // add the last ) neighborIDs += ")"; return neighborIDs; } /** * Return a String representation of the attributes, as in * "{key1:value1,...}". */ public String attributesToString() { // for the resulting string String result = "{"; // get the names of the existing attributes Iterator<String> attributeNames = getAttributeNames(); // track the current attribute name String currentName; // while there are attribute names left while (attributeNames.hasNext()) { // store the current attribute name currentName = attributeNames.next(); // add to the string result += currentName + ":" + getAttribute(currentName).toString() + ","; } // if there was at least one attribute, remove trailing comma if ( result.length() > 1 ) // remove the last character result = result.substring(0, result.length() - 1); // return the generated string return result; }}