Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Moka is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Fahrenheit to Celsius help please

    Hello,

    I was working on my java homework when I ran into a wall.

    The assignment is to implement 2 methods (one to convert from Celsius to Fahrenheit and another for vice versa) and then to implement those in a program. I decided the best way to do this is to use a menu.

    It was all working great, but now whenever I try to convert from Fahrenheit, it
    always returns 0 degrees Celsius, and it seems my conversion from C to F is off as well.

    I'm not sure if the problem is in the way I entered the calculations or in the way the methods are called or what.

    Any help would be greatly appreciated.

    PS - remember, this is a beginning Java course, so no advanced solutions please.

    Here is my code:

    Java Code:
    import java.util.Scanner;
    
    public class TempConvert 
    {
    	public static void main (String [] args)
    	{
    		Scanner keyBd = new Scanner( System.in );
    		char selection;
    		int convert;
    		int temp;
    
    		do{
    			System.out.println("\n--------------");
    			System.out.println("Main Menu\n");
    			System.out.println("1. Fahrenheit to Celsius");
    			System.out.println("2. Celsius to Fahrenheit");
    			System.out.println("3. Exit\n");
    			System.out.print  ("Selection: ");
    			
    			selection = keyBd.next().charAt(0);
    			
    			switch (selection)
    			{
    				case '1':
    				{
    					System.out.print("Enter temperature in Fahrenheit: ");
    					
    					temp = keyBd.nextInt();
    					
    					convert = celsius(temp);
    					
    					System.out.printf("%d degrees F = %d degrees C",temp,convert);
    				}
    					break;
    				case '2':
    				{
    					System.out.print("Enter temperature in Celsius: ");
    					
    					temp = keyBd.nextInt();
    					
    					convert = fahrenheit(temp);
    					
    					System.out.printf("%d degrees C = %d degrees F",temp,convert);
    				}	
    					break;
    				case '3':
    					break;
    				default :
    					System.out.printf("%c\n",7);
    					System.out.println("Invalid Selection");
    			}
    
    		}
    		while( selection != '3');
    
    	}
    	public static int fahrenheit(int celsius)
    	{
    		int fahrenheit;
    		fahrenheit = (9 / 5) * (celsius + 32);
    	
    	return fahrenheit;
    	}
    	
    	public static int celsius(int fahrenheit)
    	{
    		int celsius;
    		celsius = (5 / 9) * (fahrenheit - 32);
    		
    	return celsius;
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're doing int division, and realize that when you do this (an int divided by an int), the result is always an int. To fix this, you need to to do double division -- 5.0/9.0 and 9.0/5.0.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Your Celsius to Farenheit convert is coming out wrong because you are using the inaccurate integer division (e.g. 9/5 = 1). I'm trying to figure out why the other method is returning 0's
    Last edited by ozzyman; 03-20-2011 at 12:21 AM.

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

    Default

    Ok, when I change my ints to doubles, it gives me this error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Type mismatch: cannot convert from double to int

    at TempConvert.celsius(TempConvert.java:68)
    at TempConvert.main(TempConvert.java:30)

    I'm guessing this has something to do with the methods having to return ints not doubles. I was going to change that, but my assignment specifically says "Implement the following integer methods".

    Thank you guys for you quick replies!

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

    Default

    Also note that the pair of equations you use:

    fahrenheit = (9 / 5) * (celsius + 32)
    celsius = (5 / 9) * (fahrenheit - 32)

    are not the inverse of one another as one would expect.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Okay this is the reason: in Integer division 5/9 = 0!! which equates your calculation to 0.

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

    Default

    Yes pbrockway2, I did notice that. These are equations I got from the book this assignment is from. I just added some parenthesis when I started having problems (I thought maybe there was an order issue or something).

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

    Default

    cannot convert from double to int

    The compiler will not let you convert a double to an int without an explicit cast. (And returning a double value when you said you would return an int involves such a conversion.)

    A cast look like this:

    Java Code:
    int foo = (int)Math.sqrt(9);
    (int) takes the double returned by sqrt() and tells the compiler that you know what you are doing and not to worry about the inaccuracy.

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

    Default

    lol, thanks ozzyman. I'm still not sure how to fix this though. Should I change everything to doubles (or floats) instead of ints even though the assignment says "integer methods"?

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    if the assignment says integer methods just use integer methods but multiply by 100 before you make any calculation on it? - or there must be a way around it

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

    Default

    Indeed it is an order issue. Wikipedia is your friend.

  12. #12
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i just found this on the net:

    In order to convert temperature to Celsius from Fahrenheit, subtract the Fahrenheit number by 32, multiply by 100 and divide by 180. Its better than 9/5!

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

    Default

    This book doesn't know that. This is the exact way it tells me to enter the calculations:

    celsius = 5.0 / 9.0 * ( fahrenheit - 32 );

    fahrenheit = 9.0 / 5.0 * celsius + 32;

    and that is how I had them in my very original code (before some tweeking).

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

    Default

    No don't multiply by 100!

    Return what you calculate, but cast it to an int. Or, better perhaps, round it to an int. The assignment really should have said which to do if it said you must return an int. If it didn't I would go for casting because it's easier (and I'm lazy). I would add a note to the method recognising the inherent inaccuracy of the returned value.

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

    Default

    pb, please clarify.
    I have no experience with "casting", and I'm not sure what you mean by that. The example you showed earlier makes sense, but I'm unclear as to how to implement it into my code.
    Thank you guys again for helping!

  16. #16
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    actually wait, this is the problem, re-arrange the brackets:
    for 72 fahrenheigt, 72-32 = 40
    40* (5/9) in integer division = 40* 0
    but
    (40*5)/9 in integer division = 200 / 9 = 22
    a double will give you the answer 22.2222222 so its not much difference

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

    Default

    The book has it right, I think. (But I would still check with Wikipedia) Notice how for celsius the subtraction is done first and for fahrenheit the corresponding addition is done last. The reversal is how it should be.

    In your equations you do both the addition and the subtraction before the multiplication.

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

    Default

    Try what I posted. I don't know what you code is at the moment. But whereever you assign a double expression to an int, or return a double expression where you said you would return an int, put (int) in front of the expression. Often it looks like this:

    Java Code:
    int foo = (int)(/*some double expression*/);
    
    // or
    
    return (int)(/*some double expression*/);

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

    Default

    Something like this?:

    Java Code:
    public static int fahrenheit(int celsius)
    	{
    		int fahrenheit;
    		fahrenheit = (int)9.0 / (int)5.0 * celsius + 32;
    	
    	return fahrenheit;
    	}
    	
    	public static int celsius(int fahrenheit)
    	{
    		int celsius;
    		celsius = (int)5.0 / (int)9.0 * (fahrenheit - 32);
    		
    	return celsius;
    	}
    This removes the errors, but it still returns 0 C every time...

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

    Default

    No - just a single cast:

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

Page 1 of 2 12 LastLast

Similar Threads

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