I teach AP CSA (first year) and have decided to give my kids a challenging (to me at least) assignment. They are supposed to use produce a print statement that will tell how long it takes a villain to travel a specific distance. I would like to have a solution BEFORE I give it to them.
The print statement was easy for me, and the program works "as intended"-- I can't help but think I made it too complicated and lost an opportunity to abstract a little instead of hard coding my longitude and latitude. Was there a better way for me to connect the GeoLocation start and GeoLocation end objects to my cities? See below for my code.
GeoLocation start = new GeoLocation(37.765469, 100.015167);
GeoLocation end = new GeoLocation(37.275280, 107.880066);
double distance = start.distanceFrom(end);
double travelTime = distance/15;
int travelReport = (int)travelTime;
WesternTown sweatyPost = new WesternTown();
sweatyPost.saloons = 2;
sweatyPost.sheriffs = 1;
sweatyPost.troublemakers = 5;
WesternTown dodgeCity = new WesternTown();
dodgeCity.saloons = 7;
dodgeCity.sheriffs = 2;
dodgeCity.troublemakers = 29;
dodgeCity.longitude = 100.015167;
dodgeCity.latitude = 37.765469;
WesternTown durango = new WesternTown();
durango.saloons = 4;
durango.sheriffs = 0;
durango.troublemakers = 6;
durango.longitude = 107.880066;
durango.latitude = 37.275280;
You can see the GeoLocation
as a property of a WesternTown
so it coule be an attribute :
public class WesternTown{
private int saloons;
private int sheriffs;
private int troublemakers;
private GeoLocation location;
// appropriate constructor with all :
public WesternTown(int saloons, int sheriffs, int troublemakers, Geolocation location){
this.saloons = saloons;
...
}
}
And you'll have
WesternTown dodgeCity = new WesternTown(7, 2, 29, new GeoLocation(37.765469, 100.015167));
WesternTown durango = new WesternTown(4, 0, 6, new GeoLocation(37.275280, 107.880066));
// 1. Leave method in GeoLocation class
double distance = dodgeCity.getLocation().distanceFrom(durango.getLocation());
// 2. or move it into WesternTown
double distance = dodgeCity.distanceFrom(durango);
Leave method in GeoLocation class
double distanceFrom(Geolocation other){ return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2)); }
or move it into WesternTown
// move all the method double distanceFrom(WesternTown other){ return Math.sqrt(Math.pow(this.location.getX() - other.location.getX(), 2) + Math.pow(this.location.getY() - other.location.getY(), 2)); } // or just call the Geolocation method double distanceFrom(WesternTown other){ return this.location.distanceFrom(other.location); }