package cse1030; public class Location { private double latitude; private double longitude; public Location() { this(0.0, 0.0); } public Location(double lati, double longi) { this.setLatitude(lati); this.setLongitude(longi); } public Location(Location other) { // Could use constructor chaining, but this would perform an unnecessary // validation of the latitude and longitude this.latitude = other.latitude; this.longitude = other.longitude; } public double getLatitude() { return this.latitude; } public final void setLatitude(double latitude) { if (!Location.latitudeOk(latitude)) { throw new IllegalArgumentException("latitude out of range"); } this.latitude = latitude; } public double getLongitude() { return this.longitude; } public final void setLongitude(double longitude) { if (!Location.longitudeOk(longitude)) { throw new IllegalArgumentException("longitude out of range"); } this.longitude = longitude; } @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(latitude); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(longitude); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Location other = (Location) obj; if (Double.doubleToLongBits(latitude) != Double .doubleToLongBits(other.latitude)) return false; if (Double.doubleToLongBits(longitude) != Double .doubleToLongBits(other.longitude)) return false; return true; } private static boolean latitudeOk(double lati) { return (lati >= -90.0 && lati <= 90.0); } private static boolean longitudeOk(double longi) { return (longi >= -90.0 && longi <= 90.0); } }