# OOP newbie, booleans not working

• 04-01-2011, 07:09 PM
gnng
OOP newbie, booleans not working
I am simply trying to test if two temperatures, one in F and one in C, are the same. But no matter what it comes out true. I am confused on how to get it compare the two values, and do the math to determine if the values are the same.

Thanks - Gary :confused:

Code:

``` public class Temperature {         private double tempValue;         private char scale;         //constructors public Temperature(double tempValue) {         this.tempValue = tempValue;         scale = 'C'; } public Temperature(char scale) {         tempValue = 0;         this.scale = scale;//should this be C? } public Temperature(double tempValue, char scale) {         this.tempValue = tempValue;         this.scale = scale; } public Temperature() {         tempValue = 0;         scale = 'C'; } //mutators or setters  public void setTempValue(double tempValue)         {                 this.tempValue = tempValue;         } public void setScale(char scale)         {                 this.scale = scale;         } public void setBoth(double tempValue, char scale) {         this.tempValue = tempValue;         this.scale = scale; } //getters public double getTempValue() //not sure I need this but put it in so boolean works {         double tempValue = 0;         return tempValue; } public double getTemperatureF() {         double degreesF = (9 * (tempValue / 5)) + 32;         return degreesF; } public double getTemperatureC() {         double degreesC = 5 *(tempValue - 32) /9;         return degreesC; } //boolean needed to 1) test whether two temps are equal, and 2) test if one temp is less han the other public boolean equals(Temperature otherTemperature) {         return(this.getTempValue() == otherTemperature.getTempValue()); } public boolean isLessThan(Temperature otherTemperature) {         return(getTemperatureF() < getTemperatureC()); } public boolean isGreaterThan(Temperature otherTemperature) {         return(getTemperatureF() > getTemperatureC()); } //toString public String toString() {   return tempValue + " " + scale; } }```
AND HERE IS THE PART THAT TESTS IT

Code:

```import java.util.*; public class TempDemo { public static void main(String[] args) {         //use all of the constructors         Temperature zeroC = new Temperature();     zeroC.setTempValue(0.0);         System.out.println("Freezing Water in Celsius: " + zeroC.toString());           Temperature freezeF = new Temperature(32.0, 'F');     freezeF.setTempValue(32.0);         freezeF.setScale('F');     System.out.println("Freezing Water in Farenheit: " + freezeF.toString());         Temperature negativeC = new Temperature();     negativeC.setTempValue(-40.0);     System.out.println("Negative number in Celcius: " + negativeC.toString());         Temperature negativeF = new Temperature();         negativeF.setTempValue(-40.0);         negativeF.setScale('F');     System.out.println("Negative number in Farenheit: " + negativeF.toString());         Temperature positiveC = new Temperature();         positiveC.setTempValue(100.0);         System.out.println("Positive number in Celsius: " + positiveC.toString());         Temperature positiveF = new Temperature();         positiveF.setTempValue(212.0);         negativeF.setScale('F');     System.out.println("Positive number in Farenheit: " + positiveF.toString()); // test for equality     System.out.println(zeroC + " = " + freezeF + " is " + zeroC.equals(freezeF));         System.out.println(negativeC + " = " + negativeF + " is " + negativeC.equals(negativeF)); } }```
• 04-01-2011, 07:19 PM
Zack
You're comparing two objects' "getTempValue" results, which is always zero:
Code:

```public double getTempValue() //not sure I need this but put it in so boolean works {         double tempValue = 0;         return tempValue; }```
Since 0 == 0 always, it will always be true.
• 04-01-2011, 07:20 PM
JosAH
Quote:

Originally Posted by gnng
Code:

```public double getTempValue() //not sure I need this but put it in so boolean works {         double tempValue = 0;         return tempValue; }```

This method always returns 0.0 which is not what you want.

kind regards,

Jos
• 04-01-2011, 07:20 PM
ozzyman
Quote:

Code:

```public double getTempValue() //not sure I need this but put it in so boolean works {         double tempValue = 0;         return tempValue; } public boolean equals(Temperature otherTemperature) {         return([COLOR="red"]this[/COLOR].getTempValue() == otherTemperature.getTempValue()); }```

no you don't need to put that in to make your boolean work, your boolean isn't working because this.getTempValue() returns Temperature.getTempValue() -> the method you just made up.

i'm not sure how to code in the way you want so that you can use object.equals(otherObject) but if you're willing to take a different approach just throw both temperatures into parameters e.g. boolean isEqual(Temperature a, Temperature b);
• 04-01-2011, 07:21 PM
lovelesh
You are trying to achieve very little from too much of code.

You seriously need to refactor your code.

Just a few things which one must take care of:
1. If you are not using the arguments in a method, then do not declare them as part of method signature. The thing you are doing in isLessThan & isGreaterThan methods.
2. No need to write three different methods for comparison. All three comparisons can be done in a single method, Just put a little thought into this.
3. You have unwanted contructors in your class. Just think do you need them all.
• 04-01-2011, 09:54 PM
gnng
OK, so I am pretty confused! Let me first state that I am taking this class online, meaning NO teacher interaction. So I am reading a book and doing the best I can trying to figure out how to do this.

Could someone explain what code I should change and what I should change it to? For example, you note that this code is wrong - double tempValue = 0; But I don't know enough to know what the right answer is. I had thought you had to declare it as 0 based on previous exercises. I can't change it to (), and I can't just put a semi-colon after tempValue.