TemperatureConvertor

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

Program Syntax

You must follow the syntax shown below when writing a Java program.

Styling Conventions

Your code must meet each of the following requirements.

  • Associate a block with every conditional branch
  • A left-curly will never have a character after it on any line
  • The indentation level of all lines that are directly contained within a block is identical.
  • The indentation level increases by one when creating a new block.
  • All variable names begin with lower-case letters
  • All variable names must be full words or phrases. Do not abbreviate. Occassionaly, symbolic names such as x or y will make sense, but not often
  • All class names begin with upper-case letters
  • No empty lines unless they separate things like blocks of code, methods, or variable declarations
  • Delete all spurious comments
  • Use spaces in expressions. A single space should surround every variable, number, and operator.
  • Never have a line longer than about 80 characters (this is flexible and I'm not going to get overly picky, but 80 is a good rule of thumb)

Submission

Email only the java files. Make sure they are sent as attachments.