Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java language I need to do this program and i have it started but have no idea h

ID: 3572677 • Letter: J

Question

Java language I need to do this program and i have it started but have no idea how to finish it? can someone help me out and the first link is the program homework and the second link is the file for the program:

http://mypccourse.com/cosc2436/homework/homework7.html

http://mypccourse.com/cosc2436/interstate.txt

the compiler im using is textpad

This is what i have so far

import java.io.*;
import java.util.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.alg.*;

public class map
{
   public static void main (String[] args) throws IOException
   {
       File file = new File("interstate.txt");
       Scanner infile = new Scanner(file);

       SimpleWeightedGraph g =
       new SimpleWeightedGraph(DefaultWeightedEdge.class);

       String Input;
       while (infile.hasNext())
       {
           Input = infile.nextLine();
           if(Input.length() > 0 && Input.charAt(0) == 'I')
           {
               String[] parts = (Input.split(" "));
               System.out.println(parts[0]);
               g.addVertex(parts[1]);
               g.addVertex(parts[3]);
               g.addEdge(parts[1],parts[3]);
               g.setEdgeWeight(g.getEdge(parts[1], parts[3]), Double.parseDouble(parts[5]));
           }
       }
       // test graph
       System.out.println(g.vertexSet());
   }
}

Explanation / Answer

I have written a simple example program that can load and work with the graph that represents the interstate highway system. The example program contains the code for three simple classes.

The first class is a Vertex class, which represents a city and the road segments that run outward from that city.

The second class is a simple Edge class that represents edges. In this application Edges are directed. Since Edges are stored in Vertices, we will always know what city the Edge comes from. The only thing the Edge itself needs to represent is the Vertex the Edge goes to and the length of the road segment that this Edge represents.

Finally, the graph is represented as a collection of Vertices. Since we will very often need to look up Vertices by their city names, the Vertices are actually stored in a Map that uses the city names as keys and the Vertices themselves as values.

For convenience, the Graph class contains a method that reads the graph in from a text file containing a list of edges. Since each line in the input file will contain a pair of city names and a distance, the readFrom() method will read those two city names and try to loop up the corresponding vertices in the map. If no Vertex currently exists for a city, readFrom() will create the Vertex and add it to the map under that city name. Once readFrom() has Vertex objects for each of the two cities for the edge, it will add an Edge object to each of the two Vertex objects that makes it possible to travel between the two cities.

The Graph class also contains a method getEdgesFrom() that allows us to look up the Vertex for a particular city and get a list of all of the Edges running outward from that city. The main method uses this method to allow the user to enter a city name and see a list of all of the cities that are directly connected to that city.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote