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

java class file Routes.csv Link download : /////////--------- mediafire.com/file

ID: 3722875 • Letter: J

Question

java class

file Routes.csv Link download : /////////--------- mediafire.com/file/c7tl7wiwpr38b4w/Routes.csv

Using the program you developed in Program 7, add logic to accept input of a from airport and a to airport. You new code should determine and display the path of airports that represents the shortest path between the entered from and to airports.

this is code i that wrote in program 7

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.text.ParseException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

/**

* @author Lokesh Kumar

*

*/

public class Example {

public static void main(String[] args) throws ParseException, IOException {

Scanner travelScanner = null;

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader("E://Routes.csv"));

String line = null;

List airportList = new ArrayList();

while ((line = br.readLine()) != null) {

String str[] = line.split(",");

for (int i = 0; i < str.length; i++) {

airportList.add(str[1] + "," + str[2]);

}

}

System.out.println("Enter FROM & TO Airport Codes (like EGO,ASF) : ");

travelScanner = new Scanner(System.in);

String travel = travelScanner.nextLine();

String str[] = travel.split(",");

if (airportList.contains(travel)) {

System.out.println("Possible to travle " + str[0] + " To " + str[1]);

} else {

System.out.println("It's Not Possible to travle " + str[0] + " To " + str[1]);

}

} catch (Exception exception) {

br.close();

travelScanner.close();

exception.printStackTrace();

}

}

}

Explanation / Answer

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.text.ParseException;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Map;

import java.util.Scanner;

import java.util.Set;

/**

*

* @author Lokesh Kumar

*

*

*

*/

public class Example1 {

public static void main(String[] args) throws ParseException, IOException {

Scanner travelScanner = null;

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader("C:\Users\ashekhawat\Downloads\Routes.csv"));

String line = null;

Map<String, Set<String>> airportMap = new HashMap<String, Set<String>>();

while ((line = br.readLine()) != null) {

String str[] = line.split(",");

for (int i = 0; i < str.length; i++) {

if(airportMap.containsKey(str[1])) {

airportMap.get(str[1]).add(str[2]);

}else {

Set<String> set = new HashSet<>();

set.add(str[2]);

airportMap.put(str[1], set);

}

}

}

System.out.println("Enter FROM Airport Code(like EGO): ");

travelScanner = new Scanner(System.in);

String fromAirport = travelScanner.nextLine();

System.out.println("Enter to Airport Code(like EGO): ");

String toAirport = travelScanner.nextLine();

StringBuilder pathBuilder = new StringBuilder(fromAirport);

Set<String> destinations = airportMap.get(fromAirport);

String path = findPath(airportMap,destinations, toAirport);

if(path!=null && !path.isEmpty()) {

pathBuilder.append(path);

}

if (!pathBuilder.toString().isEmpty()) {

System.out.println("Possible to travle " + fromAirport + " To " + toAirport);

System.out.println("The path is: ");

System.out.println(pathBuilder.toString());

} else {

System.out.println("It's Not Possible to travle " + fromAirport + " To " + toAirport);

}

} catch (Exception exception) {

br.close();

travelScanner.close();

exception.printStackTrace();

}

}

public static String findPath(Map<String, Set<String>> airportMap, Set<String> destinations,String toAirport) {

StringBuilder pathBuilder = new StringBuilder();

if(destinations.contains(toAirport)) {

pathBuilder.append("---->").append(toAirport);

return pathBuilder.toString();

}

for(String destination:destinations) {

if(airportMap.containsKey(destination)){

String path = findPath(airportMap, airportMap.get(destination), toAirport);

if(path!=null && !path.isEmpty()) {

pathBuilder.append(path);

return pathBuilder.toString();

}

}

}

return pathBuilder.toString();

}

}