Results 1 to 7 of 7

Thread: Simple Help

  1. #1
    Andy_M is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Question Simple Help

    Hello All!

    I am new to Java and hope to learn this over time and progress onto EE and ME for future developments.

    I am still learning Java syntax basics.

    Below is some code I have written, but found when using a single line variable it gives me the wrong answer. I decided to do this with multiple variables and get the correct answer, now I have managed to problem solve the single line variable with stepping the code and found the divide is where my problems start.

    Java Code:
    public class Temperature {
        public int fahrenheit = 9;
        public int celsius1 = fahrenheit - 32;
        public int celsius2 = celsius1 * 5;
        public int celsius3 = celsius2 / 9;
    
        public int celsius = fahrenheit - 32 * 5 / 9;
        
        // public double celsius = fahrenheit(double) - 32 * 5 / 9;
    
        public void calculateCelsius() {
    
            System.out.println(fahrenheit + "Fahrenheit to Celsius is " + celsius3 + "c is correct");
            System.out.println(fahrenheit + " Fahrenheit to Celsius is " + celsius + "c");
    
        }
    }
    As you can see I also tried to Double this, with no success. When using a method to view the calculation it shows fine in console, just wrong numbers for one output.

    I don't want to move on in learning until I understand the problem and what I did wrong.

    Any help welcomed :)
    Last edited by Andy_M; 01-09-2010 at 06:00 PM. Reason: Update

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    You have been bitten by the 'integer division' operator. That operator divides two ints and forgets all about the remainder. Literal numbers that don't contain a decimal point (a dot) are interpreted as ints by the compiler, so 5/9 is an integer division of the two int numbers 5 and 9; the result is 0 (that forgotten remainder, remember?).

    Better use double numbers, e.g. 5.0/9.0 and use variables of type double. Try it and see for yourself.

    kind regards,

    Jos

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Java Code:
        public double celsius = (fahrenheit - 32) * 5.0 / 9.0;
    I just noticed; change your code to the above version. (multiplication has a higher precedence than subtraction).

    kind regards,

    Jos

  4. #4
    Andy_M is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Jos,

    This has worked a treat. So the game is to make sure I try and use double numbers IE 1.0 for doubles and make sure I close in calculation with ( ) before a higher operator?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Andy_M View Post
    Jos,

    This has worked a treat. So the game is to make sure I try and use double numbers IE 1.0 for doubles and make sure I close in calculation with ( ) before a higher operator?
    Yup, it's just 101 math, e.g 2*(3+4), you add 3 and 4 first and multiply the result by 2 (14), while 2*3+4, you multiply 2 and 3 and add 4 to the result (10). It's no rocket science ;-)

    kind regards,

    Jos

  6. #6
    Andy_M is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank you Jos for taking time out to help me

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Andy_M View Post
    Thank you Jos for taking time out to help me
    You're welcome of course; always take care that you have the basics right because without them you're on quicksand and all the rest doesn't have a solid foundation.

    kind regards,

    Jos

Similar Threads

  1. Simple AI?
    By Atriamax in forum New To Java
    Replies: 4
    Last Post: 12-23-2009, 10:35 PM
  2. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 02:55 AM
  3. Simple Help....
    By jpnym15 in forum New To Java
    Replies: 1
    Last Post: 11-11-2008, 02:33 PM
  4. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  5. simple GUI
    By dim_ath in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 03:00 PM

Posting Permissions

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