package test2;
/**
* A class that represents temperatures in degrees Celcius or degrees
* Fahrenheit.
*
*
*/
public class Temperature {
/**
* The abbreviation of Celcius.
*/
public static final String CELCIUS = "C";
/**
* The abbreviation of Fahrenheit
*/
public static final String FAHRENHEIT = "F";
private double degC;
private double degF;
private String units;
/**
* Initializes the temperature to 0 degrees Celcius.
*/
public Temperature() {
this(0.0, Temperature.CELCIUS);
}
/**
* Initializes the temperature by copying the numeric value and units of
* another temperature.
*
* @param other
* the temperature to copy
*/
public Temperature(Temperature other) {
this(other.getTemperature(), other.getUnits());
}
/**
* Initializes the temperature to the given numeric value having the given
* units.
*
* @param temp
* the temperature
* @param units
* the units (Temperature.CELCIUS or Temperature.FAHRENHEIT)
* @throws IllegalArgumentException
* if units is not one of Temperature.CELCIUS or
* Temperature.FAHRENHEIT
*/
public Temperature(double temp, String units) {
this.setUnits(units);
this.setTemperature(temp);
}
/**
* Gets the numeric value of the temperature using the current units. For
* instance, the value of degC
is 0.0
and the
* value of degF
is 32.0
in the following example:
*
*
* Temperature t = new Temperature(0.0, Temperature.CELCIUS); * double degC = t.getTemperature(); * * Temperature u = new Temperature(32.0, Temperature.FAHRENHEIT); * double degF = u.getTemperature(); ** * @return the numeric value of the temperature using the current units */ public double getTemperature() { if (this.units.equals(Temperature.CELCIUS)) { return this.degC; } return this.degF; } /** * Sets the numeric value of the temperature using the current units. For * instance, the final numeric value of
t
is 15.0
* degrees Celcius in the example below:
*
* * Temperature t = new Temperature(0.0, Temperature.CELCIUS); * t.setTemperature(15.0); ** * @param temp * the numeric value of the temperature */ public final void setTemperature(double temp) { if (this.units.equals(Temperature.CELCIUS)) { this.degC = temp; this.degF = Temperature.toFahrenheit(temp); } else { this.degF = temp; this.degC = Temperature.toCelcius(temp); } } /** * Gets the units of the temperature. * * @return Temperature.CELCIUS if the temperature is in * Celcius, or Temperature.FAHRENHEIT if the * temperature is in Fahrenheit. */ public String getUnits() { return this.units; } /** * Set the units of the temperature. This method has the side effect that * the numeric value of the temperature changes to reflect the change in * units. For instance, changing the units of
0
degrees Celcius
* to Fahrenheit causes the value of degF
to be
* 32.0
(because 0C is equal to 32F):
*
* * Temperature t = new Temperature(0, Temperature.CELCIUS); * t.setUnits(Temperature.FAHRENHEIT); * double degF = t.getTemperature(); // degF is equal to 32.0 * String units = t.getUnits(); // units is equal to Temperature.FAHRENHEIT ** * @param units * the units of the temperature (Temperature.CELCIUS * or Temperature.FAHRENHEIT) * @throws IllegalArgumentException * if units is not one of * Temperature.CELCIUS or Temperature.FAHRENHEIT * */ public final void setUnits(String units) { if (!units.equals(Temperature.CELCIUS) && !units.equals(Temperature.FAHRENHEIT)) { throw new IllegalArgumentException(); } this.units = units; } /** * Converts a value of degrees Fahrenheit to the equivalent value of degrees * Celcius. The formula to convert degrees Fahrenheit to degrees Celcius is: * *
* C = (F - 32) × (5 / 9) * *
* where F is degrees Fahrenheit and C is degrees Celcius. * * @param degF * a value of degrees Fahrenheit * @return the equivalent value in degrees Celcius */ public static double toCelcius(double degF) { return (degF - 32.0) * 5.0 / 9.0; } /** * Converts a value of degrees Celcius to the equivalent value of degrees * Fahrenheit. The formula to convert degrees Celcius to degrees Fahrenheit * is: * *
* F = C × (9 / 5) + 32 * *
* where C is degrees Celcius and F is degrees Fahrenheit. * * @param degC * a value of degrees Celcius * @return the equivalent value in degrees Fahrenheit */ public static double toFahrenheit(double degC) { return degC * 9.0 / 5.0 + 32.0; } /** * Returns a string made up of the numeric value of the temperature followed * by its units. For example, the Strings * *
* -12.5C * 73.01234F ** *
* are returned for temperatures of -12.5
degrees Celcius and
* 73.01234
degrees Fahrenheit, respectively.
*
* @return a string representation of the temperature
*/
@Override
public String toString() {
if (this.getUnits().equals(Temperature.CELCIUS)) {
return this.getTemperature() + Temperature.CELCIUS;
} else {
return this.getTemperature() + Temperature.FAHRENHEIT;
}
}
/**
* Compares two temperatures for equality. Two temperatures are considered
* equal if they represent the same temperature expressed in degrees
* Celcius. For instance, the values of eq1
and
* eq2
are true
in the following example:
*
*
* Temperature t = new Temperature(0.0, Temperature.CELCIUS); * Temperature u = new Temperature(32.0, Temperature.FAHRENHEIT); * boolean eq1 = t.equals(u); * boolean eq2 = u.equals(t); ** *
* because 32
degrees Fahrenheit is equivalent to
* 0
degrees Celcius, and vice versa.
*
* @param obj an object to compare with this Temperature
* @return true if obj is a Temperature object with the same
* temperature when expressed in degrees Celcius as this temperature
*
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Temperature other = (Temperature) obj;
if (Double.doubleToLongBits(this.degC) != Double.doubleToLongBits(other.degC))
return false;
return true;
}
}