**Create the following program in Java: Eclipse Neon** Create a new class called
ID: 3861511 • Letter: #
Question
**Create the following program in Java: Eclipse Neon**
Create a new class called “City” that can be used to keep track of location information for a given city. Your class should include the following – be sure to comment your class appropriately:
String name
double lon (for longitude)
double lat (for latitude)
A constructor that accepts a name, lon and lat value and stores them in the instance variables for the object
A method that reports the current position of a city. Here’s a method header to get you started:
A method that computes the distance from the lon and lat of one city to the lon and lat of another city. Use the standard distance formula to compute this value (let’s pretend that the cities lie on a flat plane and not on a sphere!) Here’s a method header to get you started:
Create a new class called “Assignment2”. Do the following in this class:
Prompt the user to enter in a number of cities (i.e. How many cities do you want to create?)
Next, ask the user to enter in the name, lon and lat for each city. Note that you will probably need to use two scanners since you are asking for both String and double data. Create a new City object and store it in an array that is designed to hold objects of type City (i.e. City[] myCities)
Iterate through your array of Cities and ask each city to report its position.
Iterate through your array of Cities and compute the distance from each city to each other city. Ensure that you do not calculate the distance from a given city back to itself (i.e. no need to compute distance between NYC and NYC – the result will be zero) — Here’s a sample running of your program.
Explanation / Answer
public class City
{
//member variables
String name;
double lon ;//(for longitude)
double lat ;//(for latitude)
//constructor to initialize meber variables
public City(String c_name, double longitude, double latitude){
name=c_name;
lon=longitude;
lat=latitude;
}
//member method to report the location
public void report(){
System.out.println(name+":"+lat+","+lon);
}
//meber method to calculate the distance
public double distanceFrom(City anotherCity){
double lat1,lon1;// latitude and longitude of another city
lat1=anotherCity.lat;
lon1=anotherCity.lon;
double d;// stores the distance between two points
//using the standard distance formula i.e Euclidean's distance formula which can be changed as per the rquirements.
d=Math.sqrt((lat1-lat)*(lat1-lat)+(lon1-lon)*(lon1-lon));
return d;
}
}
public class Assignment2 {
public static void main(String args[]){
int n,i,j;
String name;
double lat,lon,d;
City myCities[]=new City[5];
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of cities");
n=sc.nextInt();//n stores number of cities.
//to get name, latitude and longitude for eac city
for(i=0;i<n;i++){
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
System.out.println("Enter city name");
name=sc1.nextLine();
System.out.println("Enter latitude");
lat=sc2.nextDouble();
System.out.println("Enter longitude");
lon=sc2.nextDouble();
myCities[i]= new City(name,lon,lat);
}
//to report the position of each city
for(i=0;i<n;i++)
myCities[i].report();
// to calculate the distance of a city from every other city
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(i==j)
continue; //to avoid calculation of distance between same city
d= myCities[i].distanceFrom(myCities[j]);//to calculate distance of city i from city j
System.out.println("Distance between "+myCities[i].name+" and "+myCities[j].name+" is "+d );
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.