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

Write a Java program that accepts the coordinates of two cities, and outputs the

ID: 3904897 • Letter: W

Question

Write a Java program that accepts the coordinates of two cities, and outputs the distance between them in miles.

Inputs: The names of two cities and their latitudes and longitudes.

Enter the first city: Enter the second city: Enter latitude and longitude of first city separated by a space (example 28.8 81.2): Enter latitude and longitude of second city separated by a space (example 28.8 81.2): Outputs: The distance between the two cities in miles, note: replace <> with the distance calculated The distance between the two cities is <> miles

Code the program in Eclipse to compute and display the distance between the two cities in miles. The distance in miles must be formatted to two decimal places using the printf command.

Explanation / Answer

package com.im.service;

import java.text.DecimalFormat;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;

public class DistanceCalculator {

private static DecimalFormat df2 = new DecimalFormat(".##");

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter first city");

String sourceCity=sc.next();

System.out.println("Enter second city");

String destCity=sc.next();

System.out.println("Enter latitude and Langitude of first city");

List<Double> latit=new ArrayList<>();

List<Double> languitude=new ArrayList<>();

double[] value = new double[2];

for(int i = 0; i < 2; i++)

{

value[i] = sc.nextDouble();

if(i==0){

latit.add(value[i]);

}

else{

languitude.add(value[i]);

}

}

System.out.println("Enter latitude and Langitude of second city");

value = new double[2];

for(int i = 0; i < 2; i++)

{

value[i] = sc.nextDouble();

if(i==0){

latit.add(value[i]);

}

else{

languitude.add(value[i]);

}

}

double distance=DistanceCalculator.distance(sourceCity,destCity,latit.get(0),latit.get(1),languitude.get(0), languitude.get(1));

System.out.println("the distance betwwen given two cities in miles is "+df2.format(distance));

}

public static double distance(String city1,String city2,double lat1, double lat2, double lon1,

double lon2) {

final int R = 6371; // Radius of the earth

double latDistance = Math.toRadians(lat2 - lat1);

double lonDistance = Math.toRadians(lon2 - lon1);

double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)

+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))

* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

double distance = R * c * 1000; // convert to meters

distance = Math.pow(distance, 2);

double distanceinMeters=Math.sqrt(distance);

//System.out.println(distanceinMeters);

  

// If you want in kilometrs you need to return this distanceInKm

// double distanceInKm=distanceinMeters/1000;

// System.out.println(distanceInKm);

  

double distanceInMiles=distanceinMeters/1609.344;

//System.out.println(distanceInMiles);

return distanceInMiles;

}

}

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