Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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));

}
  
}

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 field attributes, and member methods to code. Write a class with the following attributes and interface specification. Here we are using the term interface to describe the public methods, while "attributes is the term given to instance field variables. Place name: String description String location GeoLocation Place(name String, desc String, latitude double, longitude double) setName(name: String) void getName0 String setDescription (desc String) void getDescription0:String setLocation( latitude double, longitude double) void setLocation( location GeoLocation) void getLocation0 GeoLocation toString0 String get IdentificationString0:String A call to print a Place object system.out.println (myPlaceobject); should return a string of the format: San Diego zoo, 2920 Zoo Drive, latitude :32.735316, longitude: 117.149046

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote