An interface allows you to define a behavior that objects of different types have in common
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 sameString class also implements Comparable
"hop".compareTo("hope") returns a negative number"hope".compareTo("hope") returns zero"hope".compareTo("apple") returns a positve number
/**
A country with a name and area.
*/
public class Country implements Comparable
{
private String name;
private double area;
private int population;
/**
Constructs a country.
@param aName the name of the country
@param anArea the area of the country
* @param pop the population
*/
public Country(String aName, double anArea, int pop)
{
name = aName;
area = anArea;
population = pop;
}
/**
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;
}
/**
Gets the population of the country.
@return the population
*/
public int getPopulation()
{
return population;
}
/**
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;
}
}
public int compareTo(Object otherObject) { Country other = (Country) otherObject; if (area < other.area) return -1; if (area > other.area) return 1; return 0; }
public int compareTo(Object otherObject) { Country other = (Country) otherObject; return area - other.area; }
other.area. Could use other.getArea().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;
}
}
Comparablesmallest methodpublic class SmallestDemo
{
public static void main(String[] args)
{
String[] names = { "Fred", "Wilma", "Barney" };
Comparable first = Util.smallest(names);
System.out.println(first);
}
}
What if I want to print the length of the name?
What will this code print?
public class SmallestDemo
{
public static void main(String[] args)
{
String[] names = { "Fred", "Wilma", "Barney" };
Comparable first = Util.smallest(names);
System.out.println(first.length());
}
}
Open Netbeans. Create a test project. Create a class in the default package. Copy and paste the following code. Copy it from the D2L content area. Also create and copy classes for Util.java and Country.java
Then add the code so that you print the name of the smallest Country
public class SmallestCountry
{
public static void main(String[] args)
{
Comparable[] countries = new Comparable[3];
countries[0] = new Country("Mexico", 761602, 113724226);
countries[1] = new Country("Brazil", 3286470, 203429773);
countries[2] = new Country("Bolivia", 424162, 10118683);
Comparable smallest = Util.smallest(countries);
//write to code to print the name of the smallest country
}
}
System.out.println(((Country)smallest).getName());
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
objectsComparator interface
The Comparator interface has one method: compare()
public interface Comparator
{
int compare(Object object1, Object object2);
}
import java.util.*;
public class CountryComparatorByPopulation implements Comparator
{
public int compare(Object object1, Object object2)
{
Country country1 = (Country) object1;
Country country2 = (Country) object2;
return country1.getPopulation() - country2.getPopulation();
}
}
Arrays.sort can use a comparator object to sort an array.
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));Add a method to the Util class that takes an array and a Comparator object and finds the smallest object in the array
import java.util.Comparator;
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 static Object smallest(Object[] values, Comparator comp)
{
Object smallestSoFar = values[0];
for (Object obj : values)
if (comp.compare(obj, smallestSoFar) < 0)
smallestSoFar = obj;
return smallestSoFar;
}
}
public class SmallestCountryByPopulation
{
public static void main(String[] args)
{
Object[] countries = new Object[3];
countries[0] = new Country("Mexico", 761602, 113724226);
countries[1] = new Country("Brazil", 3286470, 203429773);
countries[2] = new Country("Bolivia", 424162, 10118683);
Object smallest = Util.smallest(countries,
new CountryComparatorByPopulation());
//print the name of the smallest country
System.out.println("Smallest Population: "
+ ((Country)smallest).getName());
}
}