/** * An instance of this class represents a temperature that can be expressed * in either the Fahrenheit or Celsius scale. * * @author P.M.J. (modified slightly by R.W.M) * @version 2009-08-19 */ public class Temperature { // Instance Variable(s) // ==================== private double fahrenheitReading; // Constructor Method(s) //====================== /** * Initializes the temperature to wather's freezing point. */ public Temperature() { fahrenheitReading = 32.0; } /** * Initializes the temperature to the given Fahrenheit reading. */ public Temperature(double f) { fahrenheitReading = f; } // Observer Method(s) //=================== /** * Returns the temperature as a Fahrenheit reading. */ public double getAsFahrenheit() { return fahrenheitReading; } /** * Returns the temperature as a Celsius reading. */ public double getAsCelsius() { return fahrenheitToCelsius(fahrenheitReading); } // Mutator Method(s) //================== /** * Sets the temperature to the given Fahrenheit reading. */ public void setAsFahrenheit(double f) { fahrenheitReading = f; } /** * Sets the temperature to the given Celsius reading. */ public void setAsCelsius(double c) { fahrenheitReading = celsiusToFahrenheit(c); } // Conventional Method(s) //======================= /** * Returns the current Fahrenheit reading as a printable string value. */ public String toString() { return getAsFahrenheit() + " F"; } // Private Method(s) //================== /** * Given a Fahrenheit reading, returns the corresponding Celsius reading. */ private double fahrenheitToCelsius(double f) { return (5.0 * (f - 32.0)) / 9.0; } /** * Given a Celsius reading, returns the corresponding Fahrenheit reading. */ private double celsiusToFahrenheit(double c) { return ((9.0 * c) / 5.0) + 32.0; } }