Excercise: Listed next is the skeleton for a class named City. Each city has a name and temperature: public class City { private String cityName; private int temperature; } Flesh out the class with appropriate accessors, constructors, and mutators. Next, modify the class so that it implements the Comparable interface. The order between instances of the City class depends on the temperature. Test your class by creating an array of sample cities and sort them in an ascending order using Arrays.sort
Excercise:
Listed next is the skeleton for a class named City. Each city has a name and temperature:
public class City { private String cityName; private int temperature; } |
Flesh out the class with appropriate accessors, constructors, and mutators. Next, modify the class so that it implements the Comparable interface. The order between instances of the City class depends on the temperature. Test your class by creating an array of sample cities and sort them in an ascending order using Arrays.sort
Notes:
The language used for this question is Java. So far, this is the City class after fleshing it out:
public class City implements Comparable<City> { public City() { public City(String cityName, double temperature) { public String getCityName() { public double getTemperature() { public void setName(String cityName) { public void setTemperature(double temperature) { public int compareTo(City other) { if(temperature < otherCity.temperature) |
The array of the cities in the testCity class:
City[] cities = new City[6]; cities[0] = new City("Dammam", 29.48); |
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images