Reputation: 47
This is my prompt:
To create a Temperature converter, I was wondering what return a newTemp
object in the OtherUnit
means?
Implement a class named Temperature that represents a temperature in either Fahrenheit or Celsius. Allow the degrees to have decimal places (e.g. double) and use an enumeration to store the unit. Name the enumeration TemperatureUnit
with the possible values Celsius, Fahrenheit and Kelvin. Specifically, the Temperature class has the following instance variables (a.k.a. fields or data):
The degrees
(double)
The unit
(TemperatureUnit - from the enum you created)
The Temperature class will have methods to:
Create a new Temperature
(given a value for degrees and unit) [parameterized constructor]
Create a new Temperature
(given no parameters - initialize Temperature to 0.0 degrees Celsius) [default constructor]
Create a new Temperature
(from another Temperature) [copy constructor]
getDegrees
getUnit
setDegrees
setUnit
convertTo
(TemperatureUnit otherUnit) - this method will convert a degree in one unit to a degree in another (e.g. Fahrenheit to Celsuis) and returns true if a conversion was made, false otherwise (the only way a conversion is not made is if the units are the same (e.g. Fahrenheit to Fahrenheit)
inOtherUnit
(TemperatureUnit otherUnit) - this method will return a new Temperature object in the otherUnit, without changing the current object.
This is the code I already have:
public class Temperature {
private double degrees;
private TempUnit unit;
public Temperature(double degrees, TempUnit unit) {
this.degrees = degrees;
this.unit = unit;
}
// default constructor
public Temperature() {
this.degrees = 0.0;
this.unit = TempUnit.Celsius;
}
public Temperature(Temperature copyTemperature) {
this.degrees = copyTemperature.degrees;
this.unit = copyTemperature.unit;
}
public double getDegrees() {
return this.degrees;
}
public TempUnit unit() {
return this.unit;
}
public void setDegrees(double newDegrees) {
this.degrees = newDegrees;
}
public boolean convertTo(TempUnit otherUnit) {
if (this.unit == otherUnit) {
return false;
}
if (this.unit == TempUnit.Celsius && otherUnit == TempUnit.Fahrenheit) {
this.degrees = this.degrees * (9.0 / 5.0) + 32.0;
} else if (this.unit == TempUnit.Celsius && otherUnit == TempUnit.Kelvin) {
this.degrees += 273.15;
} else if (this.unit == TempUnit.Fahrenheit && otherUnit == TempUnit.Celsius) {
this.degrees = (this.degrees - 32.0) * 5.0/9.0;
} else if (this.unit == TempUnit.Fahrenheit && otherUnit == TempUnit.Kelvin) {
this.degrees = (this.degrees + 459.67) * 5.0/9.0;
} else if (this.unit == TempUnit.Kelvin && otherUnit == TempUnit.Celsius) {
this.degrees = this.degrees - 273.15;
} else if (this.unit == TempUnit.Kelvin && otherUnit == TempUnit.Fahrenheit) {
this.degrees = (this.degrees * 9.0/5.0) - 459.67;
}
this.unit = otherUnit;
return true;
}
public boolean another(TempUnit otherUnit) {
if (this.unit == otherUnit) {
return false;
}
else
return true;
}
Upvotes: 0
Views: 1060
Reputation: 52185
Essentially, the method otherUnit
will need to return a different Temperature
object which will the equivalent temperature in some other temperature unit. otherUnit
will essentially be a converter, that is, given the current state of the object, it will return a new object which has different values but is equivalent to the current one.
Thus, an object denoting a temperature of 0 degrees Celsius, could be converted to another object which is 32 degrees Fahrenheit, which in turn could be converted to an object which is 272 Kelvin. This means that despite the objects having different values, given the context (that is, temperature value and units) they are all equivalent.
Essentially, this would mean something like so:
public Temperature otherUnit(TempUnit unit) {
switch(unit) {
case Celcius: {
//If the current temperature is in Celcius, then return the current object.
if(this.unit == TempUnit.celcius) return this;
// return a new object which has the equivalent temperature in Celcius.
}
...
}
}
Upvotes: 0