Respuesta :
Answer:
Explanation:
The following code is written in Java and performs all of the methods that were asked in the question correctly. It also outputs the exact output that is provided in the question as an example...
package sample;
import java.util.ArrayList;
public class RoadTripTester
{
public static void main(String[] args)
{
RoadTrip rt = new RoadTrip();
rt.addStop("San Francisco", 37.7833, -122.4167);
rt.addStop("Los Angeles", 34.052235, -118.243683);
rt.addStop("Las Vegas", 36.114647, -115.172813);
for (GeoLocation x: rt.getRoadTrip()) {
System.out.println(x.toString());
}
System.out.println("\nStops: " + rt.getNumberOfStops());
System.out.println("Total Miles: "+ rt.getTripLength());
}
}
class RoadTrip
{
public static ArrayList<GeoLocation> roadTrip = new ArrayList<GeoLocation>();
static int i = 0;
// Create a GeoLocation and add it to the road trip
public void addStop(String name, double latitude, double longitude){
GeoLocation location = new GeoLocation(name, latitude, longitude);
roadTrip.add(location);
i += 1;
}
public ArrayList<GeoLocation> getRoadTrip() {
return roadTrip;
}
// Get the total number of stops in the trip
public int getNumberOfStops() {
return i;
}
// Get the total miles of the trip
public double getTripLength(){
double numberOfMiles = 0;
for (int x = 0; x < roadTrip.size()-1; x++) {
if ((x-1) != roadTrip.size()) {
numberOfMiles += roadTrip.get(x).distanceFrom(roadTrip.get(x+1));
}
}
return numberOfMiles;
}
}
class GeoLocation
{
public static final double RADIUS = 3963.1676;
private String name;
private double latitude;
private double longitude;
public GeoLocation(String name, double theLatitude, double theLongitude)
{
this.name = name;
latitude = theLatitude;
longitude = theLongitude;
}
public String getName() {
return this.name;
}
// returns a string representation of this geo location
public String toString()
{
return this.name + " (" + longitude + ", " + latitude + ")";
}
// returns the distance in miles between this geo location and the given
// other geo location
public double distanceFrom(GeoLocation other)
{
double lat1 = Math.toRadians(latitude);
double long1 = Math.toRadians(longitude);
double lat2 = Math.toRadians(other.latitude);
double long2 = Math.toRadians(other.longitude);
// apply the spherical law of cosines with a triangle composed of the
// two locations and the north pole
double theCos = Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2);
double arcLength = Math.acos(theCos);
return arcLength * RADIUS;
}
}
The program is an illustration of classes, methods and array lists
Classes are templates used to create methods, while array lists are simply arrays that can be resized
The program written in Java, where comments are used to explain each line is as follows:
import java.util.ArrayList;
public class Main{
//The main method begins here
public static void main(String[] args){
//This creates a RoadTrip object
RoadTrip rt = new RoadTrip();
//These initialize the RoadTrip object
rt.addStop("San Francisco", 37.7833, -122.4167);
rt.addStop("Los Angeles", 34.052235, -118.243683);
rt.addStop("Las Vegas", 36.114647, -115.172813);
//The following iteration prints each object
for (GeoLocation x: rt.getRoadTrip()) {
System.out.println(x.toString());
}
//This prints a new line
System.out.println();
//This prints the number of stops
System.out.println("Stops: " + rt.getNumberOfStops());
//This prints the total number of miles
System.out.println("Total Miles: "+ rt.getTripLength());
}
}
//This defines the RoadTrip class
class RoadTrip{
//This creates an ArrayList for the RoadTrip
public static ArrayList<GeoLocation> roadTrip = new ArrayList<GeoLocation>();
//This declares and initializes i to 0
int i = 0;
// This creates a GeoLocation and add it to the road trip
public void addStop(String name, double latitude, double longitude){
GeoLocation location = new GeoLocation(name, latitude, longitude);
roadTrip.add(location);
i += 1;
}
//This defines an ArrayList method that returns the roadTrip
public ArrayList<GeoLocation> getRoadTrip() {
return roadTrip;
}
// This defines an int method that returns the total number of stops
public int getNumberOfStops() {
return i;
}
// The following method returns the total miles of the trip
public double getTripLength(){
double numberOfMiles = 0;
for (int x = 0; x < roadTrip.size()-1; x++) {
if ((x-1) != roadTrip.size()) {
numberOfMiles += roadTrip.get(x).distanceFrom(roadTrip.get(x+1));
}
}
return numberOfMiles;
}
}
//This defines the GeoLocation class
class GeoLocation{
//The next four lines declare the required variables
public static final double RADIUS = 3963.1676;
private String name;
private double latitude;
private double longitude;
//This defines the GeoLocation method
public GeoLocation(String name, double theLatitude, double theLongitude){
//The next three lines initialize name, latitude and longitude
this.name = name;
latitude = theLatitude;
longitude = theLongitude;
}
//The following string method returns the name of the geo location
public String getName() {
return this.name;
}
// The following string method returns a string representation of this geo location
public String toString(){
return this.name + " (" + longitude + ", " + latitude + ")";
}
// The following double method returns the distance in miles between the geo locations
public double distanceFrom(GeoLocation other){
double lat1 = Math.toRadians(latitude);
double long1 = Math.toRadians(longitude);
double lat2 = Math.toRadians(other.latitude);
double long2 = Math.toRadians(other.longitude);
// This calculates the distance between the two locations and the north pole
double theCos = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2);
double arcLength = Math.acos(theCos);
//This returns the calculated distance
return arcLength * RADIUS;
}
}
Read more about classes, methods and array lists at:
https://brainly.com/question/8045197