GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Jul-2009, 14:33
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Simple Question Regarding Precedence


Another newbie question from me.

I'm doing a really basic exercise to practice precedence rules in Java. The rules seem pretty straight forward to me, but something that I feel like should work simply doesn't.

The exercise is to write a console program that converts Farenheit temperature to Celsius, based on the following equation:

Quote:
C=5/9 x (F-32)

The class I wrote works perfectly and I understand how it conforms to precedence rules in Java:
JAVA Code:
public class FarenheitToCelsius extends ConsoleProgram {
	public void run() {
		println("This program converts Farenheit to Celsius");
		double farenheit = readDouble("Enter Ferenheit temperature: ");
		double celsius = 5 * (farenheit - 32) / 9;
		println("Celsius equivalent = " + celsius);
	}
}

Here's what I DON'T understand. Why does the following expression return a "0.0" regardless of what initial value I enter for Farenheit? I feel like it still conforms to Java's precedence rules, but obviously I'm still not getting something (and perhaps it has nothing to do with precendence).

JAVA Code:
		double celsius = (5 / 9) * (farenheit - 32);

I thought that the computations within the parenthesis were done first.

Sorry for the super basic questions! I'm doing this solo so I appreciate folks' taking the time out.
  #2  
Old 13-Jul-2009, 16:52
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Simple Question Regarding Precedence


What you are not understanding is that when the compiler sees that statement, it assigns 5 and 9 to an integer. So, when you compute the integer division of 5/9, you get an integer back which is zero. Make it 5.0 and see what happens.
  #3  
Old 13-Jul-2009, 20:24
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Re: Simple Question Regarding Precedence


Ah that makes sense. So always express a number as x.0 if I want the compiler to treat it as a double.

Are there other ways to tell it to treat all values in an equation as doubles?

Thanks for setting me straight on that one!
  #4  
Old 13-Jul-2009, 21:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Simple Question Regarding Precedence


Quote:
Originally Posted by ebot9000
Ah that makes sense. So always express a number as x.0 if I want the compiler to treat it as a double.

Are there other ways to tell it to treat all values in an equation as doubles?

Thanks for setting me straight on that one!
You can use a cast on either operand (or both). I think that just getting into the habit of tacking on the ".0" onto any integer constant that you want to be treated as a double is more elegant (but that's just me).

JAVA Code:
double celsius = ((double)5 / (double)9) * (farenheit - 32);

Consistent with my personal style (and noting that you misspelled "Fahrenheit"), I probably would have written it
JAVA Code:
double celsius = (5.0 / 9.0) * (fahrenheit - 32.0);

Cleaner appearing and not as many of those annoying parentheses to keep track of.


Bottom line:
If the division has only integers, the quotient will be the result of integer division.

If one operand of a binary arithmetic operation is a double and the other is an integer, the integer is "promoted" and the operation will be done with floating point arithmetic.

Regards,

Dave
  #5  
Old 14-Jul-2009, 01:31
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Re: Simple Question Regarding Precedence


That clears up a lot (down to the spelling).

So basically, because "5 / 9" is enclosed in parentheses, that computation is done first. And since both operands are integers, I end up with the integer 0.

And when I rewrite the expression as "5 * (F-32) / 9," the 5 is converted into 5.0 because (F-32) is treated as a double, and the 9 becomes 9.0 for the same reason. So that works, but of course doesn't look as nice.

"(5.0 / 9.0) * (fahrenheit - 32)" definitely wins on all fronts.

You've cleared up a lot for me. Thanks for taking the time out on the remedial end of things! I'm sure I'll be back in another chapter or two.
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple array question steve_d C++ Forum 2 02-May-2007 03:40
Problems about simple tricked loop question allenfanwenyuan C++ Forum 6 01-May-2007 09:19
simple question betazee C Programming Language 5 07-Jun-2006 13:08
Simple question about while loop Lej C++ Forum 6 07-Aug-2005 13:57
Simple question on arrays--please help! brookeville C++ Forum 16 17-Nov-2004 23:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:40.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.