Question

Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods...

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.

Homework Answers

Answer #1

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;

}

}

Know the answer?
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
Similar Questions
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from a text file containing a series of course grades (a value between 0.0 and 100.0) with one grade entry per line. However, the first line in the file is an integer value specifying how many grade entries are contained in the file. 3. The Grades class contains four static methods: a. A method called loadGrades() that opens the file, reads in the data and...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”}
Write a Java part code to do the following   : ** Suppose a file called infile stored...
Write a Java part code to do the following   : ** Suppose a file called infile stored in drive D: ,filled with five integers(1000,200,3030,40 and 500) Read  the integers from infile then store them in array called ar[] and print it elements. Method called search( ) to print the location of   value 500. Method called sort() to sort array elements. Build another file called outfile in drive D: to save the sorted array elements . Note : Use ArrayIndexOutOfBoundsException when you use array...
Write in C++. Define a class called Text whose objects store lists of words. The class...
Write in C++. Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dy- namic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different implementation. Each symbol table will contain as a key a word read from a text file and as a value the number of times that word occurs in the text file. Have the program fill the first symbol table with these counts, keeping track of how long that takes using a Stopwatch object. It then does the same thing with the second symbol table....
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice)....
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold: Enter first integer: 3 Enter second integer: 4 Enter third integer: 5 Product: 60 More details about the method you need to write are...
Java... Write a class named TestScores. The class constructor should accept an array of test scores...
Java... Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to...
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method....
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma. TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed...
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed to this method is the name of a file that will be read in the method. Each line in the file contains an order number (integer), isbn number (character array), and amount ordered (integer). If an order number is on a line, it is guaranteed that there will be an isbn number and amount ordered to go along with it. Create an input file...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT