wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes A variable is 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A variable is 0" Watch "A variable is 0" New topic
Author

A variable is 0

Alex MG
Greenhorn

Joined: Jan 23, 2005
Posts: 1
Hi,

I'm new in Java and to learn I'm typing and testing code. I have the following code:



What I want to do is to display the sum of the numbers after dividing them. The problem is the result of r and res variables that always is 0 and I don't know why.

What's wrong?

Thanks.
[ January 23, 2005: Message edited by: Ilja Preuss ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Please use code tags in the future - I will add them for you in this case...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

The problem is that you have declared your variables as type int. Therefore when doing the division (this.n/this.d and this.nm/this.nd) the results (0.5 and 0.66666666667) are being cast to type int and being set to of 0 and 0 (since the fraction is truncated off).

Change your variables and parameters n, d, nm, and nd to be of type float and you will get the result of 1.1666667.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Better yet, don't do the division. I would guess that "fraccion" is your language's equivalent to the English "fraction". A very OO approach to this code, would be to have summ() return a fraccion, rather than a float. You can do this with a single line of code, if you understand the algebra for adding fractions together.

With that said, you should also look into "integer division" as it is called. You should be able to google for this topic to find out why you are getting a 0 as the answer.

HTH

Layne

[ January 23, 2005: Message edited by: Layne Lund ]
[ January 23, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

As another alternative, if you wanted to ensure that your method only took 4 int's as parameters (since it appears you are trying to mimic a pair of fractions), you could cast them to float's as you do the calculation:



This would "prevent" two products of the division from being cast to int's.

[edit]
Layne made his post while I was typing this second post. I agree that a true object oriented way of doing it would be to have your method return a "fraction" (or faccion) object. If you are not quite ready for that yet in your Java knowledge, give the casting to a float as I suggest here a try.
[ January 23, 2005: Message edited by: Mark Vedder ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Hi, Alex MG

Welcome to JavaRanch! Please adjust your display name to meet the JavaRanch Naming Policy. Unfortunately your last name does not comply. You can change it here.

Thanks!


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
 
subject: A variable is 0
 
Similar Threads
deprecated API
Sorting array's
while loops and all that fun stuff, need assignment help
Percision problems with Double and Float numbers
Add an exception for divide by zero to my fraction class