# New to operators, need some help...

Printable View

• 09-26-2009, 12:58 AM
Stryker4526
New to operators, need some help...
Alright... I'm trying to convert temperatures into different scales using hard-coded variables, but the calculations aren't being performed... My professor would only tell me it's a problem with my operators, but I can't find what the problem is... any help?
Code:

```import java.util.Set; /**  *  */ /**  * @author Robert Bazan  *  */ public class Temperature {         static char scale = 'F';         static double fahrenheit;         static double celsius;         /**         * @param args         */         public static void main(String[] args) {                 // TODO Auto-generated method stub                 fahrenheit = 212.0;                 celsius = (5 / 9) * (fahrenheit - 32);                 System.out.println("The scale is: " + scale);                 System.out.println("The Celsius temp is: " + celsius);                 System.out.println("The Fahrenheit temp is: " + fahrenheit);                 scale = 'C';                 celsius = 22.22222;                 fahrenheit = (9 / 5) * (celsius + 32);                 System.out.println("The scale is: " + scale);                 System.out.println("The Celsius temp is: " + celsius);                 System.out.println("The Fahrenheit temp is: " + fahrenheit);         } }```
And my output is:
Code:

```The scale is: F The Celsius temp is: 0.0 The Fahrenheit temp is: 212.0 The scale is: C The Celsius temp is: 22.22222 The Fahrenheit temp is: 54.22222```
• 09-26-2009, 01:17 AM
Fubarable
Rather than 5/9 or 9/5, try using 5.0/9.0 and 9.0/5.0; the difference here is 5/9 is int division which must return an int rounded toward 0 (I think). Thus 5/9 returns 0, not 0.55555. 5.0/9.0 on the other hand will return a double and will give you a useful result here.

Note that I have not checked your code for other errors.
• 09-26-2009, 01:18 AM
Stryker4526
Quote:

Originally Posted by Fubarable
Rather than 5/9 or 9/5, try using 5.0/9.0 and 9.0/5.0; the difference here is 5/9 is int division which must return an int rounded toward 0 (I think). Thus 5/9 returns 0, not 0.55555. 5.0/9.0 on the other hand will return a double and will give you a useful result here.

Note that I have not checked your code for other errors.

What? You mean in Java it will only store integer values if I don't type out a decimal place or two? How absurd.

EDIT:
Yep, that did it... what a ridiculous language already... I can tell I'm going to prefer C++ to Java...
Thanks for the help.
• 09-26-2009, 01:21 AM
Fubarable
Quote:

Originally Posted by Stryker4526
What? You mean in Java it will only store integer values if I don't type out a decimal place or two? How absurd.

Nothing absurd about this as it is 1) also the case for many programming languages, 2) it is useful to have this ability to do either int or double division as required, and 3) it is well documented.
• 09-26-2009, 01:29 AM
Stryker4526
Quote:

Originally Posted by Fubarable
Nothing absurd about this as it is 1) also the case for many programming languages, 2) it is useful to have this ability to do either int or double division as required, and 3) it is well documented. You may wish to be careful what you call absurd until you have a little more programming under your belt.

Never run into a program yet that assumes I would want an answer of 0 when I ask it to perform (5/9), so yeah, it does seem a little absurd.

At any rate, I know what to do in Java for that now, so thanks for your help.