Question
Hi I need help to write the methods: 1. Write a method with this signature: public Airport getAirportClosestTo(String code) which returns the Airport which is
Hi I need help to write the methods: 1.
- Write a method with this signature:
public Airport getAirportClosestTo(String code)
which returns the Airport which is closest to the airport corresponding to code.
2.
- Write a method with this signature:
public List getAirportsClosestTo(String code, int num)
Returns a list of the num Airports that are closest to the Airport with code. For example, if num=3, the find the 3 closest Airports to code. No hints. This will take some thought!
Using, airportsSmall.txt, these are good tests.
getAirportsClosestTo("ANB", 1);
getAirportsClosestTo("ANB", 2);
getAirportsClosestTo("ANB", 3);
--------------------------------------
ANB 33.58 85.85 Anniston AL ABQ 35.05 106.6 Albuquerque NM ALS 37.45 105.87 Alamosa CO UCC 37.58 116.08 YuccaFlat NV GLH 33.48 90.98 Greenville MS NGZ 37.78 122.32 AlamedaNAS CA BRL 40.78 91.12 Burlington IA GMU 34.85 82.35 Greenville SC ALB 42.75 73.8 Albany NY IDA 43.52 112.07 IdahoFalls ID AEL 43.68 93.37 AlbertLea MN BTV 44.47 73.15 Burlington VT 3B1 45.45 69.55 Greenville ME 75S 48.5 122.33 Burlington WA
------------------------------------
public class DistanceCalculator {
private final static double conversionRateDR = 0.017453; private final static double conversionRateKM = 0.621371; private final static double radiusEarth = 6371.01;
public static double getDistance(double lat1, double long1, double lat2, double long2){
double lat1R, long1R, lat2R, long2R;
lat1R = convertToRadians(lat1); lat2R = convertToRadians(lat2);
long1R = convertToRadians(long1); long2R = convertToRadians(long2);
double distance = Math.acos(Math.sin(lat1R) * Math.sin(lat2R) + Math.cos(lat1R) * Math.cos(lat2R) * Math.cos(long2R - long1R)) * radiusEarth; return convertToMiles(distance); } private static double convertToMiles(double distance){ return distance * conversionRateKM; }
private static double convertToRadians(double degree){ return degree * conversionRateDR; } }
-----------------------------
public class Airport { private String code; private String city; private String state; private double latitude; private double longtitude; public Airport(String code, double latitude, double longtitude, String city, String state ) { this.code=code; this.latitude=latitude; this.longtitude=longtitude; this.city=city; this.state=state;
} protected Airport (String code) { this.code=code; } public String getCode() { return code; } public double getLatitude() { return latitude; } public double getLongtitude() { return longtitude; } public String getCity() { return city; } public String getState() { return state; }
// equal method public boolean equals(Object o) { if(o instanceof Airport) { Airport a = (Airport)o; return(this.code==a.code); } return false; } @Override public String toString() { return String.format(" Code:%s latitude:%.2f longtitude:%.2f city:%s state:%s ", getCode(), getLatitude(), getLongtitude(), getCity(), getState()); }
Step 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