Java country arraylist sorting 1. Write a class called Countries that implements
ID: 3822722 • Letter: J
Question
Java country arraylist sorting
1. Write a class called Countries that implements the Comparable interface. The class should contain two fields CountryName(String) and Code(Int). Write the necessary constructors, accessors and mutators methods. Following is the list of methods to be defined in the class: GetName, SetName, GetCode, SetCode,
2. Write a client class that creates an ArrayList of 10 Country objects called list1. Print the list. Call Collections.sort(list1), to sort the elements according to CountryName. Print the sorted list.
Explanation / Answer
Country.java
public class Country implements Comparable<Country> {
// Declaring instance variables
private String countryName;
private int code;
// Parameterized constructor
public Country(String countryName, int code) {
super();
this.countryName = countryName;
this.code = code;
}
// getters and setters
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
// Compares the Country names
@Override
public int compareTo(Country o) {
return this.getCountryName().compareTo(o.getCountryName());
}
}
__________________
Test.java
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String[] args) {
//Creating an ArrayList which holds Country Objects
ArrayList<Country> list1=new ArrayList<Country>();
//Creating 10 Country Objects by passing Country names and codes
Country c1=new Country("India",356);
Country c2=new Country("Cuba",192);
Country c3=new Country("Pakistan",586);
Country c4=new Country("Kenya",404);
Country c5=new Country("Somalia",706);
Country c6=new Country("Uruguay",858);
Country c7=new Country("Viet Nam",704);
Country c8=new Country("Yemen",887);
Country c9=new Country("Zambia",894);
Country c10=new Country("Afghanistan",004);
//Adding the country objects to ArrayList
list1.add(c1);
list1.add(c2);
list1.add(c3);
list1.add(c4);
list1.add(c5);
list1.add(c6);
list1.add(c7);
list1.add(c8);
list1.add(c9);
list1.add(c10);
System.out.println("Before Sorting Based on Country Names :");
for(Country c: list1)
{
System.out.println(c.getCountryName()+" "+c.getCode());
}
Collections.sort(list1);
System.out.println(" After Sorting Based on Country Names :");
for(Country c: list1)
{
System.out.println(c.getCountryName()+" "+c.getCode());
}
}
}
___________________
Output:
Before Sorting Based on Country Names :
India 356
Cuba 192
Pakistan 586
Kenya 404
Somalia 706
Uruguay 858
Viet Nam 704
Yemen 887
Zambia 894
Afghanistan 4
After Sorting Based on Country Names :
Afghanistan 4
Cuba 192
India 356
Kenya 404
Pakistan 586
Somalia 706
Uruguay 858
Viet Nam 704
Yemen 887
Zambia 894
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.