Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the following Country class. Write a comparator that defines an ordering on Country objects. Use lexicographic ordering on (name, area). That is it first
Consider the following Country class. Write a comparator that defines an ordering on Country objects. Use lexicographic ordering on (name, area). That is it first compares names. If they are the same (ignoring case), then compare the area. API is on the last page. class Country {private String name; private int area; public Country (String n, int a) {name = n; area = a;} public string getName {} {return name;} public int getArea {} {return area;} public String costring {} {return name + " " + area;}} public class CountryTester {public static void main (String args) {Comparator comp =//your work goes here. ArrayList countries = new ArrayList(); countries.add(new Country ("a", 30)); countries.add(new Country ("b", 10)); countries.add(new Country ("a", 20)); Collections.sort(countries, comp); System.out.printin(countries);//prints [a 20, a 30, b 10]}}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started