Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a java class named Temperature that represents a temperature in either

ID: 3812890 • Letter: I

Question

Implement a java 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 enum to store the unit. Name the enumeration TemperatureUnit with the possible values CELSIUS and FAHRENHEIT. 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 Celsius) 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.
equals - method to check if one Temperature is the same as another by determining if their degrees are the same, after comparing them with the same unit.
toString - method to turn a Temperature into a string for display, e.g. display as "Temperature [0.0 degrees Celsius]"

**Create a driver class (TemperatureDemo) to make three temperature objects (in Celsius and Fahrenheit), two of which are the same essential temperatures, but in different units. Check the two equivalent temperatures for equality. Check two different temperatures for inequality. Print all 3 temperatures to the console.***

Below is a class diagram showing the fields and methods of the Temperature class as well as the enum for TemperatureUnit:

Explanation / Answer

Please find the required solution: /** * Temparature class */ class Temparature { // instance varaibles private double degrees; private TemperatureUnit unit; //default constructor public Temparature() { degrees = 0.0d; unit = TemperatureUnit.CELSIUS; } //parametrised constructor public Temparature( double degrees, TemperatureUnit unit ) { this.degrees = degrees; this.unit = unit; } //getter and setter methods public double getDegrees() { return degrees; } public void setDegrees( double degrees ) { this.degrees = degrees; } public TemperatureUnit getUnit() { return unit; } public void setUnit( TemperatureUnit unit ) { this.unit = unit; } //equals convertTo method public boolean convertTo( TemperatureUnit unit ) { if( this.unit != unit ) { if( unit == TemperatureUnit.CELSIUS ) { degrees = (degrees - 32) * (5 / 9); } if( unit == TemperatureUnit.FAHRENHEIT ) { degrees = (degrees * (9 / 5)) + 32; } this.unit = unit; return true; } return false; } // convert to other unit public Temparature toOtherUnit( TemperatureUnit unit ) { if( this.unit == unit ) return new Temparature(this.degrees, this.unit); if( unit == TemperatureUnit.CELSIUS ) { return new Temparature((degrees - 32) * 5 / 9, TemperatureUnit.CELSIUS); } else { return new Temparature((degrees * 9 / 5) + 32, TemperatureUnit.FAHRENHEIT); } } //equals method @Override public boolean equals( Object obj ) { if( !(obj instanceof Temparature) ) return false; Temparature t1 = (Temparature) obj; if( t1.unit == this.unit ) return t1.degrees == this.degrees; Temparature t2 = this, t3 = t1; if( this.unit != TemperatureUnit.CELSIUS ) { t2 = t2.toOtherUnit(TemperatureUnit.CELSIUS); } if( t3.unit != TemperatureUnit.CELSIUS ) { t3 = t3.toOtherUnit(TemperatureUnit.CELSIUS); } return t2.degrees == t3.degrees; } //tostring method @Override public String toString() { return "Temparature[" + "degrees=" + degrees + " " + unit + ']'; } } //enum for degrees enum TemperatureUnit { CELSIUS, FAHRENHEIT } /** * Driver class */ public class TemperatureDemo { public static void main( String[] args ) { Temparature obj1 = new Temparature(); obj1.setDegrees(32); obj1.setUnit(TemperatureUnit.CELSIUS); Temparature obj2 = new Temparature(64, TemperatureUnit.FAHRENHEIT); Temparature obj3 = new Temparature(17.77777777777778, TemperatureUnit.CELSIUS); System.out.println("Temparature Object obj1:" + obj1); System.out.println("Temparature Object obj2:" + obj2); System.out.println("Temparature Object obj3:" + obj3); System.out.println(" ---------------Checking equality"); if( obj2.equals(obj3) ) System.out.println(obj2 + " & " + obj3 + " are equal"); else System.out.println(obj2 + " & " + obj3 + " not are equal"); System.out.println(" ---------------Checking inequality"); if( obj1.equals(obj2) ) System.out.println(obj1 + " & " + obj2 + " are equal"); else System.out.println(obj1 + " & " + obj2 + " are not equal"); } } Sample output: Temparature Object obj1:Temparature[degrees=32.0 CELSIUS] Temparature Object obj2:Temparature[degrees=64.0 FAHRENHEIT] Temparature Object obj3:Temparature[degrees=17.77777777777778 CELSIUS] ---------------Checking equality Temparature[degrees=64.0 FAHRENHEIT] & Temparature[degrees=17.77777777777778 CELSIUS] are equal ---------------Checking inequality Temparature[degrees=32.0 CELSIUS] & Temparature[degrees=64.0 FAHRENHEIT] not are equal

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote