Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fix the below code or write another code, the output of the code is incorrect. All the inforamtions are below inculde the input txt

Please fix the below code or write another code, the output of the code is incorrect. All the inforamtions are below inculde the input txt file and the expected outputs. PLEASE IF YOU COULD NOT GET THE CORRECT OUTPUT DO NOT AWNSER MY QUESTION.
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
You have a map of Turkey, and you want to start from one city and finish your trip in another city. However, there may be cities you want to visit during this trip. Therefore, you have to visit these cities as well. In this question, your task is to find the shortest route from your starting city to your ending city. This route should also include the cities you want to visit. You should read Turkey map from the .txt files which shown in following image: Eskisehir, Ankara, 10 Eskisehir, Bolu, 20 Eskisehir, Bilecik, 5 Eskisehir, Kutahya, 30 Eskisehir, Afyonkarahisar, 50 Eskisehir, Cankiri, 10 Eskisehir, Kastamonu, 5 Cankiri, Kastamonu, 12 Ankara, Istanbul, 14 Istanbul, Cankiri, 30 Kastamonu, Corum, 14 Corum, Amasya, 14 Amasya, Tokat, 12 You should read this file line by line and construct an undirected graph with given weight. For example, the first line means that there is a path between Eskisehir and Ankara with length of 10. To find a shortest path, first you should read source city, destination city and number of cities which you want to visit, and name of these cities from the console, respectively. If there is more than one city to visit, you should visit them based on console order. For example, you read cities to visit from the console as A, B, C. Then, you should keep this order during your trip and first visit A, then B, then C. input.txt // you should read input toxt file from the user Eskisehir // source city Kastamonu //destination city 1 // number of city you want to visit Cankiri // City to visit Routes are: Eskisehir-Cankiri-Kastamonu Length of route is: 22 input.txt // you should read input bxt file from the user Eskisehir // source city Kastamonu //destination city 2 // number of city you want to visit Ankara // first City to visit Cankiri // second City to visit Routes are: Eskisehir-Ankara-Istanbul-Cankiri-Kastamonu Length of route is: 66 er the file name (e.g., input.txt): input_q2.txt er the source city: Eskisehir ar the destination city: Kastamonu er the number of cities you want to visit: 0 aption in thread "main" java. lang. NumberFormatexception: for input string: "Eskisehir, Ankara, 10 at Javar base/ java, lang. NumberFormatexception. forInputsting (NumberFormatException.java:67 at java, base/fava. lang.Integer.parseInt (Integer. java:668) at java.base/ java, lang. Integer.parseInt (Integer.java:786) at TurkeyTrip,reodgraphfromFild (TurkeyTrip. java:29) at Turkey Trip, main TurkeyTrip. java:106 distances, patiource, dit ) 1 ) ? ratam distances: + Int shartestlength = distases+get(destinationcity); fime currentcity = destieationcityd if (icurrentcity, equatiotsourcecit currentit ? 7 3. er the file name (e.g., input.txt): input_qz.txt er the source city: Eskisehir er the destination city: Kastamonu er the number of cities you want to visit: 0 aption in thread "main" java.lang.NumberFormatexception: For inputistring "Eskisehir,Ankara, 10 at java, base/ java. Lang . NumberFormat Exceptrion. for Inputstring (NumberFormatException.java:67) at Java, base/ Java. lang.Integer, parseInt (Integer, java:668) at fava.base/java, laing. Inteper. par seInt Integer, java:786) at Turkey Thip,readgraphfromFile (TurkeyTrip.java:29) at Turkey Trip.nain (TurkeyTrip.java:106) import java.io.File: import java.io.FileNotFoundException: import java.util."; public elass TurkeyTrip \{ public static void main(Stringl] args) throws FileNotFoundException f Scanner inputScanner = new Scanner(System in): System.out.print("Enter the filename: "); String filename = inputScanner.nextLine(): Map> graph = readinput(filename): String startCity = getAlphabeticallyFirstCitylgraph.keySet0); MapeString, Double> shortestDistances = dikstra(graph, startCity): System,out.printin("Shortest paths from " + startCity +" ) printShortestPaths(shortestDistances); ) private static String getAlphabeticallyFirstCity(SeteString> cities) ( return cities.stream().min(Comparator naturalOrder(0). orElse(null): \} private static woid printShortestPaths(Map shortestDistances) f shortestDistances.entrySet() stream() sorted(Comparator.comparing(Map.Entry=getKey)] for Each(entry 1 String city = entry.getKey(): double distance = entry.getValue 0 : if (distance == Double.POSITIVE_INFINITY) 1 System.out.printin(city +" " Unreachable"): , else ( System.out. printin(city + "i " + distance): ) Wi \} private static MapeString, Map readinput(String filename) throws FileNotFoundException \& MapeString, MapeString, Double>> graph = new HashMapes 0: Scanner scanner = new Scanner(new File(filename)): while (scanner-hasNextline()) ( String line = scanner.next Line ( : if (line.startsWith ("/I) |l line.isBlank0) \{ continue; t String [ parts = line.split( ( ) ; if (parts, length ()): graph.forEach((otherCity, otherCoordinates) 1 if (totherCity.equals(city)) \& double distance = calculateDistance (x,y, otherCocrdinates get ("xn), otherCoordinates.get ("yn)); graph.get(city)-put(otherCity, distance); graph.get(otherCity).put(city, distance): // Assuming an undirected graph \} h): ) // Sort the keys alphabetically TreeMap> sortedGraph = new TreeMap(graph); return sortedGraph; 1 private static double calculateDistance(double 1, double y1, double 2, double y2 ) ( return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); \} private static Map dijkstra(Map> graph, String startCity) \{ Map> distances = new HashMap 0; for (String city : graph.keySet()) \{ distances.put(city, Double.POSITIVE_INFINITY); \} distances.put(startCity, 0.0 ); Set visited = new HashSet ; PriorityQueue>>pq= new PriorityQueue ( Comparator.comparing(Map.Entry::getValue)): pq.offer(new AbstractMap.SimpleEntry ( startCity, 0.0)): while (!pq.isEmpty()) \{ Map.Entry> currententry = pq.poll(); String currentCity = currentEntry.getKey () ; double currentDistance = currentEntry.getValue( ) : if (visited.contains(currentCity)) \{ continue; \} visited.add(currentCity); for (Map.Entry neighborEntry : graph.get(currentCity).entrySet()) ( String neighborCity = neighborEntry.getKey(); double neighborDistance = neighborEntry.getValue(); double newDistance = currentDistance + neighborDistance; if (distances.get(neighborCity) > newDistance) \{ distances.put(neighborCity, newDistance); pq.offer(new AbstractMap.SimpleEntry

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

What lessons in OD contracting does this case represent?

Answered: 1 week ago

Question

Does the code suggest how long data is kept and who has access?

Answered: 1 week ago