Results 1 to 10 of 10
  1. #1
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default Please help super noob understand

    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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    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. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    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?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  9. #9
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    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!

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, please mark your thread solved with the thread tools at the top.

Similar Threads

  1. Noob here
    By Iron Lion in forum Introductions
    Replies: 1
    Last Post: 11-15-2010, 05:50 PM
  2. calling variable using super super..
    By Stephen Douglas in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 06:12 AM
  3. Noob Here
    By Salamander in forum Introductions
    Replies: 1
    Last Post: 08-03-2010, 08:19 AM
  4. Help im a noob.. a super noob on java..
    By critdevil in forum New To Java
    Replies: 12
    Last Post: 03-07-2009, 03:17 AM
  5. Please help a noob :)
    By Bays in forum New To Java
    Replies: 15
    Last Post: 06-17-2008, 06:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •