Converting Temperatures

From CompSciWiki
Revision as of 15:16, 8 December 2011 by RyanV (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Create a program that will allow the user to enter a temperature in Celsius (°C) and will output a temperature in Fahrenheit (°F). The temperature in Celsius should be entered using JOptionPane.showInputDialog, and the temperature in Fahrenheit should be outputted using System.out.println. The input and output should both be given as integers. Assume valid input.


The formula for calculating the hypotenuse is <math>f = c*9/5 + 32</math>, where <math>f</math> is the temperature in Fahrenheit, and <math>c</math> is the temperature in Celsius.

To solve this problem, you will need to understand:

 Sample Input: -40
Sample Output: -40

Sample Input: 21
Sample Output: 70 
 

...By Students

"The reason that this program converts from Celsius to Fahrenheit instead of the other way around was solely so that you would run into a problem involving integer division. This isn't done to be mean or tricky, but because this is a mistake you will probably make regularly for the rest of your computer science career."

"Even in fourth year, I've made this mistake. My professor had just posted the term marks for a course, and I wanted to know how well I needed to do on the final project in order to do well in the class. I didn't have a calculator on hand and I had to add together 6 or 7 fractional values. I am a computer scientist; clearly the solution was to throw the numbers into a python script and have it calculate the mark for me (fun fact: Python will solve every problem you ever have in life). I wasn't really thinking about my programming basics at the time, so I didn't enter decimal places, except where I had to. Needless to say, I was very confused about my "D" in the course until my friend pointed out that I was doing integer division."

"The lesson here is twofold: there is no amount of programming experience that will prevent you from occasionally making basic mistakes; it is always important to thoroughly test your code, because not every mistake you make will result in a compiler error."

Solution

There are four distinct steps in this program:

  • reading in input from the user
  • calculating the result
  • rounding the result
  • printing out the result.

The first step is to read in the input. You are going to need to use an input dialog box.

 String input = JOptionPane.showInputDialog(null, "Enter a temperature in Celsius"); 

Don't forget your import statement.

 import javax.swing.*; //needed for JOptionPane 

The input you receive is going to be a string. You are going to need to convert that input into a int before using it.

 int c = Integer.parseInt(input); 

The next step is calculating the temperature in fahrenheit. Recall, the formula is <math>f = c*9/5 + 32</math>. Your first instinct will probably be to write code that looks something like this:

int f = (c*9)/5 + 32;

There are two subtle mistakes with this line of code, that will result in incorrect output values.

The formula <math>f = c*9/5 + 32</math> can produce decimal values, even with integer inputs. This means the value will need to be rounded before being displayed. You will need a temporary value to store the double-type temperature.

If you recall what you learned about ints, you will remember that when double values are passed to int variables, the decimal points are truncated. This means that you are going to need to explicitly round the value yourself. The Math class has you covered, as it has a built-in method for rounding.

 import java.lang.Math;//needed for round 

You can now write the Fahrenheit temperature to a temporary value to a double, and then use Math.round to round it, and then cast that result to an int:

 double temp = (c*9)/5 + 32; //calculate fahrenheit.
int f = (int)Math.round(temp); 

With the temperature in Fahrenheit calculated, all that is left is to output the result. This should be done using System.out

 System.out.println(c + "°C is: " + f + "°F"); 

Your program should now be done, right? Try running the two sample inputs and see what you get. An input of "-40" will result in "-40," but an input of "21" incorrectly results in "69." This is where the second subtle bug in that original conversion code comes in.

 int f = (c*9)/5 + 32; 

The issue has to deal with division. 21*9 is 189. 5 fits into 189 37.8 times. Because we have specified all three values (c, 9 and 5) as integers, Java assumes that we would like to use integer division. This means that we lose the 0.8, and only get the 37. The solution to this problem, is to specify one of the numbers as a double instead of an int:

 double temp = (c*9)/5.0 + 32; //calculate fahrenheit.
int f = (int)Math.round(temp); 

You should now have a program that correctly gives the Fahrenheit equivalent to a Celsius temperature. For the entire code solution, see below.

Code

Solution Code

Back to the Program-A-Day homepage