1. Member
Join Date
Mar 2011
Posts
35
Rep Power
0

Hi guys, I finally got some guts to start learning how to program and started reading a Java textbook. I am currently in chapter 2 and was doing "Personal Project" However my program doesn't seem to be calculating properly. Could someone explain to me in EASY ENGLISH so that this super noob can understand?

/* write an application that reads two floating point numbers and prints their sum, difference, and product */
public class pp0203 {
public static void main (String[] args)
{
float num1 = 13/8, num2 = 12/6;
System.out.println ("The sum of two mystery numbers is: " + (num1 + num2));
System.out.println ("The difference of two mystery numbers is:" + (num1 - num2));
System.out.println ("The product of two mystery numbers is: " + (num1 * num2));
System.out.println ("What are the TWO mystery numbers?");
}

}

The funny thing is that the sum should be 3.625, the difference should be -0.375, and the product should be 3.25. However, the answers my program gives me are: 3.0, -1.0, and 2.0. What seems to be the problem here?
If anyone would help me I will really appreciate it! :)

2. Your problem (in the "easiest" English I know) is that you're doing int division, that is dividing an int by an int, and this will always result in an int. So 3/2 is 1, 5/2 is 4 and 3/5 is 0. The solution is to do double division, 3.0 / 2.0, 5.0 / 2.0, and 3.0 / 5.0. Also, use doubles, not floats as you'll get much better accuracy for little or no cost.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
The / operator when applied to integers throws away the remainder. So that 6/2 is 3, but 7/2 is also 3. (8/2 is 4 etc).

Java Code:
`float num1 = 13/8;`

Java will deal with the right hand of this assignment first - which is why it decides that it is integers that are being divided. The correct way to do what you are attempting is

Java Code:
```double num1 = 13.0 / 8;
double num2 = 12.0 / 6;
System.out.println( [i]etc[/i]```

The .0 means that you are using a floating point literal not an integer literal so everything will be good. This little bit of code brings out a couple of other points: use double for floating point values, not float. (float is there in case you have some compelling reason to use it, but you don't) And, secondly, put declarations each on their own line as that makes the code easy to read.

4. Originally Posted by Fubarable
Your problem (in the "easiest" English I know) is that you're doing int division, that is dividing an int by an int, and this will always result in an int. So 3/2 is 1, 5/2 is 4 and 3/5 is 0. The solution is to do double division, 3.0 / 2.0, 5.0 / 2.0, and 3.0 / 5.0. Also, use doubles, not floats as you'll get much better accuracy for little or no cost.
:ahem:

------ (danged character limit)

5. Originally Posted by Fubarable
OK, OK, OK, busted! Your honor, I swear she said she was 18!
As 25/2 == 18?

kind regards,

Jos

6. Member
Join Date
Mar 2011
Posts
35
Rep Power
0
wow thanks guys! it helped a lot!
If people don't mind, one more question: is it possible to get answers in fraction? i now understand that if i use doubles then i will get correct answer, but if i were to use this program to test elementary students for their fraction quiz, how will i do it?

7. You can create a fraction class which handles all that stuff for you, it shouldn't be too challenging, there may be other ways but this seems like the way to go to me.

8. Member
Join Date
Mar 2011
Posts
35
Rep Power
0
oh, okay i'll look into that, thanks a lot! THANKS A LOT GUYS! i will try to help other people who's new to Java in the future!