Question
Write a program that allows the user to convert a temperature given in degrees Celsius or Fahrenheit to the opposite value. Begin by getting a
Write a program that allows the user to convert a temperature given in degrees Celsius or Fahrenheit to the opposite value.
Begin by getting a temperature from the user. After the temperature, ask the user to enter the character 'C' or 'c' for Celsius or the character 'F' or 'f' for Fahrenheit. If the user enters anything other than 'C', 'c', 'F', or 'f', then display an error message telling the user to only enter 'C' or 'F'.
Once you have a temperature and the unit, then convert the temperature to the opposite unit and display the result. The answer must be displayed showing only one decimal place.
The formula to convert a Celsius temperature to Fahrenheit is: DegreesFahrenheit = ( 9 ( DegreesCelsius ) / 5 ) + 32 )
The formula to convert a Fahrenheit temperature to Celsius is: DegreesCelsius = 5 ( DegreesFahrenheit 32 ) / 9
The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics:
Enter a temperature: 98.6 Enter (C)elsius or (F)ahrenheit: F The equivalent temperature in Celsius is: 37.0
Here is another example run of the program:
Enter a temperature: 23.9 Enter (C)elsius or (F)ahrenheit: f The equivalent temperature in Celsius is: -4.5
Here is another example run of the program:
Enter a temperature: 100 Enter (C)elsius or (F)ahrenheit: C The equivalent temperature in Fahrenheit is: 212.0
Here is another example run of the program:
Enter a temperature: 49 Enter (C)elsius or (F)ahrenheit: M Error: Enter only "C" or "F".
Technical Notes, Special Requirements and Hints:
When asking the user to enter 'C' or 'F', your program should NOT be doing any String comparisons, but should be doing comparisons with the primitive data type char.
The kbd Scanner does not have a method for reading only a single char, and thus you will need to use next() or nextLine() to read an entire String. You will then need to use the appropriate method from the String class to extract the first character from the entered String so that you can do your comparisons to see if the user entered 'C' or 'F'.
Using the above described method, this also means that if the user enters the word "Celsius", the program should still work --- but also, if the user enters the word "Color", the program should still work also, since your program should ONLY be looking at the first letter of the word.
You can reduce the number of comparisons your program needs to perform by converting the entered word to all upper (or all lower) case letters. This will eliminiate the need to physically check to see if the user entered 'C' or 'c'.
Remember that you can use the printf command to make the answer display with only one decimal place.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started