Interfaces

9.1 - 9.4

Interfaces (9.1 - 9.4)

An interface allows you to define a behavior that objects of different types have in common

Comparable interface

Comparable interface

String class also implements Comparable

Implementing the Comparable interface

 
/**
   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;
   }
}

 

Implementing the Comparable interface

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;
}

What is the point of using interfaces?

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;
   }   
}

Using the smallest method

public class SmallestDemo
{
   public static void main(String[] args)
   {
      String[] names = { "Fred", "Wilma", "Barney" };
      Comparable first = Util.smallest(names);
      System.out.println(first);
   }
}

8/27 Class Question 1

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());
   }
}
  1. 4
  2. 6
  3. Nothing - the code will not compile
  4. Nothing - the code compiles but throws an exception

8/27 Class Question 2

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
   }
}

Answer to Class Question #2

 System.out.println(((Country)smallest).getName());

8/27 Lecture 1 Question 3

What is the advantage of using the Comparable interface in the Util.smallest method?

  1. None—it would have been simpler to implement Util.smallest(String[] values)
  2. None—it would have been simpler to implement Util.smallest(Measurable[] values)
  3. None—it would have been simpler to implement Util.smallest(Object[] values)
  4. The method is reusable for arrays containing objects that implement the Comparable interface, e.g. Country objects

Comparator interface

 

Comparator interface

The Comparator interface has one method: compare()

public interface Comparator
{
   int compare(Object object1, Object object2);
}

Implementing a Comparator interface

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();
   }
}

8/27 Lecture 1 Question 4

Arrays.sort can use a comparator object to sort an array.

Which of the following sorts an array of countries by name?

  1. Arrays.countryComparatorByName(countries);
  2. Arrays.sort(countries, CountryComparatorByName);
  3. Arrays.sort(countries, new CountryComparatorByName());
  4. Arrays.sort(new CountryComparatorByName(countries));

Using a Comparator object to find smallest object

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;
   }
}

Find the country with the smallest population

 

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());
   }
}

Homework