In: Computer Science
In this program, you are modifying given code so that the class
is object-oriented.
2. Write a Java class called CityDistancesOO in a class file called
CityDistancesOO.java.
3. Your class will still make use of two text files. a. The first
text file contains the names of cities with the first line of the
file specifying how many city names are contained within the file.
b. The second text file contains the distances between the cities
in the file described above without containing an entry for how
many distances are within the file.
4. The CityDistancesOO class will have two class attributes: a. The
first attribute, called cities, is a one-dimensional array of
String containing the city names. b. The second attribute, called
distances, is a two-dimensional array (n x n array where n is the
number of cities) of double and organized such that each row
corresponds to the distances from a particular city.
5. The CityDistancesOO class contains a constructor and four
non-static methods: c. The constructor takes two arguments both of
type String. The first argument, called nameFile, is the file name
of the list of city names. The second argument, called
distanceFile, is the file name of the list of city distances. The
constructor then calls the helper methods loadCities() and
loadDistances() to create and populate the two class attributes. d.
The method called loadCities() is a helper method and should be
private. The method takes a single argument called filename of type
String that is the name of the file containing the city names. This
method opens the file, reads in the data into cities attribute. The
first item read from the text file should be the number of city
names within the file (read as an integer). If done correctly, the
cities attribute should be the correct size to store all city names
without any “extra” space within the array. If the file does not
exist or a problem reading the file is encountered, then an
IOException is thrown. Hint: Be aware that using the nextInt()
method from the Scanner class will read the number but not the
newline character found after the number. Your method should
correctly handle the newline character.
e. The method called loadDistances() is a helper method and should
be private. The method takes an argument called filename of type
String that is the name of the file containing the list of
distances. The method opens the file, reads in the data into the
distances attribute. If the file does not exist or a problem
reading the file is encountered, then an IOException is
thrown.
f. A public method called getNumberOfCities() that returns the
number of cities stored in the cities attribute.
g. A private method called getCityIndex() that takes a single
argument called cityName of type String (that is, the name of a
particular city). The method iterates through the cities attribute
and returns the index of the location within the array that
contains the value of cityName. If the string specified by cityName
is not found in cities, then the method returns the value -1.
h. A public method called getCity() that takes a single argument
called index of type int. This method returns the city name as a
String 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 called
end of type String. The method makes use of the getCityIndex()
helper method to retrieve the indices of the city names
corresponding to start and end arguments. Then, the correct
distance is retrieved from the distances attribute and returned to
the caller.
Given Code:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class CityDistances {
public static String[] loadCities(String filename) throws
IOException{
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,int
numCities) 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,String
cityName){
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;
}
}
}
/***************************************CityDistancesOO.java******************************/
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class CityDistancesOO {
private String[] cities;
private double[][] distances;
public CityDistancesOO(String nameFile, String distanceFile) throws IOException {
loadCities(nameFile);
loadDistances(distanceFile,
10);
}
public String[] loadCities(String filename) throws IOException {
Scanner fileIn = new Scanner(new
File(filename));
int n = fileIn.nextInt();
cities = new String[n];
fileIn.nextLine();
int i = 0;
while (fileIn.hasNextLine())
{
cities[i] =
fileIn.nextLine();
i++;
}
fileIn.close();
return cities;
}
public int getNumberOfCities() {
return cities.length;
}
public String getCity(int index) {
return cities[index];
}
public double[][] loadDistances(String filename, int numCities) throws IOException {
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,
String cityName) {
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;
}
}
}
Please let me know if you have any doubt or modify the answer, Thanks :)