Question: PLEASE MAKE SURE THE ANSWER CAN PASS THE TEST CODE Program.java This class, as its name suggests, will be the main heart of the program.
PLEASE MAKE SURE THE ANSWER CAN PASS THE TEST CODE
Program.java
This class, as its name suggests, will be the main heart of the program. It will be the entry point of the program, read in a file of cities and create objects for each of them, contain the array of those cities, and create a CompressedArray containing the distances between each of the cities read in from the file.
The class must have the following private variables:
- cityCount (int)
- cityArray (City[])
- array (CompressedArray)
The class must have the following methods:
- public Program(String, boolean) [constructor]
- Takes in a String representing the file to load (i.e. "cities1.txt") and a boolean (showMap) that indicates whether or not the map GUI should be displayed
- Initialize cityArray with 3 cells
- Create an object of MyFileReader or use your own code to read in a text file, and load in the file with the given name.
- Read in each line from the file and create a City object containing the city name, and the x and y values from the file (look at the text file to see this format). Add the city object to the cityArray and expand the capacity if needed.
- If the boolean (showMap) is true, then create a Map object and call addCity() on the Map object for each city in the cityArray. This will add the marker icons to the map for each city.
- public City[] getCityList()
- Returns cityArray
- private void expandCapacity()
- Expands the capacity of cityArray by adding 3 additional slots to the array
- public double distBetweenCities(City,City)
- Calculates the Euclidean distance between the two given Cities
- public void compareDistances()
- Create a 2D double array (i.e. double[][]) with a size of N by N, where N is the number of cities in cityArray. Loop through every combination of pairs of cities and call distBetweenCities() for each pair. Save this result into the double[][] array in the appropriate cell.
- public CompressedArray getArray()
- Returns array
TEST CODE
public class TestProgram {
public static void main(String[] args) { String[] files = new String[] { "cities1.txt", "cities2.txt", "cities3.txt", "cities4.txt", "cities5.txt" }; int[] sizes = new int [] { 21, 15, 10, 15, 36 }; String[] solns = new String[] { " 36.88 441.59 441.44 420.49 421.01 22.20 396.09 399.24 57.70 37.20 96.54 117.34 531.75 510.19 483.50 179.23 176.82 264.62 244.25 223.79 273.31 ", " 551.88 377.00 191.27 457.96 94.25 111.80 98.09 455.81 278.93 362.35 134.53 652.92 466.59 561.43 205.83 ", " 269.11 91.35 257.26 802.24 718.88 712.53 589.42 480.42 504.12 239.15 ", " 372.31 420.49 64.41 98.05 278.93 324.04 658.98 306.54 245.60 561.06 111.33 466.59 519.70 205.83 762.13 ", " 512.66 552.89 42.05 372.31 157.18 191.27 420.49 95.52 133.24 64.41 98.05 415.28 455.81 278.93 324.04 396.09 129.03 163.60 28.16 37.20 301.38 96.54 603.83 643.29 458.33 510.19 192.94 483.50 179.23 334.43 375.20 203.30 244.25 81.22 223.79 273.31 " }; double[] firsts = new double[] { 36.87817782917155, 551.881327823292, 269.1059270993487, 372.3130403303113, 512.6646077115134 };
for (int f = 0; f < files.length; f++) { String file = files[f]; Program prog = new Program(file, false); prog.compareDistances(); CompressedArray ca = prog.getArray();
boolean t1 = ca.getLength() == sizes[f]; boolean t2 = ca.toString().equals(solns[f]); boolean t3 = ca.getElement(0) == firsts[f];
if (t1 && t2 && t3) { System.out.println("Test " + (f+1) + " Passed"); } else { System.out.println("Test " + (f+1) + " Failed"); } } }
}
Cities1.txt
city name,x,y Calgary 367 661 Edmonton 375 625 Montreal 802 737 Ottawa 780 740 Toronto 750 762 Vancouver 273 683 Winnipeg 546 670
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
