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

**JAVA PROJECT You will be practicing the use of the built in List libraries in

ID: 3184718 • Letter: #

Question

**JAVA PROJECT

You will be practicing the use of the built in List libraries in java.util including ArrayList, List, and Collections. Modify template classes to find the distance between Philadelphia and the other cities. Your goal is to compute travel plans from Philadelphia to each provided city, sort the plans by distance, then print the results.

The following imports should already be included in your java file:

import java.util.ArrayList;

import java.util.List;

import java.util.Collections;

First, the City constructor. The constructor should simply set the fields (name, longitude and latitude), of the city class with the values of the constructor argument.

Next, implement the constructor for the TravelPlan class. The origin, destination, and dis- tance should be set in this constructor. To find the distance between two points, use the Euclidean distance formula. If you would like to use built in commands, you may use the Math library located in java.lang. (does not need to be imported). Relevant functions would be Math.pow and Math.sqrt.

Next, override the toString method to print out the origin, destination, and distance, and the compareTo method to compare the objects of the class based upon distance.

Finally, finish the file by storing the distance between Philadelphia and each of the other provided cities in an generic ArrayList of TravelPlan objects. Sort the list of TravelPlan objects using the Collections.sort static method. Finally, print the list to see the Travel Plans in order of shortest trip to longest.

Rubric:

Compiles without errors.

City class correctly implemented. TravelPlan class correctly implemented. driver program correctly implemented.

CITIES:

Philadelphia pivot city

Boston

New York

Washington DC

Atlanta

TEMPLATE:

public class Flight implements Comparable<Flight>

   {

      protected String fromVertex;

      protected String toVertex;

      protected int distance;

      public Flight(String fromVertex, String toVertex, int distance)

      {

      this.fromVertex = fromVertex;

      this.toVertex = toVertex;

      this.distance = distance;

      }

      public String getFromVertex()

      {

      return fromVertex;

      }

     

      public String getToVertex()

      {

      return toVertex;

      }

     

      public int getDistance()

      {

      return distance;

      }

     

      public void setFromVertex(String vertex)

      {

      fromVertex = vertex;

      }

      public void setToVertex(String vertex)

      {

      toVertex = vertex;

      }

      public void setDistance(int distance)

      {

      this.distance = distance;

      }

      public int compareTo(Flight other)

      {

      return (other.distance - this.distance); // shorter is better

      }

     

      @Override

      public String toString()

      {

      return (fromVertex + " " + toVertex + " " + distance);

      }

   }

Explanation / Answer

ANSWER:

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class Driver {

public static final double LENGTH_OF_A_DEGREE = 110.25;

public static void main(String[] args) {

List<TravelPlan> travelPlans = new ArrayList<>();

City philadelphia = new City("Philadelphia",39.9526,75.1652);

City boston = new City("Boston",42.3601,71.0589);

City newyork = new City("New York",40.7128,74.0060);

City washingtondc = new City("Washington DC",38.9072,77.0369);

City atlanta = new City("Atlanta",33.7490,84.3880);

double distance = DistanceBetweenPlaces(philadelphia, boston);

travelPlans.add(new TravelPlan(philadelphia, boston, distance));

distance = DistanceBetweenPlaces(philadelphia, newyork);

travelPlans.add(new TravelPlan(philadelphia, newyork, distance));

distance = DistanceBetweenPlaces(philadelphia, washingtondc);

travelPlans.add(new TravelPlan(philadelphia, washingtondc, distance));

distance = DistanceBetweenPlaces(philadelphia, atlanta);

travelPlans.add(new TravelPlan(philadelphia, atlanta, distance));

Collections.sort(travelPlans);

System.out.println(travelPlans.toString());

}

public static double DistanceBetweenPlaces(City city1,City city2)

{

double city1Lat = city1.getLatitude();

double city1Long = city1.getLongitude();

double city2Lat = city2.getLatitude();

double city2Long = city1.getLongitude();

double latDiff = city1Lat - city2Lat;

double longDiff = (city1Long - city2Long)*Math.cos(latDiff);

  

return LENGTH_OF_A_DEGREE * Math.sqrt(latDiff*latDiff+longDiff*longDiff);

}

}

public class City{

private String name;

private double longitude;

private double latitude;

public City(String name, double longitude, double latitude) {

super();

this.name = name;

this.longitude = longitude;

this.latitude = latitude;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the longitude

*/

public double getLongitude() {

return longitude;

}

/**

* @param longitude the longitude to set

*/

public void setLongitude(double longitude) {

this.longitude = longitude;

}

/**

* @return the latitude

*/

public double getLatitude() {

return latitude;

}

/**

* @param latitude the latitude to set

*/

public void setLatitude(double latitude) {

this.latitude = latitude;

}

@Override

public String toString() {

return ("name:"+name+" longitude:"+longitude+", latitude:"+latitude);

}

public class TravelPlan implements Comparable<TravelPlan>{

private City origin;

private City destination;

private double distance;

/**

* @return the origin

*/

public City getOrigin() {

return origin;

}

/**

* @param origin the origin to set

*/

public void setOrigin(City origin) {

this.origin = origin;

}

/**

* @return the destination

*/

public City getDestination() {

return destination;

}

/**

* @param destination the destination to set

*/

public void setDestination(City destination) {

this.destination = destination;

}

/**

* @return the distance

*/

public double getDistance() {

return distance;

}

/**

* @param distance the distance to set

*/

public void setDistance(double distance) {

this.distance = distance;

}

public TravelPlan(City origin, City destination, double distance) {

super();

this.origin = origin;

this.destination = destination;

this.distance = distance;

}

@Override

public int compareTo(TravelPlan o) {

if(o == null){

return -1;

}else{

return (this.distance>=o.distance?1:-1);

}

}

@Override

public String toString() {

return ("(Origin:"+origin+" Destination:"+destination+", Distance:"+distance+" km)");

}