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

Compute the distance between following cities using point2D concept in Classes a

ID: 3719702 • Letter: C

Question

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

Explanation / Answer

import java.util.*;

import java.lang.*;

class DistanceCalculator

{

String city1,city2 ;

double lon1, lat1, lon2, lat2;

public DistanceCalculator (c1,lon1,lat1,c2,lon2,lat2){

this.city1 = c1;

this.lon1 = lon1;

this.lat1 = lat1;

this.city2 = c1;

this.lon2 = lon1;

this.lat2 = lat1;

}

//Method to calculate the distance

double calculate (lon1,lat1,lon2,lat2) {

int r = 6371 // Radius of Earth

double dLon = Math.toRadians(lon2-lon1) //longitude difference

double dLat = Math.toRadians(lat2-lat1) //longitude difference

double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2);

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

double d = R * c ; // Distance in km

return d;

}

//Method to print result

void Print(city1,lon1,lat1,city2,lon2,lat2) {

double d = this.calculate(lon1,lat1,lon2,lat2)

System.out.println("Distance between "+city1+" and "+city2+" is "+d+" "+"km.");

}

public static void main (String[] args) throws java.lang.Exception

{

Scanner sc = new Scanner(System.in);

String city1 = sc.next()

double lat1 = Integer.parseInt(sc.next().replaceAll("[^0-9]", ""));

double lon1 = Integer.parseInt(sc.next().replaceAll("[^0-9]", ""));

String city2 = sc.next()

double lat2 = Integer.parseInt(sc.next().replaceAll("[^0-9]", ""));

double lon2 = Integer.parseInt(sc.next().replaceAll("[^0-9]", ""));

DistanceCalculator obj = new DistanceCalculator(city1,lat1,lon1,city2,lat2,lon2)

obj.Print()

    }

}

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