Results 1 to 5 of 5
  1. #1
    Stryker4526 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Exclamation New to operators, need some help...

    Alright... I'm trying to convert temperatures into different scales using hard-coded variables, but the calculations aren't being performed... My professor would only tell me it's a problem with my operators, but I can't find what the problem is... any help?
    Java Code:
    import java.util.Set;
    
    /**
     *
     */
    
    /**
     * @author Robert Bazan
     * 
     */
    public class Temperature {
    	static char scale = 'F';
    	static double fahrenheit;
    	static double celsius;
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		fahrenheit = 212.0;
    		celsius = (5 / 9) * (fahrenheit - 32);
    		System.out.println("The scale is: " + scale);
    		System.out.println("The Celsius temp is: " + celsius);
    		System.out.println("The Fahrenheit temp is: " + fahrenheit);
    		scale = 'C';
    		celsius = 22.22222;
    		fahrenheit = (9 / 5) * (celsius + 32);
    		System.out.println("The scale is: " + scale);
    		System.out.println("The Celsius temp is: " + celsius);
    		System.out.println("The Fahrenheit temp is: " + fahrenheit);
    
    	}
    
    }
    And my output is:
    Java Code:
    The scale is: F
    The Celsius temp is: 0.0
    The Fahrenheit temp is: 212.0
    The scale is: C
    The Celsius temp is: 22.22222
    The Fahrenheit temp is: 54.22222

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

    Default

    Rather than 5/9 or 9/5, try using 5.0/9.0 and 9.0/5.0; the difference here is 5/9 is int division which must return an int rounded toward 0 (I think). Thus 5/9 returns 0, not 0.55555. 5.0/9.0 on the other hand will return a double and will give you a useful result here.

    Note that I have not checked your code for other errors.

  3. #3
    Stryker4526 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Rather than 5/9 or 9/5, try using 5.0/9.0 and 9.0/5.0; the difference here is 5/9 is int division which must return an int rounded toward 0 (I think). Thus 5/9 returns 0, not 0.55555. 5.0/9.0 on the other hand will return a double and will give you a useful result here.

    Note that I have not checked your code for other errors.
    What? You mean in Java it will only store integer values if I don't type out a decimal place or two? How absurd.

    EDIT:
    Yep, that did it... what a ridiculous language already... I can tell I'm going to prefer C++ to Java...
    Thanks for the help.

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

    Default

    Quote Originally Posted by Stryker4526 View Post
    What? You mean in Java it will only store integer values if I don't type out a decimal place or two? How absurd.
    Nothing absurd about this as it is 1) also the case for many programming languages, 2) it is useful to have this ability to do either int or double division as required, and 3) it is well documented.
    Last edited by Fubarable; 09-26-2009 at 03:17 AM.

  5. #5
    Stryker4526 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Nothing absurd about this as it is 1) also the case for many programming languages, 2) it is useful to have this ability to do either int or double division as required, and 3) it is well documented. You may wish to be careful what you call absurd until you have a little more programming under your belt.
    Never run into a program yet that assumes I would want an answer of 0 when I ask it to perform (5/9), so yeah, it does seem a little absurd.

    At any rate, I know what to do in Java for that now, so thanks for your help.

Similar Threads

  1. Loop operators!
    By aRTx in forum New To Java
    Replies: 8
    Last Post: 05-12-2009, 05:47 PM
  2. Java Operators
    By Ash-infinity in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 12:51 PM
  3. Using the bitwise operators
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:39 PM

Tags for this Thread

Posting Permissions

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