Need help in JAVA
We learned from lecture slides how to read a UML (Unified Modeling Language) diagram. Here we use one to tell us what class name, member fields, and member methods to code. EARTH_RADIUS_MILES:double = 3963.1676 latitude:double longitude:double + GeoLocation() + GeoLocation(lat:double, Ion:double) + setLatitude(lat:double):void + getLatitude():double + setLongitude(lat:double):void + getLongitude():double + distanceFrom(o:GeoLocation):double 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 TestGeo (or whatever), write a main() method that instantiates a GeoLocation 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 GeoLocation two-arg constructor
Expert Answer
import java.lang.*;
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;
}
}
class TestGeo
{
public static void main (String[] args)
{
GeoLocation loc1 = new GeoLocation();
System.out.println(“nLoc1: Latitude : “+ loc1.getLatitude());
System.out.println(“nLongitude : “+ loc1.getLongitude());
loc1.setLongitude(77.65);
loc1.setLatitude(44.34);
System.out.println(“nLoc1 :Latitude : “+ loc1.getLatitude());
System.out.println(“nLongitude : “+ loc1.getLongitude());
GeoLocation loc2 = new GeoLocation(34.5,56.23);
System.out.println(“nLoc2 : Latitude : “+ loc2.getLatitude());
System.out.println(“nLongitude : “+ loc2.getLongitude());
System.out.println(“nDistance from loc1 to loc2 : “+loc1.distanceFrom(loc2));
}
}
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