Write a Java class called CityDistances in a class file called
CityDistances.java.
2. Your methods will make use of two text files. a. The first text
file contains the names of cities. However, the first line of the
file is a number specifying how many city names are contained
within the file. For example,
5
Dallas
Houston
Austin
Nacogdoches
El Paso
b. The second text file contains the distances between the cities
in the file described above. This file does not contain an entry
for how many distances are within the file. If, as in the example
above, there are five (5) city names in the first text file, then
this text file should contain 52 = 25 distance entries. The first
entry should always be zero (since Dallas is a distance of zero
miles from itself). The second entry is the distance from Dallas to
Houston, and the third entry is the distance from Dallas to Austin,
etc.
3. The CityDistances class contains four static methods: a. A
public method called loadCities() that opens the file, reads in the
data and returns a one-dimensional array of String containing the
city names stored in the file. The method takes a single argument
called filename of type String that is the name of the file to be
opened. 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 returned array 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.
b. A public method called loadDistances() that opens the file,
reads in the data and returns a two-dimensional array of double
containing the city distances stored in the file. The method takes
an argument called filename of type String that is the name of the
file to be opened and an argument called numCities of type int
corresponding to the number of cities that were listed in the text
file read by loadCities(). If done correctly, the returned
two-dimensional array should be an n x n array where n is the
number of cities represented and organized such that each row
corresponds to the distances from a particular city. If the file
does not exist or a problem reading the file is encountered, then
an IOException is thrown.
If you have any doubts, please give me comment....
import java.io.*;
import java.util.Scanner;
public class CityDistances {
public static String[] loadCities(String fname) throws IOException {
Scanner fileIn = new Scanner(new File(fname));
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 fname, int numCities) throws IOException {
double distances[][] = new double[numCities][numCities];
Scanner fileIn = new Scanner(new File(fname));
for (int i = 0; i < numCities; i++) {
for (int j = 0; j < numCities; j++) {
distances[i][j] = fileIn.nextInt();
}
}
fileIn.close();
return distances;
}
}
Get Answers For Free
Most questions answered within 1 hours.