Question:
Looking for a class that will change the temperature from one unit to another.
Answer:
This is an example of a class that has methods that will convert the temperature from one unit to another.
Code:
public class TemperatureConverter { public static void main(String[] args) { System.out.println("Water freezes at:"); System.out.println(" 0C"); System.out.println(" " + celsiusToFahrenheit(0) + "F"); System.out.println(" " + celsiusToKelvin(0) + "K"); System.out.println("Absolute zero:"); System.out.println(" 0K"); System.out.println(" " + kelvinToCelsius(0) + "C"); System.out.println(" " + kelvinToFahrenheit(0) + "F"); System.out.println("Body Temperature:"); System.out.println(" 98.6F"); System.out.println(" " + fahrenheitToCelsious(98.6) + "C"); System.out.println(" " + fahrenheitToKelvin(96.6) + "K"); } static double celsiusToFahrenheit(double c){ return((c * 1.8000) + 32); } static double celsiusToKelvin (double c){ return (c + 273.15); } static double kelvinToCelsius (double k){ return (k - 273.15); } static double kelvinToFahrenheit (double k){ return (((k - 273.15) * 1.8000) + 32); } static double fahrenheitToCelsious (double f){ return ((f - 32) / 1.8000 ); } static double fahrenheitToKelvin (double f){ return (((f - 32) / 1.8000) + 273.15) ; } }
Output:
$ java TemperatureConverter Water freezes at: 0C 32.0F 273.15K Absolute zero: 0K -273.15C -459.66999999999996F Body Temperature: 98.6F 36.99999999999999C 309.0388888888889K