Question: (1) Create a City class with the following instance variables and methods: Instance variables: name (String), province (String) Methods: constructor, get/set methods for the instance

(1) Create a City class with the following instance variables and methods: Instance variables: name (String), province (String) Methods: constructor, get/set methods for the instance variables, and toString method The City class implements the Comparable interface. Thus, implement compareTo method. First, compare city names. If there are two cities with the same name, compare their province names. (2) Write a driver program that populates an ArrayList with cities (include the cities with the same names but different provinces). Sort the ArrayList using a Java built-in method. (3) This time, you want to sort the list of cities first by province names, and then by city names. You are not allowed to modify the compareTo method. Instead, create a CityComparator class that implements the Comparator interface. (4) Write a driver program that uses the same City objects from (2) but that sorts the ArrayList using the CityComparator. Compare the result with that of (2). For the program you need city name with providence.

Here is a list of cities / towns / places in Canada with same name but in different provinces:

Places in Canada with the same name in THREE provinces (8 cities/towns/places):

Borden - Ontario, Prince Edward Island, Saskatchewan

Beaumont - Alberta, Newfoundland, Quebec

Princeton - British Columbia, Newfoundland, Ontario

Richmond - British Columbia, Ontario, Quebec

Stratford - Ontario, Prince Edward Island, Quebec

Trout Lake - Alberta, British Columbia, Northwest Territories

Victoria - British Columbia, Newfoundland, Ontario

Windsor - Nova Scotia, Ontario, Quebec

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!