Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Credit for work Please help me fix this Java code bugs... The starter code you downloaded with this assignment is an incomplete graphical tool that

Credit for work

Please help me fix this Java code bugs...

The starter code you downloaded with this assignment is an incomplete graphical tool that lets you point and click to create and destroy airports and flight routes. The user interface is complete. Your job is to complete the Airport class and write all of the Routes classes as described below.

First create a new Java project in Eclipse. Right-click on the project name in the Package Explorer, select New->Folder, and create a Folder called pix. Drag the downloaded file usmap.jpg into the pix folder. Create a new Java package in your project. Call the package airlines. Import the 3 downloaded source files (RoutesPanel.java, Airport.java, and AirGrader) into the package. You wont edit RoutesPanel or AirGrader. 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 companys 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 doesnt need any instance variables, and youll get zero points if you have any. The class should have the following methods: public boolean nameIsAvailable(String name) Returns true if the FlightNet doesnt contain an airport with the specified name. public void connect(Airport a1, Airport a2) Connects a1 and a2. Youll 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 youre doing.

p/s: AirGrader is too long, I may post it later in comment section.

----------------------------------------------------------------------------------------------------------

----- 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); } }

------------------

Thank you, I would really appreciate your help.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data In Just 7 Chapters

Authors: Prof Marcus Vinicius Pinto

1st Edition

B09NZ7ZX72, 979-8787954036

More Books

Students also viewed these Databases questions

Question

state what is meant by the term performance management

Answered: 1 week ago