In this exercise you will write a program to compute the distance between any two geo locations

In this exercise you will write a program to compute the distance between any two geo locations. In this program you will ask the user for four numbers. USE JAVA starting latitude starting longitude ending latitude ending longitude Then, using the GeoLocation class and our earlier example as a reference, compute the distance in miles between the two locations. A sample program run should match exactly as below: Enter the latitude of the starting location: 48.8567 Enter the longitude of the starting location: 2.3508 Enter the latitude of the ending location: 51.5072 Enter the longitude of the ending location: 0.1275 The distance is 208.08639358288565 miles.
In this exercise you will write a program to compute the distance between any two geo locations.

In this program you will ask the user for four numbers. USE JAVA

starting latitude
starting longitude
ending latitude
ending longitude
Then, using the GeoLocation class and our earlier example as a reference, compute the distance in miles between the two locations.

A sample program run should match exactly as below:

Enter the latitude of the starting location: 48.8567

Enter the longitude of the starting location: 2.3508

Enter the latitude of the ending location: 51.5072

Enter the longitude of the ending location: 0.1275

The distance is 208.08639358288565 miles.

The Correct Answer and Explanation is :

To compute the distance between two geographic locations using Java, we can use the Haversine formula, which calculates the great-circle distance between two points on the Earth based on their latitudes and longitudes.

The Haversine formula is:

[
a = \sin^2\left(\frac{\Delta\phi}{2}\right) + \cos(\phi_1) \cdot \cos(\phi_2) \cdot \sin^2\left(\frac{\Delta\lambda}{2}\right)
]
[
c = 2 \cdot \text{atan2}\left(\sqrt{a}, \sqrt{1-a}\right)
]
[
d = R \cdot c
]
Where:

  • ( \phi_1, \phi_2 ) are the latitudes of the two locations in radians,
  • ( \lambda_1, \lambda_2 ) are the longitudes of the two locations in radians,
  • ( R ) is the radius of the Earth (mean radius = 3,959 miles),
  • ( \Delta\phi ) and ( \Delta\lambda ) are the differences in the latitudes and longitudes, respectively.

Java Program

import java.util.Scanner;

public class GeoDistance {

    // Function to convert degrees to radians
    public static double toRadians(double degree) {
        return degree * Math.PI / 180;
    }

    // Function to calculate distance between two locations using Haversine formula
    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        // Radius of the Earth in miles
        final double R = 3959.0;

        // Convert degrees to radians
        lat1 = toRadians(lat1);
        lon1 = toRadians(lon1);
        lat2 = toRadians(lat2);
        lon2 = toRadians(lon2);

        // Difference in latitudes and longitudes
        double deltaLat = lat2 - lat1;
        double deltaLon = lon2 - lon1;

        // Haversine formula
        double a = Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(deltaLon / 2), 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        // Distance in miles
        return R * c;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Asking the user for input
        System.out.print("Enter the latitude of the starting location: ");
        double lat1 = scanner.nextDouble();
        System.out.print("Enter the longitude of the starting location: ");
        double lon1 = scanner.nextDouble();
        System.out.print("Enter the latitude of the ending location: ");
        double lat2 = scanner.nextDouble();
        System.out.print("Enter the longitude of the ending location: ");
        double lon2 = scanner.nextDouble();

        // Calculate the distance
        double distance = calculateDistance(lat1, lon1, lat2, lon2);

        // Output the result
        System.out.println("The distance is " + distance + " miles.");

        scanner.close();
    }
}

Explanation

  1. Inputs: The program asks the user to input the latitudes and longitudes of the starting and ending locations. These values represent geographic coordinates (in degrees).
  2. Conversion to Radians: The toRadians function is used to convert the given latitudes and longitudes from degrees to radians. This is necessary because trigonometric functions in Java, like sin() and cos(), work with radians.
  3. Haversine Formula: In the calculateDistance function:
  • The latitudes and longitudes are used to compute the differences in their values.
  • The formula calculates the great-circle distance between the two points, considering the Earth’s curvature.
  • The radius of the Earth is assumed to be 3,959 miles, which is the average radius.
  1. Output: After computing the distance using the Haversine formula, the program displays the result in miles.

Example

For the input:

Enter the latitude of the starting location: 48.8567
Enter the longitude of the starting location: 2.3508
Enter the latitude of the ending location: 51.5072
Enter the longitude of the ending location: 0.1275

The program calculates and outputs:

The distance is 208.08639358288565 miles.

This program is an efficient way to compute the great-circle distance between two geographic locations using basic trigonometry in Java.

Scroll to Top