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...
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...
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...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text editor of your choice). This program will “explode” a String into an array of characters (char[]), and then print out the array. The String comes from the first command-line argument. Example output with the command-line argument foo is below: f o o --------------------------------------------------------------------- public class StringExplode { // TODO - write your code below this comment. // You will need to write a method...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...