Complete Airport.java by providing the following methods, as described by the comments in the starter file: • public void connectTo(Airport that) • public void disconnectFrom(Airport that) • public...


Complete Airport.java by providing the following methods, as described by the comments in the starter file: • public void connectTo(Airport that) • public void disconnectFrom(Airport that) • public boolean equals(Object x) • public int compareTo(Airport that) • public boolean isConnectedTo(Airport that) Then write class airlines.FlightNet, which models a company’s air routes. The class should aggregate instances of Airport. It should do this by extending HashSet. It should not also have an instance variable of type Hashet. This class doesn’t need any instance variables, and you’ll get zero points if you have any. The class should have the following methods: • public boolean nameIsAvailable(String name) – Returns true if the FlightNet doesn’t contain an airport with the specified name. • public void connect(Airport a1, Airport a2) – Connects a1 and a2. You’ll have to connect a1 to a2, and also a2 to a1. • public void disconnect(Airport a1, Airport a2) – Opposite of above. • public void removeAndDisconnect(Airport removeMe) – Removes removeMe from the FlightNet, and disconnects it from any airports that are still in the FlightNet. • public Airport getAirportNearXY(int x, int y, int maximumDistance) – Checks all airports in the FlightNet. Returns the first airport whose (x,y) location is within maximumDistance of the x,y args of the method. Returns null if no airport is within maximumDistance. Note: Check out the hypot method of the Math class. Run AirGrader to see how you’re doing. After you complete Airport and FlightNet, you’ll be able to run RoutesPanel as an app. You’ll see a map like the one on page 1. Left-click on open territory in the map to create a new airport. Double-click on any airport to delete it and any routes to it. Single-click on any airport to create or delete a route: existing routes will appear red, and you can delete one by clicking the other airport. Nonexistent routes will appear green, and you can create one by clicking on the other airport. All these operations cause calls to a FlightNet instance that is owned by RoutesPanel.


Airport.java


package airlines;


import java.util.*;


public class Airport implements Comparable


{


private String name;


private int x;


private int y;


private Set connections; // all airports with a direct route to/from this airport


public Airport(String name, int x, int y)


{


this.name = name;


this.x = x;


this.y = y;


connections = new TreeSet();


}


public String getName()


{


return name;


}


public int getX()


{


return x;


}


public int getY()


{


return y;


}


public List getConnections()


{


return new ArrayList(connections);


}


// Adds that airport to the list of connections.


public void connectTo(Airport that)


{


}


//


// Does nothing if this airport is not connected to that.


//


public void disconnectFrom(Airport that)


{


}


// Use best practice.


public boolean equals(Object x)


{


}


// Just compare by airport name.


public int compareTo(Airport that)


{


}


public boolean isConnectedTo(Airport that)


{


}


public String toString()


{


return "Airport " + name + " @(" + x + "," + y + ")";


}


}


RoutesPanel.java


package airlines;


import java.io.*;


import java.util.*;


import java.awt.*;


import java.awt.event.*;


import java.awt.geom.*;


import java.awt.image.BufferedImage;


import javax.imageio.ImageIO;


import javax.swing.*;


public class RoutesPanel extends JPanel


{


private final static Font FONT = new Font("SansSerif", Font.PLAIN, 12);


private final static int CLICK_RADIUS = 11;


private final static Color UNARMED_COLOR = Color.BLACK;


private final static Color ARMED_FOR_CONNECTION_COLOR = new Color(0, 200, 0);


private final static Color ARMED_FOR_DELETION_COLOR = Color.RED;


private final static File MAP_FILE = new File("pix/usmap.jpg");


private final static Color WHITEWASH_COLOR = new Color(255, 255, 255, 200);


private final static Stroke ROUTE_STROKE = new BasicStroke(2);


private FlightNet net;


private Airport armedAirport;


private BufferedImage usMapImage;


RoutesPanel()


{


try


{


usMapImage = ImageIO.read(MAP_FILE);


}


catch (IOException x)


{


sop("Can't read map image file " + MAP_FILE.getAbsolutePath());


System.exit(1);


}


net = FlightNet.makeTestInstance();


setPreferredSize(new Dimension(usMapImage.getWidth()/2, usMapImage.getHeight()/2));


MLis lis = new MLis();


addMouseListener(lis);


addMouseMotionListener(lis);


}


private class MLis extends MouseAdapter


{


@Override


public void mouseClicked(MouseEvent e)


{


Airport clickedAirport = net.getAirportNearXY(e.getX(), e.getY(), CLICK_RADIUS);


// First of 1 or 2 clicks.


if (armedAirport == null)


{


if (clickedAirport == null)


{


// Click in empty space to create new airport.


armedAirport = null;


NameDia dia = new NameDia(e.getX(), e.getY());


dia.setVisible(true);


}


else


{


// Click on existing airport to arm it.


armedAirport = clickedAirport;


}


}


// Second of 2 clicks


else


{


if (clickedAirport == null)


{


// 2nd click in empty space to cancel operation.


armedAirport = null;


}


else if (clickedAirport == armedAirport)


{


// 2nd click on armed airport to delete it.


net.removeAndDisconnect(armedAirport);


armedAirport = null;


}


else if (clickedAirport.isConnectedTo(armedAirport))


{


// 2nd click on connected airport to delete the connection.


net.disconnect(armedAirport, clickedAirport);


armedAirport = null;


}


else


{


// 2nd click on unconnected airport to connect it.


net.connect(armedAirport, clickedAirport);


armedAirport = null;


}


}


repaint();


}


} // MLis


private class NameDia extends JDialog implements ActionListener


{


private int x;


private int y;


private JTextField tf;


private JButton okBtn;


private JButton cancelBtn;


NameDia(int x, int y)


{


this.x = x;


this.y = y;


JPanel pan = new JPanel();


pan.add(new JLabel("3-letter name: "));


tf = new JTextField(4);


tf.addActionListener(this);


pan.add(tf);


add(pan, BorderLayout.NORTH);


pan = new JPanel();


okBtn = new JButton("Ok");


okBtn.addActionListener(this);


pan.add(okBtn);


cancelBtn = new JButton("Cancel");


cancelBtn.addActionListener(this);


pan.add(cancelBtn);


add(pan, BorderLayout.SOUTH);


pack();


setModal(true);


}


@Override


public void actionPerformed(ActionEvent e)


{


if (e.getSource() == cancelBtn)


{


setVisible(false);


}


else


{


String name = tf.getText().trim();


if (name.length() != 3)


{


JOptionPane.showMessageDialog(this, "Name must be 3 letters");


return;


}


if (!net.nameIsAvailable(name))


{


JOptionPane.showMessageDialog(this, "Name is in use");


return;


}


setVisible(false);


net.add(new Airport(name.toUpperCase(), x, y));


repaint();


}


}


} // NameDia


public void paintComponent(Graphics g)


{


// Background.


g.drawImage(usMapImage, 0, 0, getWidth(), getHeight(), this);


// Whitewash.


g.setColor(WHITEWASH_COLOR);


g.fillRect(0, 0, getWidth(), getHeight());


// Existing routes in black. If an airport is armed, some will get overdrawn.


g.setColor(UNARMED_COLOR);


Graphics2D g2 = (Graphics2D)g;


g2.setStroke(ROUTE_STROKE);


for (Airport a: net)


for (Airport dest: a.getConnections())


g.drawLine(a.getX(), a.getY(), dest.getX(), dest.getY());


// Routes that are armed for deletion.


if (armedAirport != null)


{


g.setColor(ARMED_FOR_DELETION_COLOR);


for (Airport dest: armedAirport.getConnections())


g.drawLine(armedAirport.getX(), armedAirport.getY(), dest.getX(), dest.getY());


}


// Routes that are armed for connection.


if (armedAirport != null)


{


g.setColor(ARMED_FOR_CONNECTION_COLOR);


for (Airport dest: net)


{


if (dest == armedAirport)


continue;


if (armedAirport.getConnections().contains(dest))


continue;


g.drawLine(armedAirport.getX(), armedAirport.getY(), dest.getX(), dest.getY());


}


}


// Airports


g.setFont(FONT);


for (Airport a: net)


if (a != armedAirport)


paintAirport(g, a);


if (armedAirport != null)


paintAirport(g, armedAirport);


}


private void paintAirport(Graphics g, Airport a)


{


int x = a.getX();


int y = a.getY();


boolean armed = a == armedAirport;


g.setColor(armed ? ARMED_FOR_DELETION_COLOR : UNARMED_COLOR);


g.fillOval(x-2, y-2, 5, 5);


g.drawOval(x-5, y-5, 11, 11);


int sw = g.getFontMetrics().stringWidth(a.getName());


g.drawString(a.getName(), x-sw/2, y+18);


}


static void sop(Object x) { System.out.println(x); }


public static void main(String[] args)


{


JFrame frame = new JFrame();


frame.add(new RoutesPanel());


frame.pack();


frame.setVisible(true);


}


}


AirGrader.java


package airlines;


import java.lang.reflect.*;


import java.util.*;


public class AirGrader


{


private final static Airport AAA = new Airport("AAA", 10, 20);


private final static Airport BBB1 = new Airport("BBB", 10, 20);


private final static Airport BBB2 = new Airport("BBB", 10, 20);


private final static Airport BBB3 = new Airport("BBB", 99, 88);


private final static Airport CCC = new Airport("CCC", 10, 20);


private final static Airport DDD = new Airport("DDD", 10, 20);


private final static Airport MMM = new Airport("MMM", 10, 20);


private final static Airport NNN = new Airport("NNN", 10, 20);


private static void checkAirport()


{


// Class.


Class airportClass = null;


try


{


airportClass = Class.forName("airlines.Airport");


}


catch (ClassNotFoundException x)


{


abortZeroPoints("No airlines.Airport class");


}


// Implements Comparable


Class[] interfaces = airportClass.getInterfaces();


boolean hasComparable = false;


for (Class c: interfaces)


{


if (c.getName().equals("java.lang.Comparable"))


{


hasComparable = true;


break;


}


}


if (!hasComparable)


abortZeroPoints("Airport doesn't implement Comparable");


// equals() calls compareTo()


SubAirport sub1 = new SubAirport("XXX", 0, 0);


SubAirport sub2 = new SubAirport("ZZZ", 0, 0);


sub1.equals(sub2);


if (!sub1.compareToWasCalled)


abortZeroPoints("In Airport, equals() doesn't call compareTo => Not best practice");


// equals()


checkEquals(AAA, BBB1, false);


checkEquals(BBB1, BBB2, true);


checkEquals(BBB1, BBB3, true);


// compareTo()


checkCompareTo(AAA, BBB1, -1);


checkCompareTo(BBB1, BBB2, 0);


checkCompareTo(BBB1, BBB3, 0);


// getConnections() should return sorted airports.


Airport that = new Airport("ZZZ", 1, 0);


Airport[] addOrder = { CCC, DDD, BBB1, AAA };


Airport[] expectOrder = { AAA, BBB1, CCC, DDD };


for (Airport a: addOrder)


that.connectTo(a);


List conns = that.getConnections();


String errPiece = "Added in this order:";


for (Airport a: addOrder)


errPiece += "\n " + a;


errPiece += "\ngetConnections() returned:";


for (Airport a: conns)


errPiece += "\n " + a;


errPiece += "\nexpected:";


for (Airport a: expectOrder)


errPiece += "\n " + a;


if (conns.size() != 4)


abortZeroPoints("Wrong number of airports:\n" + errPiece);


for (int i=0; i


if (!conns.get(i).equals(expectOrder[i]))


abortZeroPoints("getConnections returned airports in wrong order\n" + errPiece);


// isConnectedTo.


that = new Airport("XYZ", 0, 0);


that.connectTo(CCC);


that.connectTo(DDD);


Airport[] connOrder = { CCC, DDD };


Airport[] checks = { CCC, DDD, MMM, NNN };


boolean[] bExpect = { true, true, false, false };


for (Airport a: connOrder)


that.connectTo(a);


boolean trouble = false;


for (int i=0; i


if (that.isConnectedTo(checks[i]) != bExpect[i])


trouble = true;


if (trouble)


{


String err = "Connected an airport to " + CCC + " and " + DDD;


for (int i=0; i


err += "\n isConnectedTo(" + checks[i] + ") returned " + that.isConnectedTo(checks[i]) + ", expected " + bExpect[i];


abortZeroPoints(err);


}


}


private static void checkEquals(Airport a1, Airport a2, boolean expect)


{


boolean result = a1.equals(a2);


if (result != expect)


abortZeroPoints("equals() on " + a1 + " and " + a2 + " returned " + result + ", should be " + expect);


result = a2.equals(a1);


if (result != expect)


abortZeroPoints("equals() on " + a2 + " and " + a1 + " returned " + result + ", should be " + expect);


}


private static void checkCompareTo(Airport a1, Airport a2, int expectSignum)


{


int result = a1.compareTo(a2);


if (Math.signum(result) != expectSignum)


abortZeroPoints("compareTo() on " + a1 + " and " + a2 + " returned " + result + ", should be " + signumToString(expectSignum));


result = a2.compareTo(a1);


expectSignum *= -1;


if (Math.signum(result) != expectSignum)


abortZeroPoints("compareTo() on " + a2 + " and " + a1 + " returned " + result + ", should be " + signumToString(expectSignum));


}


private static String signumToString(int n)


{


if (n == 0)


return "0";


else if (n


return "


else


return ">0";


}


private static class SubAirport extends Airport


{


boolean compareToWasCalled;


SubAirport(String name, int x, int y)


{


super(name, x, y);


}


public int compareTo(Airport that)


{


compareToWasCalled = true;


return 0;


}


}


private static void checkFlightNet()


{


// Extends HashSet?


Class fnClass = null;


try


{


fnClass = Class.forName("airlines.FlightNet");


}


catch (ClassNotFoundException x)


{


abortZeroPoints("No airlines.FlightNet class");


}


Class supeClass = fnClass.getSuperclass();


try


{


Class hsClass = Class.forName("java.util.HashSet");


if (hsClass != supeClass)


abortZeroPoints("FlightNet doesn't extend HashSet");


}


catch (ClassNotFoundException x) { } // Shouldn't happen, ignore if it does


// Add


FlightNet that = new FlightNet();


Airport[] adds = { AAA, BBB1, CCC, DDD };


String addsErr = "Added to a FlightNet:";


for (Airport a: adds)


{


that.add(a);


addsErr += "\n " + a;


}


// Name availability


for (Airport a: adds)


{


if (that.nameIsAvailable(a.getName()))


{


String err = addsErr + "\nThen nameIsAvailable(" + a.getName() + ") returned true";


abortZeroPoints(err);


}


}


if (!that.nameIsAvailable("SFO"))


{


String err = addsErr + "\nThen nameIsAvailable(\"SFO\") returned false";


abortZeroPoints(err);


}


// Connection


that = new FlightNet();


Airport sfo = new Airport("SFO", 0, 0);


Airport jfk = new Airport("JFK", 100, 100);


that.add(sfo);


that.add(jfk);


that.connect(sfo, jfk);


if (sfo.isConnectedTo(jfk) == false || jfk.isConnectedTo(sfo) == false)


{


String err = "Created a FlightNet fn, then fn.connect(sfo, jfk);\n";


err += "Then sfo.isConnectedTo(jfk) returned " + sfo.isConnectedTo(jfk) + " and ";


err += "jfk.isConnectedTo(sfo) returned " + jfk.isConnectedTo(sfo) + "\nBoth should return true";


abortZeroPoints(err);


}


Airport sea = new Airport("SEA", 200, 200);


that.add(sea);


that.connect(sfo, sea);


if (sea.isConnectedTo(jfk))


{


String err = "Created a FlightNet fn, then fn.connect(sfo, jfk); fn.connect(sfo, sea);\n";


err += "Then sea.isConnectedTo(jfk) returned true";


abortZeroPoints(err);


}


if (jfk.isConnectedTo(sea))


{


String err = "Created a FlightNet fn, then fn.connect(sfo, jfk); fn.connect(sfo, sea);\n";


err += "Then jfk.isConnectedTo(sea) returned true";


abortZeroPoints(err);


}


// getAirportNearXY


Airport nearest = that.getAirportNearXY(110, 100, 12);


if (nearest != jfk)


{


String err = "FlightNet fn contains SFO at (0, 0), JFK at (100, 100), and SEA at (200, 200)\n";


err += "Then that.getAirportNearXY(110, 100, 12) returned " + nearest + ", expected jfk";


abortZeroPoints(err);


}


nearest = that.getAirportNearXY(123456, 123456, 100);


if (nearest != null)


{


String err = "FlightNet fn contains SFO at (0, 0) and JFK at (100, 100)\n";


err += "Then that.getAirportNearXY(123456, 123456, 100) returned " + nearest + ", expected null";


abortZeroPoints(err);


}


// removeAndDisconnect.


that.removeAndDisconnect(jfk);


String err = "Created a FlightNet fn, then fn.connect(sfo, jfk); fn.connect(sfo, sea); fn.removeAndDisconnect(jfk);\n";


if (that.contains(jfk))


{


err += "Then fn still contains jfk";


abortZeroPoints(err);


}


if (!that.contains(sfo))


{


err += "Then fn no longer contains sfo";


abortZeroPoints(err);


}


if (!that.contains(sea))


{


err += "Then fn no longer contains sea";


abortZeroPoints(err);


}


// Has instance vars?


Field[] fields = fnClass.getDeclaredFields();


if (fields.length > 0)


abortZeroPoints("FlightNet class has " + fields.length + " instance variable(s)");


}


private static void abortZeroPoints(String msg)


{


sop("Zero points: \n" + msg);


System.exit(1);


}


static void sop(Object x)


{


System.out.println(x);


}


public static void main(String[] args)


{


sop("START");


checkAirport();


checkFlightNet();


sop("DONE");


}


}


May 19, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here