Question
Help creating these class in Java Inputs are formated such as: USA 20 30 ************************************************************************************************************************************* TEST FILES: ****************************************************************1******************************************************************** public class TestCompressedArray { public static void
Help creating these class in Java
Inputs are formated such as:
USA
20
30
*************************************************************************************************************************************
TEST FILES:
****************************************************************1********************************************************************
public class TestCompressedArray {
public static void main(String[] args) {
double[][] origArray1 = new double[][] { new double[] {15.2, 43.6, 28.2, 35.0, 95.5}, new double[] {29.5, 88.3, 72.1, 44.4, 37.4}, new double[] {10.8, 16.4, 74.6, 91.7, 36.2}, new double[] {87.8, 12.2, 63.7, 19.6, 73.1}, new double[] {13.9, 25.0, 77.4, 97.4, 30.5}, }; CompressedArray arr1 = new CompressedArray(origArray1);
// --------------------------------------------------------- // Test 1 - getLength() on a CompressedArray object // --------------------------------------------------------- if (arr1.getLength() == 10) { System.out.println("Test 1 Passed"); } else { System.out.println("Test 1 Failed"); }
// --------------------------------------------------------- // Test 2 - toString() method on a CompressedArray // --------------------------------------------------------- String soln = " 29.50 10.80 16.40 87.80 12.20 63.70 13.90 25.00 77.40 97.40 ";
if (arr1.toString().equals(soln)) { System.out.println("Test 2 Passed"); } else { System.out.println("Test 2 Failed"); }
double[][] origArray2 = new double[][] { new double[] {5.8, 10.4, -4.7, 13.7}, new double[] {10.4, 7.1, -19.3, 4.8}, new double[] {-4.7, -19.3, 5.1, 17.2}, new double[] {13.7, 4.8, 17.2, -6.7} };
double[][] origArray3 = new double[][] { new double[] {5.8, 12.3, -11.6, 21.3}, new double[] {10.4, 7.1, 0.2, 35.0}, new double[] {-4.7, -19.3, 5.1, 18.4}, new double[] {13.7, 4.8, 17.2, -6.7} }; double[][] origArray4 = new double[][] { new double[] {10.4, 7.1, 0.2, 35.0}, new double[] {-4.7, -19.3, 5.1, 18.4}, new double[] {13.7, 4.8, 17.2, -6.7}, new double[] {5.8, 12.3, -11.6, 21.3}, };
CompressedArray arr2 = new CompressedArray(origArray2); CompressedArray arr3 = new CompressedArray(origArray3); CompressedArray arr4 = new CompressedArray(origArray4);
// --------------------------------------------------------- // Test 3 - getLength() on more CompressedArray objects // --------------------------------------------------------- if (arr2.getLength() == 6 && arr3.getLength() == 6 && arr4.getLength() == 6) { System.out.println("Test 3 Passed"); } else { System.out.println("Test 3 Failed"); } // --------------------------------------------------------- // Test 4 - equals() between two CompressedArray objects // --------------------------------------------------------- if (!arr2.equals(arr4) && !arr3.equals(arr4)) { System.out.println("Test 4 Passed"); } else { System.out.println("Test 4 Failed"); } // --------------------------------------------------------- // Test 5 - equals() between two CompressedArray objects // ---------------------------------------------------------
if (arr2.equals(arr3)) { System.out.println("Test 5 Passed"); } else { System.out.println("Test 5 Failed"); }
} } ************************************************************2************************************************************************
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
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"); } } }
}
Classes to implement For this assignment, you must implement 3 Java classes: City, CompressedArray, and Program. Follow the guidelines for each one below. In all of these classes, you can implement more private (helper) methods, if you want to, but you may not implement more public methods. You may not add instance variables other than the ones specified below. Penalties will be applied if you implement additional instance variables or change the variable types or modifiers than what is described here. City.java This class represents each city that is loaded in from a text file. It is a simple class that just contains the city's name, x and y coordinates, and a CityMarker icon. The class must have the following private variables: name (String) x (int) y(int) marker (CityMarker) The class must have the following public methods: public City(String, int, int) [constructor] o Takes in parameters for name, x, and y and assigns these values to the corresponding instance variables. The marker variable must also be initialized in this constructor as a new City Marker object. public String getName() o Returns name public int getX() o Returns x public int getY0 o Returns y public City Marker getMarker() o Returns marker public void setName(String) o Sets name public void setX(String) o Sets x public void setY(String) o Sets y public void setMarker(CityMarker) o Sets marker public String toString() o Returns the city name CompressedArray.java This class represents the array that has been compressed from a matrix (2D array) into a linear array that excludes elements from the diagonal and any elements above or to the right of the diagonal. Only elements below or to the left of the matrix diagonal must be included in the CompressedArray. The class must have the following private variables: origArray Size (int) array (double() The class must have the following public methods: public CompressedArray(double[]0) [constructor] o Takes in a 2D double array (double(0) which represents the original array" o Initialize the linear double array (instance variable array) and copy the elements from the original array into this linear array so that it contains only the lower-left triangle elements of the original array (the elements to the left and below the diagonal). The elements from this triangle must be added to the CompressedArray in order from left to right and top to bottom. o Hint: Read the description near the top of this document to determine the required capacity for this CompressedArray. public int getLength() o Returns the length of the new, compressed array public double getElement(int) o Returns the element in the new, compressed array stored at the given index public boolean equals(CompressedArray) o Checks equality between the two CompressedArray objects by checking if they have the same length and that all the elements are identical in the same order public String toString() o Builds a string that contains the CompressedArray and formats it in a trianglular structure to look like the lower left corner of a matrix. Each element should take up exactly 8 characters and show 2 decimal places. o Hint: Use String.format("%8.2f", element) for each element and remember to add a newline at the correct places 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 CompressedArray(String, boolean) [constructor] o Takes in a String representing the file to load (i.e. "cities 1.txt") and a boolean (showMap) that indicates whether or not the map GUI should be displayed o Initialize cityArray with 3 cells o 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. o 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. o 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() o Returns cityArray private void expandCapacity (int) o Expands the capacity of cityArray by adding 3 additional slots to the array public double distBetweenCities(City City) o Calculates the Euclidean distance between the two given Cities public void compare Distances() o Create a 2D double array (i.e. double(0) 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 distBetweenCit ) for each pair. Save this result into the double | array in the appropriate cell. public CompressedArray getArray() o Returns arrayStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started