JAVA: equals() and compareTo() methods always returning true
4 replies, posted
For the life of me, I can't figure out why my equals method and compareTo method always return true or 0. If I just compare the temperatures as is without converting to Celsius (so getC(this.temp) == getC(other.temp) becomes this.temp == other.temp) it works, so I'm assuming it has something to do with my conversion method. So somewhere in the lines following lines 57 and 101 there's a problem.
[code]public class Temp implements Comparable<Temp>
{
private static int tempCount = 0;
private int id = 0;
private char scale;
private double temp;
public Temp(double temp)
{
this(temp, 'C');
}
public Temp(char scale)
{
this(0, scale);
}
public Temp(double temp, char scale)
{
this.id = ++tempCount;
this.temp = temp;
this.scale = scale;
}
public Temp()
{
this(0, 'C');
}
public double getF()
{
if(this.scale == 'C' || this.scale == 'c')
{
return Math.round(((9*(temp/5.0))+32)*10)/10.0;
//return (9*(temp/5.0))+32;
} else
{
return this.temp;
}
}
public double getF(double temp)
{
if(this.scale == 'C' || this.scale == 'c')
{
return Math.round(((9*(temp/5.0))+32)*10)/10.0;
//return (9*(temp/5.0))+32;
} else
{
return this.temp;
}
}
public double getC()
{
if (scale == 'F' || scale == 'f')
{
return Math.round((5*(temp-32)/9.0)*10)/10.0;
//return 5*(temp-32)/9.0;
} else
{
return this.temp;
}
}
public double getC(double temp)
{
if (scale == 'F' || scale == 'f')
{
return Math.round((5*(temp-32)/9.0)*10)/10.0;
//return (9*(temp/5.0))+32;
} else
{
return this.temp;
}
}
public void setDegrees(double temp)
{
this.temp = temp;
}
public void setScale(char scale)
{
this.scale = scale;
}
public void setBoth(double temp, char scale)
{
this.temp = temp;
this.scale = scale;
}
public static int getTempCount() {
return tempCount;
}
@Override
public int compareTo(Temp obj)
{
Temp other = (Temp)obj;
if (getC(this.temp) > getC(other.temp))
{
return 1;
}
if (getC(this.temp) < getC(other.temp))
{
return -1;
}
return 0;
}
public boolean equals(Object obj)
{
if (!(obj instanceof Temp))
{
return false;
} else
{
Temp other = (Temp)obj;
return getC(this.temp) == getC(other.temp);
}
}
public String toString()
{
/*
TEMP OBJECT #1
IN C: 0.0
IN F: 32.0 */
return "Temp #" + id +
String.format("\nIn C:%5.00f", getC(this.temp)) +
String.format("\nIn F:%5.00f", getF(this.temp));
}
}[/code]
In line 57, you are comparing chars. In java comparing chars or strings is done with the .equals() function.
For line this would be
[code]scale.toLower().equals('f')[/code]
Line 101 should work, since you are comparing doubles.
Edit:
Actually, turns out I am wrong.
Chars can becompared with "==" since they are primitives. Equals is used for string comparison.
Yeah, the problem isn't in the char comparison, it's when I try to compare two different instances of the Temp class.
I use this to test them:
[code]
public class TestTemp
{
public static void main(String [] args)
{
Temp temp1 = new Temp(); // 0 C
Temp temp2 = new Temp(32); // 32 C
Temp temp3 = new Temp('F'); // 0 F
Temp temp4 = new Temp(32, 'F'); // 32 F
Temp temp5 = new Temp(); // 0 C
temp5.setDegrees(10);
temp5.setScale('F'); // 10 F
System.out.println(temp1);
System.out.println(temp2);
System.out.println(temp3);
System.out.println(temp4);
System.out.println(temp5);
System.out.println(temp1.equals(temp2)); // false
System.out.println(temp1.equals(temp3)); // false
System.out.println(temp1.equals(temp4)); // true
System.out.println(temp1.equals(temp5)); // false
System.out.println(temp1.compareTo(temp2)); //-1
System.out.println(temp1.compareTo(temp3)); // 1
System.out.println(temp1.compareTo(temp4)); // 0
System.out.println(temp1.compareTo(temp5)); // 1
}
}
[/code]
And I get this:
[code]
Temp #1
In C: 0
In F: 32
Temp #2
In C: 32
In F: 90
Temp #3
In C: -18
In F: 0
Temp #4
In C: 0
In F: 32
Temp #5
In C: -12
In F: 10
true
true
true
true
0
0
0
0
You have 5
[/code]
[code]public double getC(double temp)
{
if (scale == 'F' || scale == 'f')
{
return Math.round((5*(temp-32)/9.0)*10)/10.0;
//return (9*(temp/5.0))+32;
} else
{
return this.temp;
}
}[/code]
You're returning this.temp instead of the temp you passed into getC. You'd have to do
[code]return temp;[/code]
[editline]6th February 2015[/editline]
That's a pretty wonky way of doing it anyway, you don't really seem to need a getC method that has temp passed in as a parameter, in your equals method for example you could just do this instead:
[code]return this.getC() == other.getC();[/code]
[QUOTE=RautaPalli;47089077][code]public double getC(double temp)
{
if (scale == 'F' || scale == 'f')
{
return Math.round((5*(temp-32)/9.0)*10)/10.0;
//return (9*(temp/5.0))+32;
} else
{
return this.temp;
}
}[/code]
You're returning this.temp instead of the temp you passed into getC. You'd have to do
[code]return temp;[/code]
[editline]6th February 2015[/editline]
That's a pretty wonky way of doing it anyway, you don't really seem to need a getC method that has temp passed in as a parameter, in your equals method for example you could just do this instead:
[code]return this.getC() == other.getC();[/code][/QUOTE]
That works!
Sorry, you need to Log In to post a reply to this thread.