logo Practice-It logo

Location

Related Links:
Author: Marty Stepp

Suppose that a class Location has been defined for storing information about global map locations. Each location keeps track of its name (a string), its latitude (a real number), and its longitude (a real number). The class includes the following members:

NameDescription
private String name name of location
private double latitude location's latitude
private double longitude location's longitude
public Location(String name, double latitude, double longitude) constructs a location with given name, latitude, longitude
public String getName() returns the name
public double getLatitude() returns the latitude
public double getLongitude() returns the longitude
public String toString() returns a String representation of the location

Your task is to modify the class to be Comparable by adding an appropriate compareTo method. Locations should be ordered first by latitude with locations closer to the equator considered less than locations farther from the equator. When the latitudes are equal, you should examine longitudes with locations closer to the prime meridian considered less than locations farther from the prime meridian.

Latitudes are expressed relative to the equator with negative latitudes considered to be in the southern hemisphere and positive latitudes considered to be in the northern hemisphere. Thus, 0 represents the equator, -90 represents the South Pole and 90 represents the North Pole. Longitudes are expressed relative to what is known as the prime meridian (the line of longitude that runs through Greenwich, England), with positive longitudes considered to be in the western hemisphere and negative longitudes considered to be in the eastern hemisphere. Longitudes can range from -180 to 180.

You may assume that your constructor is passed legal values for latitude and longitude. You are allowed to use the Math.abs method to find the absolute value of a number.

Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.