Question
public class StateSpeller { private StateGraph graph; /** * The constructor sets up the state graph. * * @param stateFile A file containing the state
public class StateSpeller {
private StateGraph graph;
/** * The constructor sets up the state graph. * * @param stateFile A file containing the state adjacency information * @throws FileNotFoundException If the file cannot be opened for reading */ public StateSpeller(String stateFile) throws FileNotFoundException { this.graph = new StateGraph(stateFile); }
/** * Returns true if the provided word can be spelled by starting in any state * and using only the first letters of state names. Repeated states are * allowed. For example, "mom" can be spelled by moving from Michigan to * Ohio, then back to Michigan. * * @param word The word to spell * @return true if it can be spelled, false otherwise */ public boolean isSpellableFirstLetters(String word) {
return false;
}
How would I start this method, any help would be helpful. You can use the State and StateGraph classes with this.
public class StateGraph { private HashMapstateMap; private ArrayList stateList; /** * Build a StateGraph from a file. The file format is one state per line * followed by a comma separated list of neighboring states. * * For example, the first two lines might look like the following: * * * Alaska * Alabama,Mississippi,Tennessee,Georgia,Florida ** * No state may be listed as a neighbor if it does not begin some line in * the file. * * @param fileName The name of the file containing the graph specification. * @throws FileNotFoundException If the file cannot be opened for reading. */ public StateGraph(String fileName) throws FileNotFoundException { stateMap = new HashMap(); stateList = new ArrayList (); File file = new File(fileName); Scanner scanner; String curLine; String[] curEntries; State curState; // Scan once through the file to build all of the State objects. scanner = new Scanner(file); while (scanner.hasNext()) { curLine = scanner.nextLine(); curEntries = curLine.split(","); curState = new State(curEntries[0]); stateMap.put(curEntries[0], curState); stateList.add(curState); } scanner.close(); // Scan through the file a second time to build the adjacency lists. scanner = new Scanner(file); while (scanner.hasNext()) { curLine = scanner.nextLine(); curEntries = curLine.split(","); curState = stateMap.get(curEntries[0]); for (int i = 1; i < curEntries.length; i++) { curState.addNeighbor(stateMap.get(curEntries[i])); } } scanner.close(); } /** * Return a list of all vertices in the graph. * * @return The list of vertices. */ public ArrayList getStates() { return stateList; } /** * Return the vertex with the indicated name. * * @param name The name of the desired State. * @return The requested State, or null if it does not exist. */ public State getState(String name) { return stateMap.get(name); } /** * Clear the visited flag from all states. */ public void clearVisited() { for (State state : stateList) { state.setVisited(false); } }
public class State { private ArrayListneighbors; private String name; private boolean visited; /** * Construct a vertex. (The name should be unique!) * * @param name The name associated with this vertex. */ public State(String name) { this.neighbors = new ArrayList (); this.name = name; this.visited = false; } /** * Add a neighbor to the adjacency list for this vertex. * * @param neighbor A vertex to add to the adjacency list. */ public void addNeighbor(State neighbor) { neighbors.add(neighbor); } /** * Return the adjacency list for this vertex. * * @return The list of adjacent vertices. */ public ArrayList getNeighbors() { return neighbors; } /** * Check to see if the current vertex has been visited. This value is not * set automatically -- it must be set by by calling setVisited. (This could * be useful in conducting recursive graph traversals.) * * @return True if the vertex has been visited, false otherwise. */ public boolean isVisited() { return visited; } /** * Set or un-set the visited flag for this vertex. * * @param visited New value for the visited flag. */ public void setVisited(boolean visited) { this.visited = visited; } /** * Return the name associated with this vertex. * * @return The name. */ public String getName() { return name; } @Override public String toString() { return name; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started