CS 46B - Lecture 4

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

Lecture 4 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

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

Lecture 4 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.compare(values[i], smallestSoFar) < 0)
            smallestSoFar = values[i];
      return smallestSoFar;
   }   
}

Inner Class

Example: Comparator

Anonymous Classes

Lecture 4 Clicker Question 4

Let's make Item an inner class of ArrayListAddressBook (from the lab):

public class ArrayListAddressBook 
{
   private class Item { String name; String key; String value; }
   ...
}

Which of the following is true?

  1. All methods of the ArrayListAddressBook class can construct and access Item objects
  2. Any code outside the ArrayListAddressBook class refers to the inner class as ArrayListAddressBook.Item
  3. The Item class is poorly designed because the instance variables of Item should have been declared as private
  4. The Item class should have been added to the AddressBook interface instead of the ArrayListAddressBook class

Mock Objects

Bonus: Java 8 Lambda Expressions

Lambda Expressions 2

Lecture 4 Clicker Question 5

Complete this program to find the word with the fewest vowels. Use a lambda expression.

Paste your lambda expression as a reply to the "clicker" question.

Before Next Class