public class Temperature extends Object
Modifier and Type | Field and Description |
---|---|
static String |
CELCIUS
The abbreviation of Celcius.
|
static String |
FAHRENHEIT
The abbreviation of Fahrenheit
|
Constructor and Description |
---|
Temperature()
Initializes the temperature to 0 degrees Celcius.
|
Temperature(double temp,
String units)
Initializes the temperature to the given numeric value having the given
units.
|
Temperature(Temperature other)
Initializes the temperature by copying the numeric value and units of
another temperature.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj)
Compares two temperatures for equality.
|
double |
getTemperature()
Gets the numeric value of the temperature using the current units.
|
String |
getUnits()
Gets the units of the temperature.
|
void |
setTemperature(double temp)
Sets the numeric value of the temperature using the current units.
|
void |
setUnits(String units)
Set the units of the temperature.
|
static double |
toCelcius(double degF)
Converts a value of degrees Fahrenheit to the equivalent value of degrees
Celcius.
|
static double |
toFahrenheit(double degC)
Converts a value of degrees Celcius to the equivalent value of degrees
Fahrenheit.
|
String |
toString()
Returns a string made up of the numeric value of the temperature followed
by its units.
|
public static final String CELCIUS
public static final String FAHRENHEIT
public Temperature()
public Temperature(Temperature other)
other
- the temperature to copypublic Temperature(double temp, String units)
temp
- the temperatureunits
- the units (Temperature.CELCIUS
or
Temperature.FAHRENHEIT
)IllegalArgumentException
- if units
is not one of
Temperature.CELCIUS
or
Temperature.FAHRENHEIT
Temperature.CELCIUS
or Temperature.FAHRENHEIT
public double getTemperature()
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();
public final void setTemperature(double temp)
t
is 15.0
degrees Celcius in the example below:
Temperature t = new Temperature(0.0, Temperature.CELCIUS); t.setTemperature(15.0);
temp
- the numeric value of the temperaturepublic String getUnits()
Temperature.CELCIUS
if the temperature is in
Celcius, or Temperature.FAHRENHEIT
if the
temperature is in Fahrenheit.public final void setUnits(String units)
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
units
- the units of the temperature (Temperature.CELCIUS
or Temperature.FAHRENHEIT
)IllegalArgumentException
- if units
is not one of
Temperature.CELCIUS
or
Temperature.FAHRENHEIT
Temperature.CELCIUS
or Temperature.FAHRENHEIT
public static double toCelcius(double degF)
C = (F - 32) × (5 / 9)
where F is degrees Fahrenheit and C is degrees Celcius.
degF
- a value of degrees Fahrenheitpublic static double toFahrenheit(double degC)
F = C × (9 / 5) + 32
where C is degrees Celcius and F is degrees Fahrenheit.
degC
- a value of degrees Celciuspublic String toString()
-12.5C 73.01234F
are returned for temperatures of -12.5
degrees Celcius and
73.01234
degrees Fahrenheit, respectively.
public boolean equals(Object obj)
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.