/* Attached is a class definition that encapsulates \"Temperature\" Temperature.
ID: 3747514 • Letter: #
Question
/*
Attached is a class definition that encapsulates "Temperature"
Temperature.java
Write a method 'equals (Temperature other)' that returns true if the temperatures of the calling object and argument object 'other' are equal.
public boolean equals (Temperature other) {
return _value == other._value;
}
public boolean equals (Temperature other) {
if ( _scale == other._scale )
{
return _value == other._value;
}
else
{
return this.getTemperatureCelsius() ==other.getTemperatureCelsius();
}
}
public boolean equals (Temperature other) {
if ( _scale != other._scale )
{
return _value == other._value;
}
else
{
return this.getTemperatureCelsius() ==other.getTemperatureCelsius();
}
}
public boolean equals (Temperature other) {
if ( _scale == other._scale && _value == other._value)
{
return True;
}
else
{
return False;
}
}
public boolean equals (Temperature other) {
return _value == other._value;
}
public boolean equals (Temperature other) {
if ( _scale == other._scale )
{
return _value == other._value;
}
else
{
return this.getTemperatureCelsius() ==other.getTemperatureCelsius();
}
}
public boolean equals (Temperature other) {
if ( _scale != other._scale )
{
return _value == other._value;
}
else
{
return this.getTemperatureCelsius() ==other.getTemperatureCelsius();
}
}
public boolean equals (Temperature other) {
if ( _scale == other._scale && _value == other._value)
{
return True;
}
else
{
return False;
}
}
Explanation / Answer
public boolean equals (Temperature other) { if ( _scale == other._scale ) { return _value == other._value; } else { return this.getTemperatureCelsius() ==other.getTemperatureCelsius(); } } Explanation: --------------- This is the better way of comparing if two Temperature objects are equal. If scale is same then check if values or same or not. If scale is not equal then convert temperatures to celsius and then check if the values or same or not. Answer: Option 2.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.