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

****MAKE SURE TO WRITE PROGRAM IN JAVA: ECLIPSE NEON**** ****MAKE SURE PROGRAM R

ID: 3838848 • Letter: #

Question

****MAKE SURE TO WRITE PROGRAM IN JAVA: ECLIPSE NEON****

****MAKE SURE PROGRAM RUNS WITHOUT ANY ERROS****

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

//City.java


public class City {

   String name;
   Double lon;
   Double lat;


   public City(){
      
   }
  
   public City(String name,Double lon,Double lat){ // Constructor
       this.name=name;
       this.lon=lon;
       this.lat=lat;
   }
  
   public void report(){ // Printing Data of cities
       System.out.println("City: "+this.name+"is at:"+this.lon+" "+this.lat);
   }
  
   public double distanceFrom(City city1, City city2){ // Calculating distance
       Double sq = Math.pow(city1.getLon().intValue()-city2.getLon().intValue(), 2)+Math.pow(city1.getLat().intValue()-city2.getLat().intValue(), 2);
       double dis = Math.sqrt(sq.intValue());
       return dis;
   }
  
  
   public String getName() {
       return name;
   }


   public Double getLon() {
       return lon;
   }

   public Double getLat() {
       return lat;
   }
  
}

//Assignment2.java

import java.util.Scanner;


public class Assignment2 {

   public static void main(String[] args) {
       City c = new City();
       int n;
       Scanner s = new Scanner(System.in);
       System.out.println("How many cities do you want to create?");
       n = s.nextInt();
      
       City city[] = new City[n]; // Creating array of cities
      
       for(int i=0;i<city.length;i++){ // Reading cities
           System.out.println("Enter City, long and lat:"+(i+1));
           city[i] = new City(s.next(),s.nextDouble(),s.nextDouble());
       }

       for(int i=0;i<city.length;i++){
           for(int j=0;j<city.length;j++){
               if(city[i].getName().equals(city[j].getName())){ Getting city1 and city2 and pass into distanceFrom()
                   continue;
               }else{
                   Double dis = c.distanceFrom(city[i], city[j]);
                   System.out.println(city[i].getName()+" is "+dis+" units away from "+city[j].getName());
               }
           }
       }
   }

}

Output:

How many cities do you want to create?
3
Enter City, long and lat:1
NYC 50 75
Enter City, long and lat:2
Chicago 25 10
Enter City, long and lat:3
LA 0 50
NYC is 69.6419413859206 units away from Chicago
NYC is 55.90169943749474 units away from LA
Chicago is 69.6419413859206 units away from NYC
Chicago is 47.16990566028302 units away from LA
LA is 55.90169943749474 units away from NYC
LA is 47.16990566028302 units away from Chicago