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

From Intro to Java Programming Comprehensive Version 10th edition, by Daniel Lia

ID: 3695989 • Letter: F

Question

From Intro to Java Programming Comprehensive Version 10th edition, by Daniel Liang: (Find shortest paths) Write a program that reads a connected graph from a file. The graph is stored in a file using the same format specified in Programming Exercise 29.9. Your program should prompt the user to enter the name of the file then two vertices, and should display a shortest path between the two vertices. For example, for the graph in Figure 29.23, a shortest path between 0 and 1 can be displayed as 0 2 4 3 1.

Sample of the run:

Explanation / Answer

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package vertices;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

/**

*

* @author RAM

*/

public class Main {

    /**

     * @param args the command line arguments

     */  

    public static void main(String[] args) throws FileNotFoundException {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter a file name to read: ");

    File file = new File(input.nextLine());

    if (!file.exists())

    {

        System.out.println("Corresponding File does not exist");

    }

    System.out.print("Enter two vertices (integer indexes): ");

    int vertex1 = input.nextInt();

    int vertex2 = input.nextInt();

    Scanner inFile = new Scanner(file);

    // Read the number of vertices

    String s = inFile.nextLine();

    int numberOfVertices = Integer.parseInt(s);

    System.out.println("The number of vertices is " + numberOfVertices);

    ArrayList<WeightedEdge> list = new ArrayList<WeightedEdge>();

    while (inFile.hasNext())

    {

        String [] tokens = null;

        s = inFile.nextLine();

        int u = Integer.parseInt(tokens[0].trim());

        int v = Integer.parseInt(tokens[1].trim());

        int w = Integer.parseInt(tokens[2].trim());

    }

}

}

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