QuestionQuestion

You are creating a map of a state. There are several communities that are joined by roads of varying lengths. You need to develop a program that will print out the shortest distance between all the different communities

1. The program shall graphically prompt the user for a file.
2. The program shall read the selected file of which the first line will be a “-” separated list of the communities.
3. The program shall then read each line in the rest of the file containing an integer (the length of the road) and the 2 communities it connects separated by “-”.
4. The program shall calculate the shortest distance between each pair of communities
5. The program shall have output to a file the pairs and the shortest distance between them
6. The program shall write the output to a file in the same directory as the input file.
7. comment is part of this program requirement.

Additional Elements - Error handling, unit tests, input checking if needed.

Were good design principles should be in the construction of the program.

This is just a sample input file the program should have more inputs.

Anoka-St. Paul-Minneapolis-Big Lake
30-Minneapolis-Anoka
20-Minneapolis-St. Paul
40-Big Lake-Minneapolis
30-St. Paul-Anoka

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author
*/
public class MyMap {

    private ArrayList<MyNode> mns;
    private int[][] d;

    public MyMap(File file) {
       mns = new ArrayList<>();
       readFile(file);
    }

    /**
    * *
    * 5. The program shall have output to a file the pairs and the shortest
    * distance between them
    *
    * @return
    */
    public String getResult() {
       String result = "";
       floy();
       ArrayList<String> al = new ArrayList<>();
       for (int i = 0; i < d.length; i++) {
            int[] is = d[i];
            for (int j = 0; j < i; j++) {
                int k = is[j];
                String str = mns.get(i).getName()
                        + "-" + mns.get(j).getName() + "-" + k;
                al.add(str);
            }
       }
       Collections.sort(al);
       for (String string : al) {
            result += string + System.lineSeparator();
       }
      
       return result;
    }

    /**
    * 4. The program shall calculate the shortest distance between each pair of
    * communities
    * using Floy Algorithm
    */
    private void floy() {
       if (mns.isEmpty()) {
            return;
       }

       d = new int[mns.size()][mns.size()];
       /*
       for (MyNode mn : mns) {
            System.out.println(mn.getName());
       }      
         */
       for (int i = 0; i < d.length; i++) {
            int[] is = d[i];
            for (int j = 0; j < is.length; j++) {
                is[j] = getDistance(i, j);               
            }
       }
       int n = d.length;
       for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                   if (d[j][i] + d[i][k] < d[j][k]) {
                        d[j][k] = d[j][i] + d[i][k];
                   }
                }
            }
       }
    }

    /**
    * get distance between 2 nodes
    * @param i
    * @param j
    * @return
    */
    private int getDistance(int i, int j) {
       MyNode n = mns.get(i);
       MyNode m = mns.get(j);
       return n.distanceTo(m);
    }...
$64.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats