In Java Compute the distance between following cities using point2D concept in C
ID: 3717293 • Letter: I
Question
In Java
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 Distance is 38.7 miles Kelley laland Eucld Aqulla Burton Midd efield Sandusky Lora n Avor Chagrin Fals Vormilicn Amherst ??? North ?4 | ?) Elyria 1.1 m le Souch Amiherst Hram Mantua Collins Norwa k Pittofield Logrange Valey Cty Brunswick 48 minmStreetsbore 44 min Raverna West Branch lena State Park Cra Fitchille New London Huntington Sperce willard New H Norton RaExplanation / Answer
PROGRAM CODING:
import java.util.Scanner;
public class PointData {
/* Insert your name here */
private double x;
private double y;
// TODO Implement Point.Point(double x, double y)
/**
* instantiate a point "this.x" refers to the instance variable of the
* object x refers to the parameter same for this.y and y
*/
public PointData(double x, double y) {
// System.out.println("Point(x,y) not implemented yet");
this.x = x;
this.y = y;
}
// TODO Implement Point.Point()
/**
* Point() creates the origin by appropriately calling the general Point
* constructor
*/
public PointData() {
// System.out.println("Point() not implemented yet");
this.x = 0;
this.y = 0;
}
// TODO Implement Point.getX
/**
* @return x
*/
public double getX() {
// System.out.println("getX not implemented yet");
return x;
}
// TODO Implement Point.getY
/**
* @return y
*/
public double getY() {
// System.out.println("getY not implemented yet");
return y;
}
// Given Point.toString
/**
* convert to String
*/
public String toString() {
return "(" + x + "," + y + ")";
}
// TODO Implement Point.equals
/**
*
* @param p
* other point
* @return test for equality using epsilon, because we are dealing with
* doubles,so roundoff can occur
*/
public boolean equals(PointData p) {
if (this.x == p.x && this.y == p.y)
return true;
return false;
}
// Given equals(Object o)
/**
* We need this equals method for ArrayList<Point>, because the generic
* ArrayList is really an ArrayList of Object. In the case of equals, the
* signature is public boolean equals(Object o) and the method below
* overrides the Object equals method and the calls the class's
* equals(Point) method
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj instanceof PointData) {
PointData p = (PointData) obj;
return equals(p);
}
return false;
}
// TODO Implement Point.euclidDist
/**
*
* @param p
* @return Euclidean distance of this point to point p
*/
public double euclidDist(PointData p) {
return Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
}
/**
* @param args:
* no args
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter City Name: ");
String str = keyboard.nextLine();
System.out.println("Enter x cordinate: ");
int x = keyboard.nextInt();
System.out.println("Enter y cordinate: ");
int y = keyboard.nextInt();
PointData p1 = new PointData(x, y);
keyboard.nextLine();
System.out.println("Enter City Name: ");
String str1 = keyboard.nextLine();
System.out.println("Enter x cordinate: ");
int x1 = keyboard.nextInt();
System.out.println("Enter y cordinate: ");
int y1 = keyboard.nextInt();
PointData p2 = new PointData(x1, y1);
keyboard.nextLine();
System.out.println("Enter City Name: ");
String str2 = keyboard.nextLine();
System.out.println("Enter x cordinate: ");
int x2 = keyboard.nextInt();
System.out.println("Enter y cordinate: ");
int y2 = keyboard.nextInt();
PointData p3 = new PointData(x2, y2);
System.out.println(
"Euclidean distance between " + str + " and " + str2 + p1 + " and " + p2 + ": " + p1.euclidDist(p2));
System.out.println("Euclidean distance between " +str1 + " and " + str2 + p2 + " and " + p3 + ": " + p2.euclidDist(p3));
System.out.println("Euclidean distance between " +str2 + " and " + str + p3 + " and " + p1 + ": " + p1.euclidDist(p3));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.