Table 1: City Class Class City implements Comparable<City> Private Instance Vari
ID: 3698803 • Letter: T
Question
Table 1: City Class
Class City implements Comparable<City>
Private Instance Variables
Description
String city
City name
double gpsX
GPS Longitude in Decimal Degrees
double gpsY
GPS Latitude in Decimal Degrees
int population
Population
int vertIndex
Index in Vertex ArrayList
Public Instance Methods
Description
City (String name, double x,
double y, int size,
int index)
Constructor to initialize each instance variable. Do not round these numbers, here – do the rounding in printCity().
String getCity()
Getter for city
double getLongitude()
Getter for gpsX
double getLatitude()
Getter for gpsY
int getPopulation()
Getter for population
int getIndex()
Getter for vertIndex
void setIndex (int index)
Setter for vertIndex
int compareTo (City c)
Compares population
boolean equals (Object c)
Override equals comparing city name
String toString()
Return City name
String printCity()
Return City object information with the GPS coordinates rounded to 2 decimal places, as
Mars Hill: [1]:[-82.55, 35.83]:(1869)
public class City implements Comparable<City>
{
private String city; // City name
private double gpsX; // Longitide in degrees
private double gpsY; // Latitide in degrees
private int population; // Population size
private int vertIndex; // index in the Vertex ArrayList
/** Construct a City object and initialize the instance variables */
public City (String name, double x, double y, int size, int index)
{
// add code
}
/** Return the City name */
public String getCity()
{
// add code
}
/** Return the City longitude */
public double getLongitude()
{
// add code
}
/** Return the City latitude */
public double getLatitude()
{
// add code
}
/** Return the City poulation */
public int getPopulation()
{
// add code
}
/** Return the City index in the vertex ArrayList */
public int getIndex()
{
// add code
}
/** Set the City index of the vertex ArrayList */
public void setIndex (int index)
{
// add code
}
/** Compare the City poulations */
@Override
public int compareTo (City c)
{
// add code
}
/** Return true, when the City names are the same */
@Override
public boolean equals (Object c)
{
// add code
}
/**
* Return the City info as a String
* Example: Mars Hill: [1]:[82.55 W, 35.83 N]:(1869)
* Round the GPS coordinates to 2 decimal places for display
*/
public String printCity()
{
// add code as above
}
/** Return the City name as a String */
public String toString()
{
// add code
}
}
Class City implements Comparable<City>
Private Instance Variables
Description
String city
City name
double gpsX
GPS Longitude in Decimal Degrees
double gpsY
GPS Latitude in Decimal Degrees
int population
Population
int vertIndex
Index in Vertex ArrayList
Public Instance Methods
Description
City (String name, double x,
double y, int size,
int index)
Constructor to initialize each instance variable. Do not round these numbers, here – do the rounding in printCity().
String getCity()
Getter for city
double getLongitude()
Getter for gpsX
double getLatitude()
Getter for gpsY
int getPopulation()
Getter for population
int getIndex()
Getter for vertIndex
void setIndex (int index)
Setter for vertIndex
int compareTo (City c)
Compares population
boolean equals (Object c)
Override equals comparing city name
String toString()
Return City name
String printCity()
Return City object information with the GPS coordinates rounded to 2 decimal places, as
Mars Hill: [1]:[-82.55, 35.83]:(1869)
Explanation / Answer
public class City implements Comparable { private String city; // City name private double gpsX; // Longitide in degrees private double gpsY; // Latitide in degrees private int population; // Population size private int vertIndex; // index in the Vertex ArrayList /** Construct a City object and initialize the instance variables */ public City (String name, double x, double y, int size, int index) { city = name; gpsX = x; gpsY = y; vertIndex = index; } /** Return the City name */ public String getCity() { return city; } /** Return the City longitude */ public double getLongitude() { return gpsX; } /** Return the City latitude */ public double getLatitude() { return gpsY; } /** Return the City poulation */ public int getPopulation() { return population; } /** Return the City index in the vertex ArrayList */ public int getIndex() { return vertIndex; } /** Set the City index of the vertex ArrayList */ public void setIndex (int index) { vertIndex = index; } /** Compare the City poulations */ @Override public int compareTo (City c) { return Integer.compare(population, c.population); } /** Return true, when the City names are the same */ @Override public boolean equals (Object c) { return city.equals(((City)c).city); } /** * Return the City info as a String * Example: Mars Hill: [1]:[82.55 W, 35.83 N]:(1869) * Round the GPS coordinates to 2 decimal places for display */ public String printCity() { return String.format("%s: [%d]:[%.2f W, %.2f N]:(%d)", city, vertIndex, gpsX, gpsY, population); } /** Return the City name as a String */ public String toString() { return city; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.