You must create a Java program called TemperatureConverter
that converts
temperatures between the Celsius, Fahrenheit, and Kelvin scales.
The program should be modular, meaning you will break down the functionality into multiple methods. Importantly, some methods will call other methods to reuse code and build up more complex behaviors.
You must write the following methods within the class.
char getValidScale(String prompt)
.
This method will repeatedly prompt the user to enter one of "C", "F",
or "K" to designate one of the "Celsius", "Fahrenheit" or "Kelvin" scales.
The specific scale will be returned as either 'C', 'F' or 'K'.
The prompt is given as the input.
If the user enters an invalid scale, the method must print "Invalid scale entered."
and re-prompt.
double getValidTemperature(char scale)
This method will repeatedly prompt the user to enter a valid temperature
for the specified scale
where the scale
will be
either 'C', 'F' or 'K' to designate one of the "Celsius", "Fahrenheit" or "Kelvin" scales.
The lowest valid temperature for Celsius is -273.15.
The lowest valid temperature for Farenheit is -459.67.
The lowest valid temperature for Kelvin is 0.
There is no upper limit on valid temperatures for any scale.
double convertTemperature(double temp, char fromScale, char toScale)
This method will convert the temperature value temp
from the scale
denoted by fromScale
to the scale denoted by toScale
.
double toCelsius(double temp, char scale)
This method takes a temperature value temp
given in the scale scale
and converts it to Celsius.double fromCelsiusToFahrenheit(double celsius)
This method
takes the temperature value celsius
and converts it to the
Fahrenheit scale.double fromCelsiusToKelvin(double celsius)
This method
takes the temperature value celsius
and converts it to the
Kelvin scale.
You must include a main
method, but no others.
You must not duplicate code! An example
execution of the program is shown below. Follow the formatting of prompt and output
exactly as given.
Enter an input scale (C/F/K): F Enter temperature value: 32.5 Enter an output scale (C/F/K): C 32.5 F is equal to 0.3 C. Another conversion? (y/n): y Enter an input scale (C/F/K): K Enter temperature value: -151398851 Enter temperature value: 273.15 Enter an output scale (C/F/K): F 273.15 K is equal to 32.0 F. Another conversion? (y/n): y Enter an input scale (C/F/K): q Invalid scale entered. Enter an input scale (C/F/K): f Invalid scale entered. Enter an input scale (C/F/K): F Enter temperature value: 32.5 Enter an output scale (C/F/K): C 32.5 F is equal to 0.3 C. Another conversion? (y/n): n
You must follow the syntax shown below when writing a Java program.
Your code must meet each of the following requirements.
x
or y
will make sense, but not oftenEmail only the java files. Make sure they are sent as attachments.