Question: JAVA Implement a toString method for the WeightedGraph class. Test the method in the UseGraph.java file. Package Link: https://pastebin.com/KVWhNsyJ ***** WeightedGraph.java ***** public class WeightedGraph

 JAVA Implement a toString method for the WeightedGraph class. Test the

JAVA

Implement a toString method for the WeightedGraph class. Test the method in the UseGraph.java file.

Package Link: https://pastebin.com/KVWhNsyJ

***** WeightedGraph.java *****

public class WeightedGraph implements WeightedGraphInterface {

public static final int NULL_EDGE = 0;

private static final int DEFCAP = 50; // default capacity

private int numVertices;

private int maxVertices;

private T[] vertices;

private int[][] edges;

private boolean[] marks; // marks[i] is mark for vertices[i]

public WeightedGraph()

// Instantiates a graph with capacity DEFCAP vertices.

{

numVertices = 0;

maxVertices = DEFCAP;

vertices = (T[]) new Object[DEFCAP];

marks = new boolean[DEFCAP];

edges = new int[DEFCAP][DEFCAP];

}

public WeightedGraph(int maxV)

// Instantiates a graph with capacity maxV.

{

numVertices = 0;

maxVertices = maxV;

vertices = (T[]) new Object[maxV];

marks = new boolean[maxV];

edges = new int[maxV][maxV];

}

public boolean isEmpty()

// Returns true if this graph is empty; otherwise, returns false.

{

return (numVertices == 0);

}

public boolean isFull()

// Returns true if this graph is full; otherwise, returns false.

{

return (numVertices == maxVertices);

}

public void addVertex(T vertex)

// Preconditions: This graph is not full.

// Vertex is not already in this graph.

// Vertex is not null.

//

// Adds vertex to this graph.

{

vertices[numVertices] = vertex;

for (int index = 0; index

edges[numVertices][index] = NULL_EDGE;

edges[index][numVertices] = NULL_EDGE;

}

numVertices++;

}

public boolean hasVertex(T vertex)

// Returns true if this graph contains vertex; otherwise, returns false.

{

for (int i = 0; i

if (vertices[i].equals(vertex)) // has a vertex

return true;

}

// does not have a vertex

return false;

}

private int indexIs(T vertex)

// Returns the index of vertex in vertices.

{

int index = 0;

while (!vertex.equals(vertices[index]))

index++;

return index;

}

public void addEdge(T fromVertex, T toVertex, int weight)

// Adds an edge with the specified weight from fromVertex to toVertex.

{

int row;

int column;

row = indexIs(fromVertex);

column = indexIs(toVertex);

edges[row][column] = weight;

}

public int weightIs(T fromVertex, T toVertex)

// If edge from fromVertex to toVertex exists, returns the weight of edge;

// otherwise, returns a special null-edge value.

{

int row;

int column;

row = indexIs(fromVertex);

column = indexIs(toVertex);

return edges[row][column];

}

public QueueInterface getToVertices(T vertex)

// Returns a queue of the vertices that vertex is adjacent to.

{

QueueInterface adjVertices = new LinkedQueue();

int fromIndex;

int toIndex;

fromIndex = indexIs(vertex);

for (toIndex = 0; toIndex

if (edges[fromIndex][toIndex] != NULL_EDGE)

adjVertices.enqueue(vertices[toIndex]);

return adjVertices;

}

public void clearMarks()

// Sets marks for all vertices to false.

{

for (int i = 0; i

marks[i] = false;

}

public void markVertex(T vertex)

// Sets mark for vertex to true.

{

marks[indexIs(vertex)] = true;

}

public boolean isMarked(T vertex)

// Returns true if vertex is marked; otherwise, returns false.

{

return marks[indexIs(vertex)];

}

public T getUnmarked()

// Returns an unmarked vertex if any exist; otherwise, returns null.

{

for (int i = 0; i

if (marks[i] == false) // check for unmarked vertex

return vertices[i];

// no unmarked vertices

return null;

}

public boolean edgeExists(T vertex1, T vertex2)

// Preconditions: vertex1 and vertex2 are in the set of vertices

//

// Return value = (vertex1, vertex2) is in the set of edges

{

return (edges[indexIs(vertex1)][indexIs(vertex2)] != NULL_EDGE);

}

public boolean removeEdge(T vertex1, T vertex2)

// Preconditions: vertex1 and vertex2 are in the set of vertices

//

// Return value = true if edge was in the graph (and has been removed)

// = false if edge was not in the graph

{

boolean existed = edgeExists(vertex1, vertex2);

edges[indexIs(vertex1)][indexIs(vertex2)] = NULL_EDGE;

return existed;

}

public String toString() {

return "";

}

}

Implement a toString method for the WeightedGraph class Test the method in the UseGraph.java file Here is an example output given the following graph Dallas Austin Washington Denver Atlanta Chicago Houston Dallas: Denver, 780 Austin, 200 Chicago, 900 Austin: Dallas, 200 Houston, 160 Denver: Atlanta, 1400 Chicago, 1000 Chicago: Denver, 1000 Washington: Dallas, 1300 Atlanta, 600 Atlanta: Houston, 800 Washington, 600 Houston: Atlanta, 800 Implement a toString method for the WeightedGraph class Test the method in the UseGraph.java file Here is an example output given the following graph Dallas Austin Washington Denver Atlanta Chicago Houston Dallas: Denver, 780 Austin, 200 Chicago, 900 Austin: Dallas, 200 Houston, 160 Denver: Atlanta, 1400 Chicago, 1000 Chicago: Denver, 1000 Washington: Dallas, 1300 Atlanta, 600 Atlanta: Houston, 800 Washington, 600 Houston: Atlanta, 800

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!