Hello and welcome to our community! Is this your first visit?
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
    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
    public class Converter
        public static void main(String[] args)
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter temp: ");
            int temp = sc.nextInt();
             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
    Redmond, WA
    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: Homework Assignments), 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

    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:

    if( Math.abs( ( exactAmount - approxAmount ) ) <= 2 )
         System.out.println( "Close enough" );
         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 05: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