{"id":179677,"date":"2025-01-02T04:44:56","date_gmt":"2025-01-02T04:44:56","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=179677"},"modified":"2025-01-02T04:44:58","modified_gmt":"2025-01-02T04:44:58","slug":"in-this-exercise-you-will-write-a-program-to-compute-the-distance-between-any-two-geo-locations","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/01\/02\/in-this-exercise-you-will-write-a-program-to-compute-the-distance-between-any-two-geo-locations\/","title":{"rendered":"In this exercise you will write a program to compute the distance between any two geo locations"},"content":{"rendered":"\n<p>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.<br>In this exercise you will write a program to compute the distance between any two geo locations.<\/p>\n\n\n\n<p>In this program you will ask the user for four numbers. USE JAVA<\/p>\n\n\n\n<p>starting latitude<br>starting longitude<br>ending latitude<br>ending longitude<br>Then, using the GeoLocation class and our earlier example as a reference, compute the distance in miles between the two locations.<\/p>\n\n\n\n<p>A sample program run should match exactly as below:<\/p>\n\n\n\n<p>Enter the latitude of the starting location: 48.8567<\/p>\n\n\n\n<p>Enter the longitude of the starting location: 2.3508<\/p>\n\n\n\n<p>Enter the latitude of the ending location: 51.5072<\/p>\n\n\n\n<p>Enter the longitude of the ending location: 0.1275<\/p>\n\n\n\n<p>The distance is 208.08639358288565 miles.<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<p>To compute the distance between two geographic locations using Java, we can use the <strong>Haversine formula<\/strong>, which calculates the great-circle distance between two points on the Earth based on their latitudes and longitudes.<\/p>\n\n\n\n<p>The Haversine formula is:<\/p>\n\n\n\n<p>[<br>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)<br>]<br>[<br>c = 2 \\cdot \\text{atan2}\\left(\\sqrt{a}, \\sqrt{1-a}\\right)<br>]<br>[<br>d = R \\cdot c<br>]<br>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>( \\phi_1, \\phi_2 ) are the latitudes of the two locations in radians,<\/li>\n\n\n\n<li>( \\lambda_1, \\lambda_2 ) are the longitudes of the two locations in radians,<\/li>\n\n\n\n<li>( R ) is the radius of the Earth (mean radius = 3,959 miles),<\/li>\n\n\n\n<li>( \\Delta\\phi ) and ( \\Delta\\lambda ) are the differences in the latitudes and longitudes, respectively.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Java Program<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\npublic class GeoDistance {\n\n    \/\/ Function to convert degrees to radians\n    public static double toRadians(double degree) {\n        return degree * Math.PI \/ 180;\n    }\n\n    \/\/ Function to calculate distance between two locations using Haversine formula\n    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {\n        \/\/ Radius of the Earth in miles\n        final double R = 3959.0;\n\n        \/\/ Convert degrees to radians\n        lat1 = toRadians(lat1);\n        lon1 = toRadians(lon1);\n        lat2 = toRadians(lat2);\n        lon2 = toRadians(lon2);\n\n        \/\/ Difference in latitudes and longitudes\n        double deltaLat = lat2 - lat1;\n        double deltaLon = lon2 - lon1;\n\n        \/\/ Haversine formula\n        double a = Math.pow(Math.sin(deltaLat \/ 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(deltaLon \/ 2), 2);\n        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\n\n        \/\/ Distance in miles\n        return R * c;\n    }\n\n    public static void main(String&#91;] args) {\n        Scanner scanner = new Scanner(System.in);\n\n        \/\/ Asking the user for input\n        System.out.print(\"Enter the latitude of the starting location: \");\n        double lat1 = scanner.nextDouble();\n        System.out.print(\"Enter the longitude of the starting location: \");\n        double lon1 = scanner.nextDouble();\n        System.out.print(\"Enter the latitude of the ending location: \");\n        double lat2 = scanner.nextDouble();\n        System.out.print(\"Enter the longitude of the ending location: \");\n        double lon2 = scanner.nextDouble();\n\n        \/\/ Calculate the distance\n        double distance = calculateDistance(lat1, lon1, lat2, lon2);\n\n        \/\/ Output the result\n        System.out.println(\"The distance is \" + distance + \" miles.\");\n\n        scanner.close();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Inputs<\/strong>: The program asks the user to input the latitudes and longitudes of the starting and ending locations. These values represent geographic coordinates (in degrees).<\/li>\n\n\n\n<li><strong>Conversion to Radians<\/strong>: The <code>toRadians<\/code> function is used to convert the given latitudes and longitudes from degrees to radians. This is necessary because trigonometric functions in Java, like <code>sin()<\/code> and <code>cos()<\/code>, work with radians.<\/li>\n\n\n\n<li><strong>Haversine Formula<\/strong>: In the <code>calculateDistance<\/code> function:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The latitudes and longitudes are used to compute the differences in their values.<\/li>\n\n\n\n<li>The formula calculates the great-circle distance between the two points, considering the Earth&#8217;s curvature.<\/li>\n\n\n\n<li>The radius of the Earth is assumed to be 3,959 miles, which is the average radius.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Output<\/strong>: After computing the distance using the Haversine formula, the program displays the result in miles.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>For the input:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the latitude of the starting location: 48.8567\nEnter the longitude of the starting location: 2.3508\nEnter the latitude of the ending location: 51.5072\nEnter the longitude of the ending location: 0.1275<\/code><\/pre>\n\n\n\n<p>The program calculates and outputs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The distance is 208.08639358288565 miles.<\/code><\/pre>\n\n\n\n<p>This program is an efficient way to compute the great-circle distance between two geographic locations using basic trigonometry in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-179677","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/179677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=179677"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/179677\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=179677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=179677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=179677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}