Question

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 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

----------------------------------------------------

Program

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

//Create a class Distance
class Distance{
   //Instance variables
   String[] cities;
   int[][] distance;
   int size;
   int count;
   //default constructor
   public Distance() {
       cities=new String[10];
       size=10;
       count=0;
       distance=new int[10][10];
   }
   //Parameterized constructor
   public Distance(int sz) {
       cities=new String[sz];
       size=sz;
       count=0;
       distance=new int[sz][sz];
   }
   //Get size of the cities
   public int getSize() {
       return count;
   }
   //Get the index of city
   public int getCity(String cityName) {
       for(int i=0;i<count;i++) {
           if(cityName.equalsIgnoreCase(cities[i])) {
               return i;
           }
       }
       return -1;
   }
   //Get the city name
   public String getCity(int index) {
       if(index>=0 && index<count) {
           return cities[index];
       }
       return null;
   }
   //Add a city in to cities
   public void addCity(String city) {
       for(int i=0;i<count;i++) {
           if(city.equalsIgnoreCase(cities[i])) {
               System.out.println(city+" already in the list!!!");
               return;
           }
       }
       if(size>count) {
           cities[count]=city;
           count++;
       }
       else {
           String arr[]=new String[size*2];
           for(int i=0;i<count;i++) {
               arr[i]=cities[i];
           }
           arr[count]=city;
           cities=new String[2*size];
           cities=arr;
           count++;
           int disArr[][]=new int[2*size][2*size];
           for(int i=0;i<size;i++) {
               for(int j=0;j<size;j++) {
                   disArr[i][j]=distance[i][j];
               }
           }
           size=2*size;
       }
   }
   //Set distance
   public void setDistance(String city1, String city2, int dist) {
       boolean flag1=false,flag2=false;
       for(int i=0;i<count;i++) {
           if(city1.equalsIgnoreCase(cities[i])) {
              flag1=true;
           }
           if(city2.equals(cities[i])) {
                  flag2=true;
           }
       }
       if(flag1==false) {
           addCity(city1);
       }
       if(flag2==false) {
           addCity(city2);
       }
       int index1=getCity(city1);
       int index2=getCity(city2);
       distance[index1][index2]=dist;
   }
   //Get distance between two cities
   public int getDistance(String city1, String city2) {
       int index1=getCity(city1);
       if(index1>=0) {
           int index2=getCity(city2);
           if(index2>=0) {
               return distance[index1][index2];
           }
           else {
               return -1;
           }
       }
       else {
           return -1;
       }
   }
   //Read from a file
   public void initialize(File file) throws IOException {
       BufferedReader br = new BufferedReader(new FileReader(file));
          String line;
          while ((line = br.readLine()) != null) {
              String temp[]=line.split(" ");
              setDistance(temp[0],temp[1],Integer.parseInt(temp[2]));
          }  
   }
   //Get closest city
   public String getClosestCity(String city) {
       int min=0;
       int ind=getCity(city);
       if(ind!=-1) {
           for(int i=0;i<count;i++) {
               if(i!=ind && distance[ind][i]!=0) {
                   if(distance[ind][i]>min) {
                       min=i;  
                   }
               }
           }
           return cities[min];
       }
       else {
           return null;
       }
   }
}
public class AcmeTruckCompany {

   public static void main(String[] args) throws IOException {
       //File name
       File file=new File("C:/Users/deept/Desktop/distance.txt");
       //Create object of Distance class
       Distance distances=new Distance();
       //Read cities and distance from file
       distances.initialize(file);
       //Find distance between two cities
       System.out.println("Distance between Clearwater and Miami is "+distances.getDistance("Clearwater", "Miami"));
       System.out.println("Distance between Richmond and Mannasas is "+distances.getDistance("Richmond", "Mannasas"));
       //Find distance between two cities
       System.out.println("Closest city from Richmond is "+distances.getClosestCity("Richmond"));

   }

}

----------------------------------------------

Output

Distance between Clearwater and Miami is 131
Distance between Richmond and Mannasas is 95
Closest city from Richmond is Mannasas

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...
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 important element of this sof...

    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...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • I need help with this one method in java. Here are the guidelines. Only public Employee[]...

    I need help with this one method in java. Here are the guidelines. Only public Employee[] findAllBySubstring(String find). EmployeeManager EmployeeManager - employees : Employee[] - employeeMax : final int = 10 -currentEmployees : int <<constructor>> EmployeeManager + addEmployee( type : int, fn : String, ln : String, m : char, g : char, en : int, ft : boolean, amount : double) + removeEmployee( index : int) + listAll() + listHourly() + listSalary() + listCommision() + resetWeek() + calculatePayout() :...

  • I’m giving you code for a Class called GenericArray, which is an array that takes a...

    I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...

  • 1. Specification For this assignment, write a static class named ShortestRoute to find the shortest route...

    1. Specification For this assignment, write a static class named ShortestRoute to find the shortest route between San Francisco to New York City. (What makes it "static" is that all its members will be static.) The class will include two static recursive functions -- the first is simple, and just finds a valid route through the network, without regards to shortest distance. The second finds the shortest route. Both are explained below. 2. Create A Network Create a constant array...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • | ome Insert Design Layout References Mailings View Help Tell me what you want to do from the l U...

    | ome Insert Design Layout References Mailings View Help Tell me what you want to do from the l Unless you need to edit, it's safer to stay in 11. Write C++ statements that do the following: a. Declare an array alpha of 10 rows and 20 columns of type int. b. Initialize the array alpha to 0. c. Store 1 in the first row and 2 in the remaining rows d. Store 5 in the first column, and make...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

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