In this program, you are modifying given code so that the class is object-oriented. 2. Write a...

60.1K

Verified Solution

Question

Programming

In this program, you are modifying given code so that the classis object-oriented.

2. Write a Java class called CityDistancesOO in a class file calledCityDistancesOO.java.   

3. Your class will still make use of two text files. a. The firsttext file contains the names of cities with the first line of thefile specifying how many city names are contained within the file.  

b. The second text file contains the distances between the citiesin the file described above without containing an entry for howmany distances are within the file.   



4. The CityDistancesOO class will have two class attributes: a. Thefirst attribute, called cities, is a one-dimensional array ofString containing the city names. b. The second attribute, calleddistances, is a two-dimensional array (n x n array where n is thenumber of cities) of double and organized such that each rowcorresponds to the distances from a particular city.  

5. The CityDistancesOO class contains a constructor and fournon-static methods: c. The constructor takes two arguments both oftype String. The first argument, called nameFile, is the file nameof the list of city names. The second argument, calleddistanceFile, is the file name of the list of city distances. Theconstructor then calls the helper methods loadCities() andloadDistances() to create and populate the two class attributes. d.The method called loadCities() is a helper method and should beprivate. The method takes a single argument called filename of typeString that is the name of the file containing the city names. Thismethod opens the file, reads in the data into cities attribute. Thefirst item read from the text file should be the number of citynames within the file (read as an integer). If done correctly, thecities attribute should be the correct size to store all city nameswithout any “extra” space within the array. If the file does notexist or a problem reading the file is encountered, then anIOException is thrown. Hint: Be aware that using the nextInt()method from the Scanner class will read the number but not thenewline character found after the number. Your method shouldcorrectly handle the newline character.

e. The method called loadDistances() is a helper method and shouldbe private. The method takes an argument called filename of typeString that is the name of the file containing the list ofdistances. The method opens the file, reads in the data into thedistances attribute. If the file does not exist or a problemreading the file is encountered, then an IOException isthrown.

f. A public method called getNumberOfCities() that returns thenumber of cities stored in the cities attribute.

g. A private method called getCityIndex() that takes a singleargument called cityName of type String (that is, the name of aparticular city). The method iterates through the cities attributeand returns the index of the location within the array thatcontains the value of cityName. If the string specified by cityNameis not found in cities, then the method returns the value -1.


h. A public method called getCity() that takes a single argumentcalled index of type int. This method returns the city name as aString corresponding to the provided index.

i. A public method called findDistance that takes two arguments:the first is called start of type String; and the second is calledend of type String. The method makes use of the getCityIndex()helper method to retrieve the indices of the city namescorresponding to start and end arguments. Then, the correctdistance is retrieved from the distances attribute and returned tothe caller.

Given Code:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class CityDistances {
  

public static String[] loadCities(String filename) throwsIOException{

Scanner fileIn = new Scanner(new File(filename));
int n = fileIn.nextInt();
String[] cities = new String[n];

fileIn.nextLine();

int i = 0;
while (fileIn.hasNextLine()) {
cities[i] = fileIn.nextLine();
i++;
}

fileIn.close();

return cities;

}
  
  
public static double[][] loadDistances(String filename,intnumCities) throws IOException{

double distances[][] = new double[numCities][numCities];
Scanner fileIn = new Scanner(new File(filename));

for (int i = 0; i < numCities; i++) {
for (int j = 0; j < numCities; j++) {

distances[i][j] = fileIn.nextDouble();

}

}

fileIn.close();

return distances;

}


private static int getCityIndex(String[] cities,StringcityName){
int cityIndex = -1;
  
for(int i = 0; i < cities.length ; i++){
if(cities[i].equals(cityName)){
cityIndex = i;
break;
}
}
  
return cityIndex;
}
  


public static double findDistance(String[] cities, double[][]distances,String start,String end){
  
int startCityIndex = getCityIndex(cities, start);
int endCityIndex = getCityIndex(cities, end);

if(startCityIndex!=-1 && endCityIndex!=-1){
return distances[startCityIndex][endCityIndex];
}else{
return -1;
}
}
}

Answer & Explanation Solved by verified expert
4.4 Ratings (890 Votes)
CityDistancesOOjava import javaioFile import javaioIOException import javautilScanner public class CityDistancesOO private String cities private double distances public CityDistancesOOString nameFile String distanceFile throws IOException loadCitiesnameFile    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students