Question
-------------------------------------- Location -------------------------------------- - EARTH_RADIUS_MILES : double = 3963.1676 - latitude : double - longitude : double -------------------------------------- + Location() + Location(lat : double, lon
-------------------------------------- Location -------------------------------------- - EARTH_RADIUS_MILES : double = 3963.1676 - latitude : double - longitude : double -------------------------------------- + Location() + Location(lat : double, lon : double) + setLatitude(lat : double) : void + getLatitude() : double + setLongitude(lon : double) : void + getLongitude() : double + distanceFrom( o : Location) : double --------------------------------------
Coding distanceFrom()
The distance from one location to another can be found by the Spherical Law of Cosines.
Let point x be latitude1, longitude1 and point y be latitude2, longitude2. Then:
cosC = sin(latitude1) * sin(latitude2) + cos(latitude1) * cos(latitude2) * cos(longitude1-longitude2)
arcLenC = acos(cosC)
Now, using the Math class, we can plug in our formula (where point x is this location and point y is the other location provided as the method parameter).
// Returns the distance in miles between this geo location and the given // other geo location public double distanceFrom(Location other) { // TODO: First use Math.toRadians() convert this latitude & longitude, and // the other latitude & longitude to radians double lat1 = double lon1 = double lat2 = double lon2 = // TODO: Apply the spherical law of cosines with a triangle composed of // the two locations and the North Pole double cosC = double arcLenC = // Return the arcLenC times RADIUS }
Testing
Writing a program to test your code is a common practice. YOU WILL NOT TURN THE TEST PROGRAM IN! A simple way to do this is to open another file, called TestLocation (or whatever), write a main() method that
instantiates a Location object using the no-arg constructor
print getLatitude() and getLongitude() return values (should be zeros)
call the setters
print again to show values were modified to whatever values you passed in
repeat Steps 1 - 4 but use the Location two-arg constructor
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