Need some help in Java, got stuck. Below is also my code for GeoLocation.java Ge
ID: 3846901 • Letter: N
Question
Need some help in Java, got stuck. Below is also my code for GeoLocation.java
GeoLocation.java:
public class GeoLocation {
private static double radius = 3963.1676;
private double latitude;
private double longitude;
public GeoLocation() //no argument constructor
{
latitude = 0;
longitude = 0;
}
public GeoLocation(double latitude,double longitude)//argument constructor
{
this.latitude = latitude;
this.longitude = longitude;
}
//set and get methods for latitude and longitude
public void setLatitude(double latitude)
{
this.latitude = latitude;
}
public double getLatitude()
{
return latitude;
}
public void setLongitude(double longitude)
{
this.longitude = longitude;
}
public double getLongitude()
{
return longitude;
}
public double distanceFrom(GeoLocation o)//calculate distance between two Geolocation objects
{
double dLat = Math.toRadians(this.latitude - o.latitude);
double dLng = Math.toRadians(this.longitude - o.longitude);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(o.latitude)) * Math.cos(Math.toRadians(o.latitude)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = (double) (radius * c);
return dist;
}
}
TestGeo:
public class TestGeo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
GeoLocation loc1 = new GeoLocation();
System.out.println(" Loc1: Latitude : "+ loc1.getLatitude());
System.out.println(" Longitude : "+ loc1.getLongitude());
loc1.setLongitude(77.65);
loc1.setLatitude(44.34);
System.out.println(" Loc1 :Latitude : "+ loc1.getLatitude());
System.out.println(" Longitude : "+ loc1.getLongitude());
GeoLocation loc2 = new GeoLocation(34.5,56.23);
System.out.println(" Loc2 : Latitude : "+ loc2.getLatitude());
System.out.println(" Longitude : "+ loc2.getLongitude());
System.out.println(" Distance from loc1 to loc2 : "+loc1.distanceFrom(loc2));
}
}
Explanation / Answer
Given below is the modified code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
GeoLocation.java
--------
public class GeoLocation {
private static double radius = 3963.1676;
private double latitude;
private double longitude;
public GeoLocation() //no argument constructor
{
latitude = 0;
longitude = 0;
}
public GeoLocation(double latitude,double longitude)//argument constructor
{
this.latitude = latitude;
this.longitude = longitude;
}
//set and get methods for latitude and longitude
public void setLatitude(double latitude)
{
this.latitude = latitude;
}
public double getLatitude()
{
return latitude;
}
public void setLongitude(double longitude)
{
this.longitude = longitude;
}
public double getLongitude()
{
return longitude;
}
public double distanceFrom(GeoLocation o)//calculate distance between two Geolocation objects
{
double dLat = Math.toRadians(this.latitude - o.latitude);
double dLng = Math.toRadians(this.longitude - o.longitude);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(o.latitude)) * Math.cos(Math.toRadians(o.latitude)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = (double) (radius * c);
return dist;
}
public String toString(){
return "latitude:" + latitude + ",longitude:" + longitude;
}
public boolean equals(GeoLocation o){
if(Math.abs(latitude - o.latitude) <= 0.0001 && Math.abs(longitude - o.longitude) <= 0.0001)
return true;
else
return false;
}
}
TestGeo.java
---------
public class TestGeo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
GeoLocation loc1 = new GeoLocation();
System.out.println(" Loc1: Latitude : "+ loc1.getLatitude());
System.out.println(" Longitude : "+ loc1.getLongitude());
loc1.setLongitude(77.65);
loc1.setLatitude(44.34);
System.out.println(" Loc1 :Latitude : "+ loc1.getLatitude());
System.out.println(" Longitude : "+ loc1.getLongitude());
GeoLocation loc2 = new GeoLocation(34.5,56.23);
System.out.println(" Loc2 : Latitude : "+ loc2.getLatitude());
System.out.println(" Longitude : "+ loc2.getLongitude());
System.out.println(" Distance from loc1 to loc2 : "+loc1.distanceFrom(loc2));
GeoLocation geo1 = new GeoLocation(32.735316, -117.149046);
GeoLocation geo2 = new GeoLocation(32.735328, -117.14910);
GeoLocation geo3 = new GeoLocation(32.735328, -117.14920);
System.out.println();
System.out.println("geo1 = " + geo1);
System.out.println("geo2 = " + geo2);
System.out.println("geo3 = " + geo3);
if(geo1.equals(geo2))
System.out.println("geo1 equals geo2");
else
System.out.println("geo1 NOT equals geo2");
if(geo1.equals(geo3))
System.out.println("geo1 equals geo3");
else
System.out.println("geo1 NOT equals geo3");
}
}
output
------
Loc1: Latitude : 0.0
Longitude : 0.0
Loc1 :Latitude : 44.34
Longitude : 77.65
Loc2 : Latitude : 34.5
Longitude : 56.23
Distance from loc1 to loc2 : 1398.5772992626707
geo1 = latitude:32.735316,longitude:-117.149046
geo2 = latitude:32.735328,longitude:-117.1491
geo3 = latitude:32.735328,longitude:-117.1492
geo1 equals geo2
geo1 NOT equals geo3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.