Back to 111 Main Page

Mobius strip

Assignment 04

Task

Create a simple temperature converter program that converts an entered temperature from Celsius to Fahrenheit and also from Fahrenheit to Celsius.

Textbook: 2.6
New concepts: Input, Scanner.

Steps

Ask the user to enter a temperature. Read this in as a decimal number (double) using a Scanner. Then print what that temperature would be if converted from °C to °F, as well as from °F to °C. Print each conversion, clearly labeled, one per line.

So your final output should look something like this. Note how I executed the program four separate times; the command prompts and java command are not part of the output of the program. The stuff in green here is the input I typed when prompted by the program.

D:\TA\grading\A04>java ZtomaszeA04
Please enter a temperature to convert: 98.6
98.6 degrees C = 209.48 degrees F
98.6 degrees F = 37.0 degrees C

D:\TA\grading\A04>java ZtomaszeA04
Please enter a temperature to convert: -10
-10.0 degrees C = 14.0 degrees F
-10.0 degrees F = -23.333333333333336 degrees C

D:\TA\grading\A04>java ZtomaszeA04
Please enter a temperature to convert: 0
0.0 degrees C = 32.0 degrees F
0.0 degrees F = -17.77777777777778 degrees C

D:\TA\grading\A04>java ZtomaszeA04
Please enter a temperature to convert: 32
32.0 degrees C = 89.6 degrees F
32.0 degrees F = 0.0 degrees C

Your program will currently crash if you enter Strings instead of a number. At this point, that is okay; we'll soon learn how to prevent this.

What to Submit

Upload your UsernameA04.java file to Tamarin.

Grading [3 points]

1 - Compiles
Your program compiles successfully (no errors)
0.6 - Input
You prompt the user to enter a double, which you use to do the calculations.
1.4 - Output
Program displays the correct results of the two conversions. Conversions are printed one per line, with one number followed by a C and the other by an F. (You can spell the words out or drop the word degrees if you want to.)

FAQs

Where's the demo code from class?
Here: UserAge.java
Is it okay if my answer is very very close to yours but not exactly the same?
Yes, that's fine. For example, you're probably getting something like this:
Please enter a temperature to convert: 98.6
98.6 degrees C = 209.48 degrees F
98.6 degrees F = 36.99999999999999 degrees C
The reason for this is how doubles are stored internally. They are only precise to a certain number of decimal places and, depending on which math operations you perform on them and in which order, that last decimal place may get rounded a digit or two either way along the way. This is always a danger with double math that will become more obvious when we get to to the == (equality) operator next week.

There is a way to format floating point numbers when you print them, so you don't always have to have a big ugly string of digits. However, the formatting either involves the use of an object (such as an instance of java.text.DecimalFormat), or else a rather arcane formatting code (for example: System.out.printf("%.1f\n", tempC)). Both are more complicated than we really want to deal with at this point of the class (but have a look into them if you're interested).



~ztomasze Index : TA Details: ICS111: A04
http://www2.hawaii.edu/~ztomasze
Last Edited: 28 Jan 2009
©2009 by Z. Tomaszewski.