This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

I am having issues with a method that is not giving me the result I thought it would and I think it has something to do with the double data type. The method is -

My volume is 1728, WEIGHT_PER_CM3 is 2. I get 3.456 but the method returns 3. I'm sure I understand the double data type and think it might have something to do with it.

thanks

Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26

posted

0

Correction - I'm not sure I understand the double data type

Shelby Simpson wrote:I am having issues with a method that is not giving me the result I thought it would and I think it has something to do with the double data type. The method is -

My volume is 1728, WEIGHT_PER_CM3 is 2. I get 3.456 but the method returns 3. I'm sure I understand the double data type and think it might have something to do with it.

thanks

You get 3.456 because you have been taught in 2nd or 3rd grade how to go about multiplying decimal numbers. But you have to tell the computer to do that for you. It does not understand what is going on in your mind.

Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26

posted

0

Mansukhdeep Thind wrote:

Shelby Simpson wrote:Correction - I'm not sure I understand the double data type

Read this and try to understand all data types thoroughly.

What is the return type of getVolume() method? Is it int?

That was it, the return type was int. Does int concatenation work the same as string. So to say that everything int + becomes a int? So now that volume is a double everything following will also be a double?

Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26

posted

0

Mansukhdeep Thind wrote:

You get 3.456 because you have been taught in 2nd or 3rd grade how to go about multiplying decimal numbers. But you have to tell the computer to do that for you. It does not understand what is going on in your mind.

I confused, the decimal was a result of dividing whole numbers.

You get 3.456 because you have been taught in 2nd or 3rd grade how to go about multiplying decimal numbers. But you have to tell the computer to do that for you. It does not understand what is going on in your mind.

I confused, the decimal was a result of dividing whole numbers.

What are you confused about? Can you show me the whole code?

Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26

posted

0

Mansukhdeep Thind wrote:

Shelby Simpson wrote:

Mansukhdeep Thind wrote:

You get 3.456 because you have been taught in 2nd or 3rd grade how to go about multiplying decimal numbers. But you have to tell the computer to do that for you. It does not understand what is going on in your mind.

I confused, the decimal was a result of dividing whole numbers.

What are you confused about? Can you show me the whole code?

I thought the issue was with concatenation. I thought it was converting my 3.456 to an int. I just don't understand how multiplying decimals plays into this.

That was it, the return type was int. Does int concatenation work the same as string.

Nothing to do with concatenation. You get 3 because the int type only deals with whole numbers so something like 3.4 gets rounded down to 3. All integer arithmetic rounds down so 3.8 would also yield the result 3.

Shelby Simpson wrote:I thought the issue was with concatenation. I thought it was converting my 3.456 to an int. I just don't understand how multiplying decimals plays into this.

An int multiply with an int results in an int. An int divided by an int results in an int. And in your example, an int multiply with an int and then divided by an int results in an int. Your method doesn't know that you need a double until it is time to return the value

Java didn't convert "3.456" into an int. It converted your int result into a double -- and when it does that, it is highly unlikely that doubles which came from ints turn into something that isn't a whole number.