Reputation: 65
I need to write a Temperature.equals() method that compares the first temperature to the second temperature in the parenthesis, but I don't know the proper syntax to.
Part 1:
public Temperature(double degreesd, char typec)
{
degrees = degreesd;
type = typec;
}
public void set(double degreesd, char typec)
{
degrees = degreesd;
type = typec;
}
Part 2:
t1.set(100, 'C');
t2.set(212, 'F');
System.out.println(t1.equals(t2));
The equals() method I need to finish:
public boolean equals(Temperature t2)
{
return ( Temperature.getC() == t2.getC() );
// getC() is a function that returns degrees in Celsius as a double.
}
I don't know the proper syntax to pass t1 into where "Temperature.getC()" is. Any help is appreciated.
Upvotes: 1
Views: 130
Reputation: 1325
It's easier using the getC()
method, then you don't have to get into trouble like me:
public boolean equals(Temperature t1) {
return this.getC() == t1.getC();
}
As for the euqals
method (without using getC()
):
public boolean equals(Temperature t1) {
if (this.typec == t1.typec) {
return (this.degrees == t1.degrees);
} else {
double fah = t1.typec == 'F' ? t1.degrees : t2.degrees;
double cel = t1.typec == 'C' ? t1.degrees : t2.degrees;
fah = (fah - 32) * 5 / 9;
return fah == cel;
}
}
Upvotes: 1
Reputation:
public boolean equals(Temperature t2)
{
return (this.getC() == t2.getC());
// getC() is a function that returns degrees in Celsius as a double.
}
Use this
to reference the object on which you call equals
. In the case of t1.equals(t2)
, this
will be t1
.
Upvotes: 3