Question: I need these 3 methods please! These are premade (do not change) classes for reference (For example, in the first method it asks to create
I need these 3 methods please!
These are premade (do not change) classes for reference (For example, in the first method it asks to create a Map object )
----Map.java----
import java.awt.Color; import java.awt.Container; import java.awt.Image; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;
import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JLayeredPane; import javax.swing.SwingConstants; import javax.swing.WindowConstants;
public class Map extends JFrame {
private static final long serialVersionUID = 1;
private static Container contentPane; private final static int frame_length = 1100; private final static int frame_height = 881; private final static int map_length = 1000; private final static int map_height = 781;
private static int ICON_WIDTH = 16; private static int ICON_HEIGHT = 37; private JLayeredPane mainPanel;
/* Constructor. Creates a panel to represent the map and destroys the panel when its window is closed. */ /* -------------------------------------------- */ public Map() { /* -------------------------------------------- */ super("Map of Canada");
// Initialize panels. contentPane = getContentPane(); mainPanel = new JLayeredPane();
// Load Canada map. ImageIcon icon = new ImageIcon("canada.jpg"); Image img = icon.getImage(); Image scaledImage = img.getScaledInstance(map_length,map_height, java.awt.Image.SCALE_SMOOTH); icon = new ImageIcon(scaledImage); JLabel canadaMap = new JLabel(icon); canadaMap.setSize(map_length, map_height); canadaMap.setLocation(50, 30); mainPanel.add(canadaMap);
addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent event) { System.exit(0); } });
// Main panel properties. setSize(frame_length, frame_height); setVisible(true); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); contentPane.setBackground(new Color(249, 249, 249)); contentPane.add(mainPanel); contentPane.setFocusable(true); contentPane.requestFocusInWindow(); revalidate(); }
/** * Refresh the GUI. */ public void refresh () { revalidate(); repaint(); } /** * Add a city marker to the map. * @param city */ public void addCity (City city) { JLabel marker = new JLabel(city.getMarker()); marker.setLocation(city.getX() - (ICON_WIDTH/2), city.getY() - ICON_HEIGHT); marker.setSize(ICON_WIDTH, ICON_HEIGHT); marker.setName(city.getName()); marker.setToolTipText(city.getName()); mainPanel.add(marker, 0); refresh(); }
}
----MyFileReader.java----
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;
/* This class contains methods to open a text file, read a line of the file, and read an integer value from the file. */ public class MyFileReader { private String buffer = null; private BufferedReader in = null; /* Open the file whose name was give as parameter */ public MyFileReader(String fileName) { try { in = new BufferedReader(new FileReader(fileName)); buffer = in.readLine(); // Store first line } catch (IOException e) { System.out.println("Cannot read file \'"+fileName+"\'"); System.exit(0); } } /* Returns true if the end of the file has been reached; it returns false otherwise. */ public boolean endOfFile() { if (buffer == null) return true; else return false; } /* Reads a line from the file. */ public String readString() { String line = buffer; if (buffer == null) { System.out.println("Error. The end of file was reached, so another string cannot be read from it"); return null; } try { buffer = in.readLine(); } catch (IOException e) { System.out.println("Error. Cannot read from file"); System.exit(0); } return line; } /* Reads an integer from the file. Returns -1 if no integer could be read. */ public int readInt() { String line = readString(); if (line == null) return -1; else return Integer.parseInt(line); } /* Reads an double from the file. Returns -1.0 if no integer could be read. */ public double readDouble() { String line = readString(); if (line == null) return -1.0; else return Double.parseDouble(line); } }


The constructor needed is for the City Object:
public City(String name, int x, int y) { //constructor for City /* * @param name * @param x * @param y */ this.name=name; this.x=x; this.y=y; marker= new CityMarker(); // Initializing marker }
THE FORMAT OF THE TXT FILE IS THE FOLLOWING;
LONDON
30
70
WINDSOR
22
90
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] o 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 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. 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. O public double distBetweenCities(City, City) Calculates the Euclidean distance between the two given Cities public void compare Distances() 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. O 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] o 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 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. 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. O public double distBetweenCities(City, City) Calculates the Euclidean distance between the two given Cities public void compare Distances() 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. O
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
