Question: Write a program that will find the pairs of coordinates that are closest to each other, as if these were 2 planes in space, flying

Write a program that will find the pairs of coordinates that are closest to each other, as if these were 2 planes in space, flying too close to each other. Report of the 2 pairs of coordinates that are closest to each other. Display a warning message such as:

"Alert, Alert! Plane at coordinate: x-axis, y-axis is too close to Plane at coordinate: x-axis, y-axis."

Hints:

1.) Define a class that can hold 2 coordinates. Call it the Location class. Implement the Comparable interface, and thus create a compareTo() method.

2.) Create an array of Location objects, from reading the input file. (Note: the first record in the input file contains the number of records in the file).

3.) Sort the array by either x- or y-coordinate (use either the insertion sort or the selection sort algorithm. Do NOT use Collections.sort()...)

4.) Compare each object in the sorted array with the previous object, and calculate their distance by using the Pythagorean formula below:

double distanceBetween2Points = Math.sqrt((x2 - x1)^2 + (y2 - y1)^2)

Note: to get the square of a number in Java, use Math.pow(base, exponent)

5.) Good luck! Their safety depends on you!

Note: To process the input file that contains spaces and commas separating each word in each record, please use the following method to specify MULTIPLE delimeters:

inFile.useDelimiter(", |\ \ ");

********************************************************************

package airtrafficcontroller;

import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner;

/** * * @author mtsguest */ public class AirTrafficController {

/** * @param args the command line arguments */ Location[] myLocations; public static void main(String[] args) { AirTrafficController myATC = new AirTrafficController(); try { myATC.processFile(); myATC.selectionSort(); myATC.findMinDist(); } catch (Exception e) { System.out.println("Sorry, file corrupted. Cannot proceed. Contact Support"); } } public void processFile() throws Exception { boolean tryAgain = true; Scanner keyboard = new Scanner(System.in); String fileName; File aFile = null; Scanner inFile = null; do { try { System.out.println("What is the name of the input file?"); fileName = keyboard.nextLine(); aFile = new File(fileName); inFile = new Scanner(aFile); tryAgain = false; } catch (FileNotFoundException e) { System.out.println("Please try to enter a different file name."); tryAgain = true; } }while (tryAgain); tryAgain = true; do { try { //read each record - using a Loop //create a Loc object //add it to the array } catch (InputMismatchException e) { inFile.nextLine(); } catch (Exception e) { throw new Exception("File is corrupt"); } } while (tryAgain); } public void selectionSort() { } public void findMinDist() { } }

****************************************************************************

package airtrafficcontroller;

/** * * @author mtsguest */ public class Location implements Comparable { private int xLoc; private int yLoc;

public Location(int xLoc, int yLoc) { this.xLoc = xLoc; this.yLoc = yLoc; }

public int getxLoc() { return xLoc; }

public void setxLoc(int xLoc) { this.xLoc = xLoc; }

public int getyLoc() { return yLoc; }

public void setyLoc(int yLoc) { this.yLoc = yLoc; }

@Override public String toString() { return "Location{" + "xLoc=" + xLoc + ", yLoc=" + yLoc + '}'; } public int compareTo(Location otherLoc) { if (this.xLoc < otherLoc.xLoc) return -1; else if (this.xLoc > otherLoc.xLoc) return 1; else return 0; } }

***************************************************

airplanesinmiami.txt

18 5, 2 8, 10 12, 6 2, 9 1, 10 3, 4 13, 5 16, 7 9, 14 6, 5 4, 1 2, 3 4, 5 10, 4 19, 3 1, 7 5, 5 6, 8

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!