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

Example: Source Destination route# distance A B AB1 12.3 A B AB2 13.1 A B AB3 13

ID: 3714211 • Letter: E

Question

Example:

Source   Destination route# distance

A                B                     AB1     12.3

A               B                     AB2     13.1

A               B                      AB3    13.3

A               C                      AC1       3.4

A              C                       AC2       3.6

A              C                       AC3       4.5

Sample update file:

Route             Speed

AB1                     23.3

AB2                     28.0

AB3                     34.4

AC1                     16.5

AC2                     15.4

AC3                      45.6

Explanation / Answer

Hi,

I am uploading the files on the link below. Just change the file path in Java class according to the path where you put the files on your system.

https://drive.google.com/drive/folders/1R5GTwDsZiHCIpAR59z7mKbx-qZG3OzGH?usp=sharing

In case the above link doesn't work, find the code below :

//-----------------------------------------------------------------------------------------------------------------------------

//[ MostEfficientRouteFinder.java]

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class MostEfficientRouteFinder {

   private static final String DISTANCE_FILE            = "C:\Users\shash\Desktop\chegg\20180423\chegg2\distanceFile.txt";
   private static final String UPDATE_FILE             = "C:\Users\shash\Desktop\chegg\20180423\chegg2\updateFile.txt";
   private static final String DISTANCE_FILE_DELIMITER = "\s+";
   private static final String UPDATE_FILE_DELIMITER    = "\s+";
  
   public static void main(String[] args) {
      
       Map<String, Double> distanceMap = new HashMap<>();
       Map<String, Double> updateMap = new HashMap<>();
      
       try(BufferedReader buff = new BufferedReader(new FileReader(DISTANCE_FILE))){
           String temp;
           while((temp = buff.readLine()) != null) {
               String[] arr = temp.trim().split(DISTANCE_FILE_DELIMITER);
               if(arr.length == 4) {
                   String route = arr[2];
                   Double distance = Double.parseDouble(arr[3]);
                   distanceMap.put(route, distance);
               }
           }
       }catch(IOException e) {
           e.printStackTrace();
       }
       try(BufferedReader buff = new BufferedReader(new FileReader(UPDATE_FILE))){
           String temp;
           while((temp = buff.readLine()) != null) {
               String[] arr = temp.trim().split(UPDATE_FILE_DELIMITER);
               if(arr.length == 2) {
                   String route = arr[0];
                   Double speed = Double.parseDouble(arr[1]);
                   updateMap.put(route, speed);
               }
           }
       }catch(IOException e) {
           e.printStackTrace();
       }
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter source:");
       String source = sc.nextLine().toUpperCase().trim();
       System.out.println("Enter destination:");
       String destination = sc.nextLine().toUpperCase().trim();
       sc.close();
      
       Map<String, Double> timeTakenMap = new HashMap<>();
      
       String srcDest = source + destination;
       String destSrc = destination + source;
      
       for(String key : updateMap.keySet()) {
           if(key.startsWith(srcDest) || key.startsWith(destSrc)) {
              
               if(distanceMap.containsKey(key)) {
                  
                   Double distance = distanceMap.get(key);
                   Double speed = updateMap.get(key);
                   Double time = distance / speed;
                  
                   timeTakenMap.put(key, time);
               }
           }
       }
      
       String fastestRoute = "";
       Double leastTime = 0.0;
       boolean isTimeSet = false;
      
       for( String key : timeTakenMap.keySet()) {
           if(!isTimeSet) {
               leastTime = timeTakenMap.get(key);
               fastestRoute = key;
               isTimeSet = true;
           }
           if(timeTakenMap.get(key) < leastTime) {
               fastestRoute = key;
               leastTime = timeTakenMap.get(key);
           }
       }
       System.out.println("************************************************************");
       System.out.println("The most efficient route is " + fastestRoute);
       System.out.println("************************************************************");
   }
}


//-----------------------------------------------------------------------------------------------------------------------------------------------

//[distanceFile.txt]

A B AB1 12.3
A B AB2 13.1
A B AB3 13.3
A C AC1 3.4

//----------------------------------------------------------------------------------------------------------------------------------------------

//[updateFile.txt]

AB1 23.3
AB2 28.0
AB3 34.4
AC1 16.0
AC2 15.4
AC3 45.6
A C AC2 3.6
A C AC3 4.5

//------------------------------------------------------------------------------------------------------------------------------------------------------------

//All Done!!! Cheers!!!!

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