The standard Java library provides a Comparable interface:


public interface Comparable
{
   /**
      
Compares this object with another.
      \@param other the object to be compared
      \@return a negative integer, zero, or a positive integer if this object
      is less than, equal to, or greater than, other
   */
   public int compareTo(Object other);
}


Modify the DataSet class of Section 9.1 to accept Comparable objects. With this interface, it is no longer meaningful to compute the average. The DataSet class should record the minimum and maximum data values. Test your modified DataSet class by adding a number of String objects. (The String class implements the Comparable interface.)

Complete the following files:

DataSet.java

/** Computes the minimum and maximum of a set of Comparable values. */ public class DataSet { . . . /** Constructs an empty data set. */ public DataSet() { . . . } /** Adds a data value to the data set. @param x a data value */ public void add(Comparable x) { . . . } /** Gets the largest of the added data. @return the maximum or null if no data has been added */ public Comparable getMaximum() { . . . } /** Gets the largest of the added data. @return the maximum or null if no data has been added */ public Comparable getMinimum() { . . . } }

DataSetTester2.java

Use the following file:

DataSetTester.java

/**
   This program demonstrates the use of the DataSet that accepts
   instances of the Comparable interface.
*/
public class DataSetTester
{
   public static void main(String[] args)
   {
      DataSet data = new DataSet();

      data.add("Helen");
      data.add("Andy");
      data.add("Robert");
      data.add("Vicky");      

      Comparable max = data.getMaximum();
      Comparable min = data.getMinimum();
      System.out.println("Maximum: " + max);
      System.out.println("Expected: Vicky");
      System.out.println("Minimum: " + min);
      System.out.println("Expected: Andy");
    }
}