JAVA!!! Compute the distance between following cities using point2D concept in C
ID: 3720472 • Letter: J
Question
JAVA!!!
Compute the distance between following cities using point2D concept in Classes and objects. Create a Java Program with user prompt. User have to enter city name and latitude and longitude and it should give exact distance between two cities . The program should able to compute distance between two cities like in Google map. Your answers must match google answers (Approximately). Your program should work for any cities between USA. You must solve this using Classes and objects
Example:
Cleveland - 1°28?56?N 81°40?11?W (Latitude, Longitude )
Akron - 41.0814° N, 81.5190° W
Distance is 38.7 miles
Explanation / Answer
Place.java
public class Place {
private double latitude;
private double longetude;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongetude() {
return longetude;
}
public void setLongetude(double longetude) {
this.longetude = longetude;
}
public Place(double latitude, double longetude) {
super();
this.latitude = latitude;
this.longetude = longetude;
}
}
============
import java.util.Scanner;
public class CalculateDistance {
private static double getDistance(Place place1,Place place2){
double longitude1 = place1.getLongetude();
double Latitude1 = place1.getLatitude();
double longitude2 = place2.getLongetude();
double Latitude2 = place2.getLatitude();
double rad_Earth = 6375.795;
// 41.4993° N, 81.6944° W cleveland coordinate
//41.0814° N, 81.5190° Akron coordinate
double dDistance = Double.MIN_VALUE;
double dLat1InRad = Latitude1 * (Math.PI / 180.0);
double dLong1InRad = longitude1 * (Math.PI / 180.0);
double dLat2InRad = Latitude2 * (Math.PI / 180.0);
double dLong2InRad = longitude2 * (Math.PI / 180.0);
double dLongitude = dLong2InRad - dLong1InRad;
double dLatitude = dLat2InRad - dLat1InRad;
double a = Math.pow(Math.sin(dLatitude / 2.0), 2.0) +
Math.cos(dLat1InRad) * Math.cos(dLat2InRad) *
Math.pow(Math.sin(dLongitude / 2.0), 2.0);
double c = 2.0 * Math.asin(Math.sqrt(a));
// radius in km
Double kEarthRadiusKms = 6371.0
;
dDistance = kEarthRadiusKms * c;
return dDistance;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter latitude1");
double latitude1 = scan.nextDouble();
System.out.println("Enter longitude1");
double longitude1 = scan.nextDouble();
Place place1 = new Place(latitude1, longitude1);
System.out.println("Enter latitude2");
double latitude2 = scan.nextDouble();
System.out.println("Enter longitude2");
double longitude2 = scan.nextDouble();
Place place2 = new Place(latitude2, longitude2);
double distance = getDistance(place1, place2);
System.out.println("distance 1 : "+distance);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.