CS 46B - Lecture 1

Cover page image

Cay S. Horstmann

Pre-class reading

The Comparable Interface Type

Implementing the Comparable Interface Type

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

Generic Comparable

Finding the Smallest Comparable

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

Clicker Question #1

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);
   }
}
  1. Fred
  2. Barney
  3. Nothing—the program doesn't compile
  4. Nothing—the program compiles but it throws a “bad cast” exception when it runs

Clicker Question #2

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 other objects that implement the Comparable interface, e.g. Country objects


Comparators

Example: Comparing Countries by Name

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

Clicker Question #3

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

Finding the Smallest, Again

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

Lab

???