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
**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
{
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);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
