Question
In this assignment, we will be writing static methods that convert temperature from Celsius to Fahrenheit and from Fahrenheit to Celsius. We will also be
In this assignment, we will be writing static methods that convert temperature from Celsius to Fahrenheit and from Fahrenheit to Celsius. We will also be using a switch statement to determine which conversion to use and validate input. We will also be using a while or do-while to run conversion of more than 1 temperature until the sentinel value is reached. We will be creating 2 java class files.
One will be the Temperature class file which will have 2 public static methods, celsiusToFahrenheit() there will be one input for Celsius temperature. The output will be Fahrenheit temperature. What is the logical primitive datatype for temperature?
The formula to convert is the following:
fahrenheit = (celsius * 9 / 5) + 32;
fahrenheitToCelsius() - there will be one input for Fahrenheit temperature. The output will be Celsius temperature. What is the logical primitive datatype for temperature?
The formula to convert is the following:
celsius = (fahrenheit - 32) * 5 / 9;
The 2nd java class file will be the driver program called TempDemo. This is the program that will have your main() method. The program will:
prompt the user if they want to convert from F to C or from C to F. User can input a 1, 2, or Q (can accept q also). This prompt will be displayed repeatedly until the user types in a Q to quit the program. This is how to display the prompt to user:
Please select the converter direction.
1. From Celsius to Fahrenheit.
2. From Fahrenheit to Celsius.
Q. To Quit.
Enter Q, 1 or 2 ===>
Read the input as a String. Notice inputs are numbers (1 or 2) and characters (Q)!
Get the first character of the String! Remember that method from way back in the String chapter 2.10. See page 80. Need to get the first char of the String to be used on the switch statement in next step (p243 example).
Use a switch statement (Chp 4.10) on the first character of the input String to:
case 1 - then prompt user for temperature in C. Read input.
case 2 - then prompt user for temperature in F. Read input.
case Q Quit program.
otherwise - invalid input, then display an error message:
Invalid converter selected. Enter 1,2 or Q
if input is 1 or 2, execute the correct static method you created in Temperature to convert the temp.
When valid input, display the converted temperature:
100.00 degrees Celsius is equal to 212.00 degrees Fahrenheit.
OR
32.00 degrees Fahrenheit is equal to 0.00 degrees Celsius.
if input is Q or q, set sentinel value to quit. Display Goodbye.
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