Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts

    temperature conversion

    Exercise 3 Write an application that takes as input a temperature in Celsius and calculates both the approximated Fahrenheit equivalent and the actual Fahrenheit equivalent. Write out all three values. If the approximation and the actual value are within two degrees, write out “close enough” if they are not within two degrees, write out “ will not do”




    Hints Remember the Math.abs() method will return the absolute (unsigned) value of its argument. Refer to 'Numeric Methods' in Section 4.5 of the Main Text.

    this is what i am working from
    Code:
    public class Converter
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter temp: ");
            int temp = sc.nextInt();
            System.out.println(convertToC(temp));
        }
             public static int convertToC(int temp)
        {
            return ((temp - 32 ) * 5/9);
        }
    }
    but i am on wrong track

    how should i start this properly?

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Well first, I'm not really sure what you mean when you say the "approximated Fahrenheit equivalent" so I can't really help you on that part.

    This seems to be a fairly straight forward Introductory Java assignment. I don't want to do the assignment for you, for reasons posted in all sorts of stickies (among them: http://www.codingforums.com/showthread.php?t=53446), but you need to do a few things:

    1) Read in the Celsius value from the user. You're already doing this right, except you shouldn't read it in as an Integer in the event they say 37.2 degrees. For this, try nextFloat and assign it to a Float data type. Also, you're going to need to enclose this in a try{}catch to catch the InputMismatchException

    Code:
    try {
         Scanner input = new Scanner( System.in );
         float celsiusValue = input.nextFloat();
    }catch( Exception e ){
         System.out.println( "You must enter a floating point number!" );
         System.exit( 1 );
    }
    2) Next, according to your assignment, output this. This is very straight forward and you've already got it for the most part: System.out.println( "Specified Celsius Value: " + celsiusValue );

    3) Now, for clarity sake, create a method for two things: A) Calculating the approximate Fahrenheit value (again, not sure what this means) and B) Calculating the finite Fahrenheit value

    The function prototypes will look something like this:
    public static float calcApproximateFahrenheit( float celsiusValue );
    public static float calcExactFahrenheit( float celsiusValue );

    4) Print both values

    5) Take the result of those methods, compare them:

    Code:
    if( Math.abs( ( exactAmount - approxAmount ) ) <= 2 )
         System.out.println( "Close enough" );
    else
         System.out.println( "Will not do" );
    The method Math.abs() allows us to see both in the positive and negative direction at the same time. Let's say exactAmount - approxAmount = -2. The absolute value will turn -2 into 2, which we can then evaluate.


    Now, try to create a program to accomplish this one step at a time. As you encounter problems feel free to post them and I'll be more than happy to help you solve them.
    Last edited by ShaneC; 01-17-2011 at 06:30 AM. Reason: Added try{}catch{} remark
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    worldtraveller (01-17-2011)


  •  

    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
    •