Write a Java program that computes the spherical distance between two points on
ID: 3782956 • Letter: W
Question
Write a Java program that computes the spherical distance between two points on the surface of the Earth, given their latitudes and longitudes.
* User interface specifications:
-The program prompts for the desired two points in DDD MM SS Direction longitude and latitude format, each of which contains three integers and one character, separated by space(s).
-Ask users to enter four coordinates in four separate lines. The first line must be the latitude of the first point, the second line is for the longitude of the first point, and the remaining two lines are for the latitude and longitude of the second point. There must be space(s) between each DDD MM SS Direction component.
- The data input lines are separated from prompt message lines. In other words, after each prompt message is displayed, your program should force user to enter data on a new line.
Example)
Latitude 1?
33 56 4 N
Longitude 1?
118 24 0 W
Latitude 2?
36 7 2 N
Longitude 2?
86 40 2 W
* Output
- Display the distance between the two designated points, descriptively and friendly
- Make sure to display the result in miles
Explanation / Answer
// Distance.java
import java.util.Scanner;
public class Distance
{
final static double earthRadius = 6372.795;
final static double milesToKm = 0.6214;
public static double getDistance(double l1, double l2, double long1, double long2)
{
double d = Math.acos(Math.sin(l1)*Math.sin(l2)+Math.cos(l1)*Math.cos(l2)*Math.cos(long2 - long1));
return (d * earthRadius);
}
// latitude longitude, "W": negative value, "E": positive, and "N": positive and "S": negative
public static char getFlag (String dir)
{
if (dir.equals('N') || dir.equals('E'))
return 1;
else
return 0;
}
public static void main(String[] args)
{
double lat1,lat2,long1,long2,a1,a2;
Scanner scan = new Scanner(System.in);
System.out.println("Latitude 1: ");
double d1 = scan.nextDouble();
double m1 = scan.nextDouble();
double s1 = scan.nextDouble();
String dir1 = scan.next();
int flag1 = getFlag(dir1);
if(flag1 == 0)
lat1 = (-1)* (d1 + (m1/60) + (s1/3600));
else
lat1 = (d1 + (m1/60) + (s1/3600));
System.out.println("Longitude 1: ");
double d2 = scan.nextDouble();
double m2 = scan.nextDouble();
double s2 = scan.nextDouble();
String dir2 = scan.next();
int flag2 = getFlag(dir2);
if(flag1 == 0)
long1 = (-1)* (d2 + (m2/60) + (s2/3600));
else
long1 = (d2 + (m2/60) + (s2/3600));
System.out.println("Latitude 2: ");
double d3 = scan.nextDouble();
double m3 = scan.nextDouble();
double s3 = scan.nextDouble();
String dir3 = scan.next();
int flag3 = getFlag(dir3);
if(flag1 == 0)
lat2 = (-1)* (d3 + (m3/60) + (s3/3600));
else
lat2 = (d3 + (m3/60) + (s3/3600));
System.out.println("Longitude 2: ");
double d4 = scan.nextDouble();
double m4 = scan.nextDouble();
double s4 = scan.nextDouble();
String dir4 = scan.next();
int flag4 = getFlag(dir4);
if(flag1 == 0)
long2 = (-1)* (d4 + (m4/60) + (s4/3600));
else
long2 = (d4 + (m4/60) + (s4/3600));
a1 = lat1;
a2 = lat2;
double longitude1 = long1;
double longitude2 = long2;
double latitude1 = Math.toRadians(a1);
double latitude2 = Math.toRadians(a2);
double lg1 = Math.toRadians(longitude1);
double lg2 = Math.toRadians(longitude2);
double l1 = Math.round(latitude1 * 10000.0)/10000.0;
double l2 = Math.round(latitude2 * 10000.0)/10000.0;
double dist = getDistance(l1,l2,lg1,lg2);
System.out.println("Distance between points: " + (dist * milesToKm) + " miles");
}
}
/*
output:
Latitude 1:
33 56 4 N
Longitude 1:
118 24 0 W
Latitude 2:
36 7 2 N
Longitude 2:
86 40 2 W
Distance between points: 1794.3623939163228 miles
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.