Build a set of variables for a temperature converter. You'll store a Celsius value, convert it to Fahrenheit, and display the result.
Here's what you need:
- A
doublefor the Celsius temperature - A
doublefor the converted Fahrenheit value - A
finalconstant for the conversion factor - A
Stringfor the output message
Try it yourself, then compare:
final double CONVERSION_FACTOR = 9.0 / 5.0;
final double OFFSET = 32.0;
double celsius = 100.0;
double fahrenheit = celsius * CONVERSION_FACTOR + OFFSET;
String result = celsius + "C = " + fahrenheit + "F";
System.out.println(result);
Notice the constants. Writing 9.0 / 5.0 instead of 1.8 makes the formula's origin clear. Anyone reading your code can see where the number comes from.