Question

City2 cty 1 Dstance Submit Dstance between two cities City 1 Check DistanceHere are sample distance data you may use for testing 1 23 4 5 6 78 9 10 CITY 1 Clearwater 0 159 247 131 197 296 262 106 53 2The Acme Trucking company has hired you to write software to help dispatch its trucks. One important element of this software is knowing the distance between any two cities that it services. Design and implement a Distance class that stores the distances between cities in a two-dimensional array. This class contains the following required data members and methods: Required Data Members: String[] cities; //it is used to store city names int[][] distance; // this 2-D array repreents distance between two cities, the index in each dimension //map the index in the cities String array, as explained below: Given String[] cities={"Mannasas", "JavaCenter","Richmond"}; Assume distance between Mannasas and JavaCenter is 6, between Mannasas and Richmond is 95, then we have distance[0][1]=6; distance[0][2]=95; Required methods: Non-parameter constructor, initialize citites with 10 elements, and distance array(think about the size of this 2-D array). Constructor with single int parameter to specify the size of the cities array (as well as distance array-think about the size again!) public int getSize(); returns the size of the cities String array(its length) public int getCity(String cityName); this method returns the index of "cityName" in the cities String array, if not found returns -1 public String getCity(int index); it returns the city name in the cities String array, if not found, returns null public void addCity(String city); add a city to the cities String array if the city with the same name does not exist in the cities array. If the cities array is full, you need to expand the cities array (hint, you may want to double its size if is full). public void setDistance(String city1, String city2, int dist); set (or update) distance between these two cities. If any of the two cities does not exist in the cities array, you need to add that city to the cities array first, then set the distance in the 2-D distance array. public int getDistance(String city1, String city2); returns the distance between this two cities if a direct route exists, returns -1 if direct route between these two cities does not exists. public void initialize(File file) throws Exception; it reads data from a file to add cities and distances between cities in the file. The data fiel contains multiple lines and each line represents two cities and the distance betgween these two cities: For example: Richmond Mannasas 95 Clearwater Gainesville 131 Clearwater Miami 131 You may assume the name of all cities in the testing cases is a single word. public String getClosestCity(String city); returns the closest city if a direct route exists, otherwise returns null. Here are sample distance data you may use for testing. Development Platform Please use wholelearning.com for this assignment. Assignment folder is created on wholelearning. Once completed, upload all files (both source code (.java) and class(.class) and any other files, images, for example) through this assignment link. Please DO NOT zip files. Once completed, you may test your program with Grader.class. Please refer to the instructions on wholelearning.com Grading Rubric Projects that are not turned in correctly according to the assignment spec (naming conventions, project packaging, etc.) will be penalized 10% for the first infraction, or 25% for each following infraction. The following is the grading rubric for this assignment: Program compiles correctly and has all required features (classes, methods, etc): 100 points Absence of ANY of the classes or methods requested: -10 points per infraction Addition of features not required by program (or extra credit): -10 points per infraction Program compiles correctly but does not run correctly or crashes: -30 -50 points, depending on severity of the error(s) Program does not compile: -60 points Extra Credit: For up to 15 additional points, make the program very interactive, by providing a menu with options that allow you to add cities to the array, remove cities from the array, and any other operations you think are appropriate for this problem. You must incorporate them into the UML diagram as well. Please turn in the new diagrams with your project as a PDF file (name of the file doesn't matter).

text file.....cityData.txt


Richmond Mannasas 95
Clearwater Gainesville 131
Clearwater Miami 131
Clearwater Orlando 106
Clearwater Tampa 22
Clearwater Jacksonville 197
Jacksonville A 789
A B 256
Jacksonville B 59
A C 56
A Orlando 609


With GUI Interface and It should take the data of new city if it is not there in the list.

City2 cty 1 Dstance Submit Dstance between two cities City 1 Check Distance
Here are sample distance data you may use for testing 1 23 4 5 6 78 9 10 CITY 1 Clearwater 0 159 247 131 197 296 262 106 53 22 2 Daytona Beach 159 230 97 89 309 255 54 181 138 247 230 3 Ft. Lauderdale 0 309 317 80 26 206 200 232 4 Gainesville 131 97 309 068 372 332 109 177 128 5 Jacksonville 197 89 317 680 396 342 134 237 190 6 Key La 296|309| 80|372|396| 0| 54|269|243| 287 7 Miami 262 25526 332 342 540 229 214 246 8 Orlando 106 54 206 109 134 269 229 0 128 85 177 237 9 Sarasota 10 Tampa 53 181 200 243 214 128 052 22 138 232 128 190 287 246 85 52 0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
import java.io.*;

public class CityDistance{

   //it is used to store city names
   String[] cities;

   // this 2-D array repreents distance between two cities,
   // the index in each dimension
   //map the index in the cities String array,
   // as explained below:
   // Given String[] cities={"Mannasas", "JavaCenter","Richmond"}; Assume distance between Mannasas and JavaCenter is 6, between Mannasas and Richmond is 95, then we have distance[0][1]=6; distance[0][2]=95;
   int[][] distance;
   int lastFillIndex = 0;


   // Non-parameter constructor, initialize citites with 10 elements,
   // and distance array(think about the size of this 2-D array).
   CityDistance(){
       cities = new String[10];
       distance = new int[10][10];
       for(int i=0;i<10;i++){
           for(int j=0;j<10;j++){
               distance[i][j] = -1;
           }
           distance[i][i] = 0;
       }
   }
   // Constructor with single int parameter to specify the
   // size of the cities array (as well as distance array-
   // think about the size again!)
   CityDistance(int size){
       cities = new String[size];
       distance = new int[size][size];
   }
   // returns the size of the cities String array(its length)
   public int getSize(){
       return cities.length;
   }

   //this method returns the index of "cityName" in the cities String array,
   // if not found returns -1
   public int getCity(String cityName){
       int ind = -1;
       for(int i=0;i<cities.length;i++){
           if(cities[i].equals(cityName)){
               ind = i; break;
           }
       }
       return ind;
   }

   // it returns the city name in the cities String array,
   // if not found, returns null
   public String getCity(int index){
       String city = null;
       if(index>=0 && index<cities.length)
           city = cities[index];
       return city;
   }

   // add a city to the cities String array if the city with the same name
   // does not exist in the cities array.
   // If the cities array is full, you need to expand the cities array
   // (hint, you may want to double its size if is full).
   public void addCity(String city){
       if(getCity(city) != -1) return;
       if(lastFillIndex==cities.length){
           String[] newCities = new String[2*cities.length];
           int[][] newDistance = new int[2*cities.length][2*cities.length];
           for(int i=0;i<cities.length;i++){
               newCities[i] = cities[i];
               for(int j=0;j<cities.length;j++){
                   newDistance[i][j] = distance[i][j];
               }
           }
           cities = newCities;
       }
       cities[lastFillIndex] = city;
       lastFillIndex++;
   }

   // set (or update) distance between these two cities.
   // If any of the two cities does not exist in the cities array
   // you need to add that city to the cities array first
   // then set the distance in the 2-D distance array
   public void setDistance(String city1, String city2, int dist){
       int ind1 = getCity(city1);
       if(ind1==-1){
           addCity(city1);
           ind1 = getCity(city1);
       }
       int ind2 = getCity(city2);
       if(ind2==-1){
           addCity(city2);
           ind2 = getCity(city2);
       }
       distance[ind1][ind2] = dist;
       distance[ind2][ind1] = dist;
   }
  
   // returns the distance between this two cities if a
   // direct route exists, returns -1 if direct route
   // between these two cities does not exists.
   public int getDistance(String city1, String city2){
       int ind1 = getCity(city1);
       int ind2 = getCity(city2);
       if(ind1== -1 || ind2== -1) return -1;
       return distance[ind1][ind2];
   }

   // it reads data from a file to add cities and distances
   // between cities in the file. The data fiel contains multiple
   // lines and each line represents two cities and the distance
   // betgween these two cities:
   // For example: Richmond Mannasas 95 Clearwater Gainesville 131
   // Clearwater Miami 131
   // You may assume the name of all cities in the testing cases is a single word.
   public void initialize(File file) throws Exception{
       FileInputStream inDataStream = new FileInputStream(file.getName());
       Scanner s = new Scanner(inDataStream);
       // BufferedReader br = new BufferedReader(new FileReader(file));
   String city1, city2;
   int dist;
   while(s.hasNext()) {
   city1 = s.next();
   city2 = s.next();
   dist = s.nextInt();
   setDistance(city1, city2, dist);
   }
   }
  
   // returns the closest city if a direct route exists
   // otherwise returns null.
   public String getClosestCity(String city){
       int ind1 = getCity(city);
       if(ind1== -1) return null;
       int dist = 999999999;
       int bestCity = -1;
       for(int j=0;j<cities.length;j++){
           if(j!=ind1 && distance[ind1][j]!=-1 && distance[ind1][j]<dist){
               dist = distance[ind1][j];
               bestCity = j;
           }
       }
       if(dist == 999999999) return null;
       return cities[bestCity];
   }
}

Add a comment
Know the answer?
Add Answer to:
The Acme Trucking company has hired you to write software to help dispatch its trucks. One important element of this sof...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Acme Trucking company has hired you to write software to help dispatch its trucks. One...

    The Acme Trucking company has hired you to write software to help dispatch its trucks. One important element of this software is knowing the distance between any two cities that it services. Design and implement a Distance class that stores the distances between cities in a two-dimensional array. This class contains the following required data members and methods: Required Data Members: String[] cities; //it is used to store city names int[][] distance; // this 2-D array repreents distance between two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT