## Java Assignment Details

The file townpairs.dat contains selected city pairs and their distances. Each line of the file contains 2 city names and a distance. Write a program to calculate the shortest path between any given city pair.

## How to Solve Such Java Problems

We obviously have to look at the demands, before we discover Java assignment solution. As we do everything according to the requirements, it is really important to find a proper approach. So, our first aim is to recognize the absolutely suitable scheme which we can use in a specific condition because in programming there are plenty of ways to do the same thing. Then we start creating a pseudo-code which has the overall map of the whole program. We need to think ahead of time if we want to avoid failure. And only then we can start working on implementing actual code.

## Step by Step Algorithm

- Collect city information.
- Find out shortest path and distance between two cities.
- Display path on the window.

## Java Assignment Solution from Our Expert Team

- Read in the city pairs and distances.
- Implement Dijkstra’s algorithm using as the edge cost the given city pair’s distance.
- Map the cities onto the window and draw the shortest path in the window between those cities. A list of (X,Y) coordinates for each of the cities can be found in the file townxy.txt.
- Display in the window the path length of the shortest path.This exercise will consider the “traveling salesperson problem” (TSP).

## Java Code

To read a file and store city information in the queues.

Read townxy file and set city's XY. while not townxy.isEmpty() do town = cityxy.readTown() ; townList.add(city); endwhile

Read town pair file and map them to the object.

while not townpair.isEmpty() do town1 = townxy.readTown1() ; town2 = townxy.readTown2() ; dist = townxy.readTownDistance() ; edgeList.add(town1,town2,dist); endwhile

Implement Dijkstra’s algorithm.

Generate graph using node and edges. While not unSettledNodes.size() > 0 do node = getMinimum(unSettledNodes); settledNodes.add(node); unSettledNodes.remove(node); findMinimalDistances(node); endwhile

Display in the window the path.

For i = 1 to pathlength do v1 = path.get(i); v2 = path.get(i + 1); g.drawString(v1.getName(), v1.getX(), v1.getY()); g.drawLine(v1.getX(), v1.getY(), v2.getX(), v2.getY()); endfor