Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24
  1. #21
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    dude, read my last post, it works and you dont need to convert to doubles and then back to ints. your assignment is in integers

  2. #22
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The whole point is that you don't want to cast 9.0 etc as that would leave where you were to start with! Rather you want to cast the double result of the whole expression.

    Since I'm posting again, that method would normally be written:

    Java Code:
        /**
         * Returns the temperature in a whole number of degrees F.  The result is rounded
         * towards zero.
         */
    public static int fahrenheit(int celsius)
    {
        return (int)(9.0 / 5.0 * celsius + 32);
    }

  3. #23
    Moka is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    By george pb! You are my hero!

    Thank you so so so much, it all seems to be working now. That is a very helpful skill to use in the future.

    Again, thank you! <3

  4. #24
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You're welcome.

    There is nothing wrong in this case with ozzyman's approach of jiggling around with the order so that the multiplications are done before the (integer) division.

    You might want to have a go at returning the rounded integer result. The Math method I linked to above yields a long result, so again you have to cast it to an int before you return it.

    All this only arises because the question wants you convert F<->C using integer values which is a bit dumb as double values would seem more natural. But you can't do much about the question...

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Celsius to Fahrenheit
    By Danieldcc in forum New To Java
    Replies: 2
    Last Post: 09-30-2010, 05:50 AM
  2. Fahrenheit to Celcius program error
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 04:38 AM
  3. Help with fahrenheit to celsius conversion
    By java_is_killingme in forum New To Java
    Replies: 4
    Last Post: 03-07-2009, 12:11 PM
  4. Fahrenheit to celsius
    By lalithalydia in forum New To Java
    Replies: 3
    Last Post: 02-05-2008, 08:50 AM
  5. Converts from Fahrenheit to Celsius
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:52 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
  •