java Graph program Write a loop to keep interacting with the user until they ask
ID: 3695737 • Letter: J
Question
java Graph program Write a loop to keep interacting with the user until they ask to get out (-1) use .nextInt( ) and no need to handle exceptions if user enters text) In that loop: 1. Display the current vertex 2. display the possible neighbors from the current vertex 3. ask the user for their choice of a vertex to go to 4. if their choice is -1 then exit 5 if the user enters a value too large or too small report an out of range error 6. if their choice is a valid neighbor then change the current vertex to that choice 7.if their choice is not a valid neighbor then report the vertex they entered is unreachable o assume user will enter an int.
Explanation / Answer
import java.util.HashSet; import java.util.InputMismatchException; import java.util.Iterator; import java.util.Scanner; import java.util.Set; public class Shortest_Path_to_AllVertex { private int distances[]; private Set settled; private Set unsettled; private int number_of_nodes; private int adjacencyMatrix[][]; public Shortest_Path_to_AllVertex(int number_of_nodes) { this.number_of_nodes = number_of_nodes; distances = new int[number_of_nodes + 1]; settled = new HashSet(); unsettled = new HashSet(); adjacencyMatrix = new int[number_of_nodes + 1][number_of_nodes + 1]; } public void shortestPath(int adjacency_matrix[][], int source) { int evaluationNode; for (int i = 1; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.