You will write a Graph class. A Graph object stores an ArrayList of the vertices
ID: 3909173 • Letter: Y
Question
You will write a Graph class. A Graph object stores an ArrayList of the vertices of the graph. The Graph constructor receives the name of the input file as a parameter, opens the input file, and reads the information within the file to construct and store the graph within the Graph object. You cannot add additional data members and any nested classes to the Graph class. You may add additional methods. This is the statement you'll use to open the file: FilelnputStream fstreamnew FilelnputStream("assignment2.txt"); import java.util.LinkedList: import java.util.ArrayList public class Graph private ArrayList vertices; public Graph(String filename) public LinkedList-Vertex> topologicalSort()Explanation / Answer
Graph.java
----------------------
import java.util.LinkedList;
import java.util.ArrayList;
import java.io.*;
public class Graph
{
private ArrayList<Vertex> vertices;
public Graph(String filename)
{
vertices = new ArrayList<Vertex>();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
filename));
String line = reader.readLine();
boolean is_first = true;
while (line != null) {
System.out.println(line);
String array1[]= line.split(" ");
if(is_first == true) {
for(int i=0; i<array1.length; i++)
vertices.add(new Vertex(array1[i]));
} else {
}
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public LinkedList<Vertex> topologicalSort()
{
return null;
}
}
Vertex.java
--------------------------------
import java.util.LinkedList;
import java.util.Iterator;
public class Vertex
{
private String name;
private LinkedList<Edge> edges;
public Vertex()
{
edges = new LinkedList<Edge>();
}
public Vertex(String name)
{
this.name = name;
edges = new LinkedList<Edge>();
}
public void setName(String n)
{
this.name = n;
}
public String getName()
{
return name;
}
public void addEdge(Edge e)
{
this.edges.add(e);
}
public Iterator<Edge> getEdgesIterator()
{
return edges.iterator();
}
}
Edge.java
----------------------
public class Edge
{
private Vertex adjacentVertex;
public Edge()
{
}
public Edge(Vertex v)
{
this.adjacentVertex = v;
}
public void setAdjacentVertex(Vertex v)
{
this.adjacentVertex = v;
}
public Vertex getAdjacentVertex()
{
return adjacentVertex;
}
}
Assignment2.java
----------------------------
public class Assignment2 {
public static void main(String[] args) {
Graph g = new Graph("assignment2.txt");
}
}
assignment2.txt
---------------------
A B C D E
A B
A D
B C
D C
E A
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.