(1) Create a City class with the following instance variables and methods: Insta
ID: 3919611 • Letter: #
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 variables, and toString method
The City class implements the Comparable<City> 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<City> 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<City> 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
Explanation / Answer
I have written programs for the above. Please check below.
City.java
public class City implements Comparable<City>{
private String name;
private String province;
@Override
public String toString() {
return "City [name=" + name + ", province=" + province + "]";
}
public City(String name, String province) {
super();
this.name = name;
this.province = province;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
@Override
public int compareTo(City c) {
// TODO Auto-generated method stub
if (this.getName() == c.getName()){
return this.getProvince().compareTo(c.getProvince());
}
else
return this.getName().compareTo(c.getName());
}
}
CityDriver.java
import java.util.ArrayList;
import java.util.Collections;
public class CityDriver {
public static void main(String[] args) {
// TODO Auto-generated method stub
//City c1 = new City("", province)
ArrayList<City> a1 = new ArrayList<City>();
City c1 = new City("Borden", "Ontario");
a1.add(c1);
c1 = new City("Borden", "Prince Edward Island");
a1.add(c1);
c1 = new City("Borden", "Saskatchewan");
a1.add(c1);
c1 = new City("Beaumont", "Alberta");
a1.add(c1);
c1 = new City("Beaumont", "Newfoundland");
a1.add(c1);
c1 = new City("Beaumont", "Quebec");
System.out.println("Before sorting");
for(City c:a1){
System.out.println(c.toString());
}
System.out.println(" After sorting");
Collections.sort(a1);
for(City c:a1){
System.out.println(c.toString());
}
}
}
Output:
Before sorting
City [name=Borden, province=Ontario]
City [name=Borden, province=Prince Edward Island]
City [name=Borden, province=Saskatchewan]
City [name=Beaumont, province=Alberta]
City [name=Beaumont, province=Newfoundland]
After sorting
City [name=Beaumont, province=Alberta]
City [name=Beaumont, province=Newfoundland]
City [name=Borden, province=Ontario]
City [name=Borden, province=Prince Edward Island]
City [name=Borden, province=Saskatchewan]
Question 3:
CityComparator.java
import java.util.Comparator;
public class CityComparator implements Comparator<CityComparator>{
private String name;
private String province;
@Override
public String toString() {
return "City [name=" + name + ", province=" + province + "]";
}
public CityComparator(String name, String province) {
super();
this.name = name;
this.province = province;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public CityComparator(){
}
@Override
public int compare(CityComparator c1, CityComparator c2) {
// TODO Auto-generated method stub
if (c1.getProvince() == c2.getProvince()){
return c1.getName().compareTo(c2.getName());
}
else
return c1.getProvince().compareTo(c2.getProvince());
}
}
CityCompDriver.java
import java.util.ArrayList;
import java.util.Collections;
public class CityCompDriver {
public static void main(String[] args) {
// TODO Auto-generated method stub
//City c1 = new City("", province)
ArrayList<CityComparator> a1 = new ArrayList<CityComparator>();
CityComparator c1 = new CityComparator("Borden", "Ontario");
a1.add(c1);
c1 = new CityComparator("Borden", "Prince Edward Island");
a1.add(c1);
c1 = new CityComparator("Borden", "Saskatchewan");
a1.add(c1);
c1 = new CityComparator("Beaumont", "Alberta");
a1.add(c1);
c1 = new CityComparator("Beaumont", "Newfoundland");
a1.add(c1);
c1 = new CityComparator("Beaumont", "Quebec");
System.out.println("Before sorting");
for(CityComparator c:a1){
System.out.println(c.toString());
}
System.out.println(" After sorting");
Collections.sort(a1,new CityComparator());
for(CityComparator c:a1){
System.out.println(c.toString());
}
}
}
Output:
Before sorting
City [name=Borden, province=Ontario]
City [name=Borden, province=Prince Edward Island]
City [name=Borden, province=Saskatchewan]
City [name=Beaumont, province=Alberta]
City [name=Beaumont, province=Newfoundland]
After sorting
City [name=Beaumont, province=Alberta]
City [name=Beaumont, province=Newfoundland]
City [name=Borden, province=Ontario]
City [name=Borden, province=Prince Edward Island]
City [name=Borden, province=Saskatchewan]
Please test with complete data and let me know any issues. Thank you. ALl the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.