Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# How can I get my temperature converter program to display negative numbers

Lee Sigauke
Ranch Hand
Posts: 30
I wrote this code and when I convert small values of Fahrenheit to Celsius it gives me zero instead of the negative number, I looked for a sample program online and there isn't anything special about it so what am I doing wrong ? My code is still not complete yet so please forgive the imperfections.

My Code :

online code

Carey Brown
Bartender
Posts: 1686
22
• 1
Change line 102 to

Lee Sigauke
Ranch Hand
Posts: 30
Carey Brown wrote:Change line 102 to

Thanks that did the trick. Why did I have to write the numbers as doubles though ? the one I found online isn't written like that. I also thought that since the variables are already declared as double I wouldn't need to do that

Campbell Ritchie
Sheriff
Posts: 50258
79
I didn’t read the whole code, but I can see a serious design error in it. You ought not to be calculating anything inside a frame class. You ought to have a temperature converter class which does the calculations. This is how you ought to have developed your app
• 1: Create a Temperature class, or a TemperatureConverter class.
• 2: Demonstrate the workings of this class with a TemperatureDemo class.
• 3: Put a GUI round that.
• I’t let you work out how the TemperatureScales enum and the Temperature class work. If you use a TemperatureConverter class, that will work completely differently.

Winston Gutkowski
Bartender
Posts: 10527
64
• 1
Lee Sigauke wrote:Thanks that did the trick. Why did I have to write the numbers as doubles though ? the one I found online isn't written like that. I also thought that since the variables are already declared as double I wouldn't need to do that

Because integer division is different from FP division - ie, it truncates. It's also generally worth making all components of an expression the same type, because it saves the code from having to convert them.

BTW, there is a better (in my opinion), but lesser known algorithm, which eliminates the need for having to remember the order in which you add and subtract, viz:
converted_temp = ((original_temp + 40) * factor) - 40
where factor = (5.0/9.0), when converting from F to C, and (9.0/5.0) when converting from C to F.

Winston

Lee Sigauke
Ranch Hand
Posts: 30
Campbell Ritchie wrote:I didn’t read the whole code, but I can see a serious design error in it. You ought not to be calculating anything inside a frame class. You ought to have a temperature converter class which does the calculations. This is how you ought to have developed your app
• 1: Create a Temperature class, or a TemperatureConverter class.
• 2: Demonstrate the workings of this class with a TemperatureDemo class.
• 3: Put a GUI round that.
• I’t let you work out how the TemperatureScales enum and the Temperature class work. If you use a TemperatureConverter class, that will work completely differently.

I get what you're saying but then where do I make the calculation ? I'd like my program to calculate the temperature rather than having predefined methods doing it for me.

Lee Sigauke
Ranch Hand
Posts: 30
Winston Gutkowski wrote:
Lee Sigauke wrote:Thanks that did the trick. Why did I have to write the numbers as doubles though ? the one I found online isn't written like that. I also thought that since the variables are already declared as double I wouldn't need to do that

Because integer division is different from FP division - ie, it truncates. It's also generally worth making all components of an expression the same type, because it saves the code from having to convert them.

BTW, there is a better (in my opinion), but lesser known algorithm, which eliminates the need for having to remember the order in which you add and subtract, viz:
converted_temp = ((original_temp + 40) * factor) - 40
where factor = (5.0/9.0), when converting from F to C, and (9.0/5.0) when converting from C to F.

Winston

I've had mishaps with things like this before, your explanation makes sense. Thank you

Campbell Ritchie
Sheriff
Posts: 50258
79
• 1
Lee Sigauke wrote: . . . I'd like my program to calculate the temperature rather than having predefined methods doing it for me.
The predefined methods are part of your program. The whole idea of OO programming is to have one class represent one thing. Your frame class should hold the display together, and your text fields or whatever show the figures. The Temperature class (or similar) records the temperature. Divide your app into different parts. The frame class ought not to do the calculations.
There are several ways you can do it.
• 1: Utility class with static toFahrenheit and toCelsius methods
• 2: Temperature class with celsius and fahrenheit fields, set up in the constructor. You can make such a class immutable. That is similar to what I showed earlier.
• 3: Temperature class (again can be made immutable) recording temperature in one scale, maybe absolute (°K), calculating C and F equivalents as required.
• 4: There is bound to be a 4th version, but I can’t think of it just at the moment.
• Note this might be easier if you use a TemperatureScale enum as well. You can even enhance the TemperatureScale objects with the conversion factors, 1.8 and 0.55555555555555555555555555555555555.... You can enhance the Temperature class with absolute zero (-273.15°C) and throw an IllegalArgumentException for temperatures below that.

I like the +40-40 algorithm, which I had never seen before. That fits very nicely with the enum recording 1.8 and 0.555555555555555555555555555555555.... Not quite so good for °K or the old French Réaumur scale.

Lee Sigauke
Ranch Hand
Posts: 30
Campbell Ritchie wrote:
Lee Sigauke wrote: . . . I'd like my program to calculate the temperature rather than having predefined methods doing it for me.
The predefined methods are part of your program. The whole idea of OO programming is to have one class represent one thing. Your frame class should hold the display together, and your text fields or whatever show the figures. The Temperature class (or similar) records the temperature. Divide your app into different parts. The frame class ought not to do the calculations.
There are several ways you can do it.
• 1: Utility class with static toFahrenheit and toCelsius methods
• 2: Temperature class with celsius and fahrenheit fields, set up in the constructor. You can make such a class immutable. That is similar to what I showed earlier.
• 3: Temperature class (again can be made immutable) recording temperature in one scale, maybe absolute (°K), calculating C and F equivalents as required.
• 4: There is bound to be a 4th version, but I can’t think of it just at the moment.
• Note this might be easier if you use a TemperatureScale enum as well. You can even enhance the TemperatureScale objects with the conversion factors, 1.8 and 0.55555555555555555555555555555555555.... You can enhance the Temperature class with absolute zero (-273.15°C) and throw an IllegalArgumentException for temperatures below that.

I like the +40-40 algorithm, which I had never seen before. That fits very nicely with the enum recording 1.8 and 0.555555555555555555555555555555555.... Not quite so good for °K or the old French Réaumur scale.

Okay, so basically it's good practice to divide an application into different parts doing different things at a time. Thank you for your help, I've learnt more than I though

Campbell Ritchie
Sheriff
Posts: 50258
79
Lee Sigauke wrote: . . . divide an application into different parts doing different things at a time. Thank you for your help, I've learnt more than I though
You’re welcome It is always pleasing when we see people learning something new.