

"Hell" < "Hello" < "Hill"
Comparable interface
public interface Comparable
{
int compareTo(Object otherObject)
}
a.compareTo(b) returns a negative int if
a < b, a positive int if a >
b, and zero if they are the same/**
A country with a name and area.
*/
public class Country implements Comparable
{
/**
Constructs a country.
@param aName the name of the country
@param anArea the area of the country
*/
public Country(String aName, double anArea)
{
name = aName;
area = anArea;
}
/**
Gets the name of the country.
@return the name
*/
public String getName()
{
return name;
}
/**
Gets the area of the country.
@return the area
*/
public double getArea()
{
return area;
}
/**
Compares two countries by area.
@param other the other country
@return a negative number if this country has a smaller
area than otherCountry, 0 if the areas are the same,
a positive number otherwise
*/
public int compareTo(Object otherObject)
{
Country other = (Country) otherObject;
if (area < other.area) return -1;
if (area > other.area) return 1;
return 0;
}
private String name;
private double area;
}
compareTo
public int compareTo(Object otherObject)
{
Country other = (Country) otherObject;
if (area < other.area) return -1;
if (area > other.area) return 1;
return 0;
}
public class Country implements Comparable<Country>
{
...
public int compareTo(Country other)
{
if (area < other.area) return -1;
if (area > other.area) return 1;
return 0;
}
}
public class Util
{
public static Comparable smallest(Comparable[] values)
{
Comparable smallestSoFar = values[0];
for (int i = 1; i < values.length; i++)
if (values[i].compareTo(smallestSoFar) < 0)
smallestSoFar = values[i];
return smallestSoFar;
}
}
public class SmallestDemo
{
public static void main(String[] args)
{
String[] names = { "Fred", "Wilma", "Barney" };
Comparable first = Util.smallest(names);
System.out.println(first);
}
}
What will this program print?
public class SmallestDemo
{
public static void main(String[] args)
{
String[] names = { "Fred", "Wilma", "Barney" };
String first = Util.smallest(names);
System.out.println(first);
}
}
What is the advantage of using the Comparable interface in
the Util.smallest method?
Util.smallest(String[] values)Util.smallest(Measurable[] values)Util.smallest(Object[] values)Comparable interface, e.g. Country
objectsCountry implements ComparableCountry to Util.smallest or
Arrays.sortComparable whenever the sort
order changespublic interface Comparator
{
int compare(Object object1, Object object2);
}
import java.util.*;
public class CountryComparatorByName implements Comparator
{
public int compare(Object object1, Object object2)
{
Country country1 = (Country) object1;
Country country2 = (Country) object2;
return country1.getName().compareTo(country2.getName());
}
}
Which of the following sorts an array of countries by name?
Arrays.countryComparatorByName(countries);Arrays.sort(countries,
CountryComparatorByName);Arrays.sort(countries, new CountryComparatorByName());Arrays.sort(new CountryComparatorByName(countries));public class Util
{
public static Object smallest(Object[] values, Comparator comp)
{
Object smallestSoFar = values[0];
for (int i = 1; i < values.length; i++)
if (comp(values[i], smallestSoFar) < 0)
smallestSoFar = values[i];
return smallestSoFar;
}
}
