Question: I have compilation errors that I'm struggling with in my code, please can you help me fix the errors. I'm trying to make a road

I have compilation errors that I'm struggling with in my code, please can you help me fix the errors. I'm trying to make a road map, where each place is a vertex and each road between two places is an edge. Each edge stores a number indicating the length of the road. Assume that there is at most one road directly connecting two given places, so that there is at most one edge between two vertices. The map is loaded in via a text file that starts with a line that contains the number of vertices and the number of edges. After the line mentioned, there are two sections of content. The initial section holds details regarding the vertices, where each line represents a single vertex. The information stored for each vertex includes its name and the availability of charging stations, represented as an integer with 1 indicating availability and 0 indicating unavailability. The second section stores information about the edges, with each line corresponding to one edge. Each line contains three numbers: the first two are 0-based indices of its two incident vertices within the places array; the last number is the edge length. An example of the map input would look like this as a txt file:

7 9 B 0 A 0 C 1 D 1 E 1 F 0 G 0 0 1 1 1 2 10 0 3 5 2 3 8 2 4 1 3 4 6 3 5 4 3 6 2 5 6 1 These are the errors i'm getting. I have compilation errors that I'm struggling with in my code, please

can you help me fix the errors. I'm trying to make aroad map, where each place is a vertex and each road betweentwo places is an edge. Each edge stores a number indicating thelength of the road. Assume that there is at most one roaddirectly connecting two given places, so that there is at most oneedge between two vertices. The map is loaded in via a textfile that starts with a line that contains the number of verticesand the number of edges. After the line mentioned, there are twosections of content. The initial section holds details regarding the vertices, whereeach line represents a single vertex. The information stored for each vertex

import java.util.*; import java.io.*;

class Vertex {

// Constructor: set name, chargingStation and index according to given values, // initilaize incidentRoads as empty array public Vertex(String placeName, boolean chargingStationAvailable, int idx) { name = placeName; incidentRoads = new ArrayList(); index = idx; chargingStation = chargingStationAvailable; }

public String getName() { return name; }

public boolean hasChargingStation() { return chargingStation; }

public ArrayList getIncidentRoads() { return incidentRoads; }

// Add a road to the array incidentRoads public void addIncidentRoad(Edge road) { incidentRoads.add(road); }

public int getIndex() { return index; }

private String name; // Name of the place private ArrayList incidentRoads; // Incident edges private boolean chargingStation; // Availability of charging station private int index; // Index of this vertex in the vertex array of the map }

class Edge { public Edge(int roadLength, Vertex firstPlace, Vertex secondPlace) { length = roadLength; incidentPlaces = new Vertex[] { firstPlace, secondPlace }; }

public Vertex getFirstVertex() { return incidentPlaces[0]; }

public Vertex getSecondVertex() { return incidentPlaces[1]; }

public int getLength() { return length; }

private int length; private Vertex[] incidentPlaces; }

// A class that represents a sparse matrix public class RoadMap {

// Default constructor public RoadMap() { places = new ArrayList(); roads = new ArrayList(); }

// Auxiliary function that prints out the command syntax public static void printCommandError() { System.err.println("ERROR: use one of the following commands"); System.err.println(" - Load a map and print information:"); System.err.println(" java RoadMap -i "); System.err.println(" - Load a map and determine if two places are connnected by a path with charging stations:"); System.err.println(" java RoadMap -c "); System.err.println(" - Load a map and determine the mininmum number of assistance cars required:"); System.err.println(" java RoadMap -a "); }

public static void main(String[] args) throws Exception { if (args.length == 2 && args[0].equals("-i")) { RoadMap map = new RoadMap(); try { map.loadMap(args[1]); } catch (Exception e) { System.err.println("Error in reading map file"); System.exit(-1); }

System.out.println(); System.out.println("Read road map from " + args[1] + ":"); map.printMap(); System.out.println(); } else if (args.length == 2 && args[0].equals("-a")) { RoadMap map = new RoadMap(); try { map.loadMap(args[1]); } catch (Exception e) { System.err.println("Error in reading map file"); System.exit(-1); } System.out.println(); System.out.println("Read road map from " + args[1] + ":"); map.printMap();

int n = map.minNumAssistanceCars(); System.out.println(); System.out.println("The map requires at least " + n + " assistance car(s)"); System.out.println(); } else if (args.length == 4 && args[0].equals("-c")) { RoadMap map = new RoadMap(); try { map.loadMap(args[1]); } catch (Exception e) { System.err.println("Error in reading map file"); System.exit(-1); } System.out.println(); System.out.println("Read road map from " + args[1] + ":"); map.printMap();

int startVertexIdx = -1, endVertexIdx = -1; try { startVertexIdx = Integer.parseInt(args[2]); endVertexIdx = Integer.parseInt(args[3]); } catch (NumberFormatException e) { System.err.println("Error: start vertex and end vertex must be specified using their indices"); System.exit(-1); }

if (startVertexIdx = map.numPlaces()) { System.err.println("Error: invalid index for start vertex"); System.exit(-1); }

if (endVertexIdx = map.numPlaces()) { System.err.println("Error: invalid index for end vertex"); System.exit(-1); }

Vertex startVertex = map.getPlace(startVertexIdx); Vertex endVertex = map.getPlace(endVertexIdx); if (!map.isConnectedWithChargingStations(startVertex, endVertex)) { System.out.println(); System.out.println("There is no path connecting " + map.getPlace(startVertexIdx).getName() + " and " + map.getPlace(endVertexIdx).getName() + " with charging stations"); } else { System.out.println(); System.out.println("There is at least one path connecting " + map.getPlace(startVertexIdx).getName() + " and " + map.getPlace(endVertexIdx).getName() + " with charging stations"); } System.out.println();

} else { printCommandError(); System.exit(-1); } }

// Task 1: Load the map from a text file public void loadMap(String filename) { File file = new File(filename); places.clear(); roads.clear();

try { Scanner sc = new Scanner(file);

// Read the first line: number of vertices and number of edges int numVertices = sc.nextInt(); int numEdges = sc.nextInt();

for (int i = 0; i

// Add your code here to create a new vertex using the information above and add // it to places Vertex vertex = new Vertex(placeName, hasChargingStataion, 0); places.add(vertex); }

for (int j = 0; j

// Add your code here to create a new edge using the information above and add // it to roads // You should also set up incidentRoads for each vertex Edge edge = new Edge(length, vtx1, vtx2); roads.add(edge);

// Set up incidentRoads for each vertex vtx1.incidentRoads.add(edge); vtx2.incidentRoads.add(edge); }

sc.close();

// Add your code here if approparite } catch (Exception e) { e.printStackTrace(); places.clear(); roads.clear(); } }

// Task 2: Check if two vertices are connected by a path with charging stations on each itermediate vertex. // Return true if such a path exists; return false otherwise. // The worst-case time complexity of your algorithm should be no worse than O(v + e), // where v and e are the number of vertices and the number of edges in the graph. public boolean isConnectedWithChargingStations(Vertex startVertex, Vertex endVertex) { // Sanity check if (startVertex.getIndex() == endVertex.getIndex()) { return true; }

// The following return statement is just a placeholder. // Update the code to correctly determine whether the tow vertices are connected by a path with charing stations

// Initialize a set of visited vertices Set visited = new HashSet();

// Initialize a stack for DFS traversal Stack stack = new Stack();

// Push the starting vertex onto the stack stack.push(startVertex);

while (!stack.empty()) { // Pop the top vertex from the stack Vertex currVertex = stack.pop();

// Check if this vertex has a charging station if (!currVertex.hasChargingStation()) { continue; }

// Check if we've reached the end vertex if (currVertex.getIndex() == endVertex.getIndex()) { return true; }

// Add this vertex to the visited set visited.add(currVertex);

// Check all the incident roads of this vertex for (RoadMap road : currVertex.incidentRoads) { // Get the other vertex connected by this road Vertex nextVertex = road.getSecondVertex(currVertex);

// Check if we've already visited this vertex if (visited.contains(nextVertex)) { continue; }

// Push this vertex onto the stack stack.push(nextVertex); } }

// We've exhausted all paths and haven't found a path with charging stations return false; }

// Task 3: Determine the mininum number of assistance cars required public int minNumAssistanceCars() { // Add your code here to compute and return the minimum number of assistance cars required for this map int numAssistanceCars = 0; boolean[] visited = new boolean[numVertices];

for (int i = 0; i

private void bfs(int start, boolean[] visited) { Queue queue = new LinkedList(); queue.offer(start); visited[start] = true;

while (!queue.isEmpty()) { int vertex = queue.poll(); for (int i = 0; i

public void printMap() { System.out.println("The map contains " + this.numPlaces() + " places and " + this.numRoads() + " roads"); System.out.println();

System.out.println("Places:");

for (Vertex v : places) { System.out.println("- name: " + v.getName() + ", charging station: " + v.hasChargingStation()); }

System.out.println(); System.out.println("Roads:");

for (Edge e : roads) { System.out.println("- (" + e.getFirstVertex().getName() + ", " + e.getSecondVertex().getName() + "), length: " + e.getLength()); } }

public void printPath(ArrayList path) { System.out.print("( ");

for (Vertex v : path) { System.out.print(v.getName() + " "); }

System.out.println(")"); }

public int numPlaces() { return places.size(); }

public int numRoads() { return roads.size(); }

public Vertex getPlace(int idx) { return places.get(idx); }

private ArrayList places; private ArrayList roads; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!