0

so I have Location class, and a radio class(the radio class contains the coordinates). Basically for a program I am writing I need the location to take the coordinates from the radio class, and use the distance formula for the 2 sets of coordinates. Location:

public class Location {


private double lat, lon;


public Location (double lat, double lon){
this.lat=lat;
this.lon=lon;
}

public Location (){
this.lat=0.0;
this.lon=0.0;
}

public void setLat(double lat){
this.lat=lat;
}

public void setLon(double lon){
this.lon=lon;
}

public double Distance (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);

double Distance = 3959 * spherDistance;

return Distance;

}


public double getLat(){
return lat;
}

public double getLon(){
return lon;
}
public double getDistance(){
return Distance;
}

public String toString(){
String dist="lat"+lat+"lon"+lon;
return dist;
}

}

and Radio:

public class Radio {
public static void main(String[] args) {
Location loc1=new Location(10,20);
Location loc2=new Location(30,40);

System.out.println(loc1.toString());    
System.out.println(loc2.toString());    

}


}

I should also mention that the only reason main method is in radio is because I am simply testing to see if everything works.Any help or suggestions would be greatly appreciated. Thank y'all very much!

1
  • 6
    And what is your exact problem ? Commented Aug 2, 2012 at 22:07

2 Answers 2

1

Extrapolation on wardd's suggestions above, I suggest doing something like this:

Location class:

public class Location {
  // private data

  // constructor

  public double distance(Location otherLoc) {
    // Use distance formula here
  }
}

Radio class:

public class Radio {
  private Location loc;

  public double distance(Radio otherRadio) {
    return loc.distance(otherRadio.loc);
  }
}

If this doesn't answer all of your questions, please edit your post above or post a new one with additional details so that we can help.

Sign up to request clarification or add additional context in comments.

Comments

0

If I can read your mind correctly, a Radio should have a Location, and you want to find the distance between two radios. In that case, each radio should contain its own instance of Location, like so:

public class Radio {
    public Location loc;
}

Then if you want to find the distance between two radios, you can create use your Distance function, but pass in (Radio r1, Radio r2) as arguments instead of four doubles. Change public double Distance to static public double Distance so that you can call it on its own without having to create an instance of Location. Then inside of your distance function, you can retrieve the location values of each radio, do your computations, and return a result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.