import java.beans.*; /** * The Max class finds and keeps track of the maximum value * in an array of Doubles * * @author Stuart Hansen * @version September 2008 */ public class Max { // We maintain the maximum in order to have an old // maximum for the property change event private Double maximum; // We use pcs to facilitate our property change events private PropertyChangeSupport pcs; // The default constructor public Max() { maximum = new Double (Double.NaN); pcs = new PropertyChangeSupport (this); } // Return the current Maximum public Double getMaximum() { return maximum; } // Return the maximum as a String public String toString() { return maximum.toString(); } // Find the average of an enumeration of Doubles public Double findMax(Double [] dArr) { Double oldMax = maximum; if (dArr.length > 0) { Double tempMax = dArr[0]; for (Double d : dArr) { if (d > tempMax) tempMax = d; } maximum = tempMax; } else maximum = Double.NaN; firePropertyChange (new PropertyChangeEvent (this, "maximum", oldMax, maximum)); return maximum; } // Fire a property change public void firePropertyChange (PropertyChangeEvent e) { pcs.firePropertyChange(e); } // Add a property change listener public void addPropertyChangeListener(PropertyChangeListener pcl) { pcs.addPropertyChangeListener(pcl); } // Delete a property change listener public void removePropertyChangeListener(PropertyChangeListener pcl) { pcs.removePropertyChangeListener(pcl); } }