Re: Distance function help

Quote:

Originally Posted by

**abf617**
... I need..to take the coordinates from the radio class, and use the distance formula for the 2 sets of coordinates...

Your Distance function has nothing to do with the Location class, so could be made a static function of any class. (It doesn't make sense for a Location object to have a Distance data member, and yours does not so your Location class has a couple of problems that could be fixed if you really need it.)

I mean, it might make sense to for the Location class to have a Distance function that tales another Location object and calculates distance from the current object, or a Distance function that takes latitude and longitude of somewhere else and calculates distance from the current object. Stuff like that.

In the meanwhile, since the title that you gave the thread indicates to me that you need the Distance function to work, I think it already works. Just call it with latitudes and longitudes (in decimal degrees) for two places on Earth:

Code java:

public class Z
{
public static void main(String [] args)
{
// Example at [url]http://en.wikipedia.org/wiki/Great-circle_distance[/url]
// gives results in km. Your Distance function is set up for statute miles.
// Distance is calculated from an airport in Nashville, Tennessee to
// Los Angeles, California.
double d = DistanceInStatuteMiles(36.12, -86.67, 33.94, -118.4);
System.out.printf("d = %.2f statute miles.\n", d);
} // End of main function
static double DistanceInStatuteMiles(double lat1, double lon1, double lat2, double lon2)
{
lat1 = Math.toRadians(lat1);
lon1 = Math.toRadians(lon1);
lat2 = Math.toRadians(lat2);
lon2 = Math.toRadians(lon2);
double cosLat2 = Math.cos(lat2);
double sinLonChange = Math.sin(lon2-lon1);
double cosLat1 = Math.cos(lat1);
double sinLat2 = Math.sin(lat2);
double sinLat1 = Math.sin(lat1);
double cosLonChange = Math.cos(lon2-lon1);
double a = Math.pow((cosLat2*sinLonChange), 2);
double b = Math.pow(((cosLat1*sinLat2)-(sinLat1*cosLat2*cosLonChange)), 2);
double c = (sinLat1*sinLat2)+(cosLat1*cosLat2*cosLonChange);
double spherDistance = Math.atan(Math.sqrt(a+b)/c);
// Statute miles
double distance = 3959 * spherDistance;
// Change the constant to 6372.8 to get km. (Change the function name too.)
return distance;
} // End of Distance function
} // End of class definition

Output

d = 1793.66 statute miles.

Now if you have problems that you don't understand with the other classes, you can probably get help if you ask specific questions: Tell us what compiler messages and/or program output that you got and tell us what you don't understand.

Cheers!

Z