11package com .thealgorithms .conversions ;
22
3+ /**
4+ * Utility class for converting temperatures between Celsius, Fahrenheit, and Kelvin.
5+ *
6+ * <p>Formulas reference:
7+ * <a href="https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature">...</a>
8+ * <p>Note: Some of the formulas presented on the wikipedia page are not correct
9+ *
10+ * <p>Examples:
11+ * <ul>
12+ * <li>0 °C = 32 °F = 273.15 K</li>
13+ * <li>-40 °C = -40 °F</li>
14+ * </ul>
15+ */
316public class TemperatureConverter {
417
518 private TemperatureConverter () {
619 }
720
8- // =============================================================
9- // Celsius to Fahrenheit
10- // =============================================================
21+ /**
22+ * Converts Celsius to Fahrenheit.
23+ * Formula: °C × 9/5 + 32
24+ */
1125 public static Double celsiusToFahrenheit (Double celsius ) {
1226 if (celsius < -273.15 ) {
1327 throw new IllegalArgumentException ("Temperature below absolute zero (-273.15°C)." );
@@ -24,9 +38,10 @@ public static String celsiusToFahrenheit(String celsius) {
2438 return ((value * 1.8 ) + 32 ) + "°F" ;
2539 }
2640
27- // =============================================================
28- // Celsius to Kelvin
29- // =============================================================
41+ /**
42+ * Converts Celsius to Kelvin.
43+ * Formula: °C + 273.15
44+ */
3045 public static Double celsiusToKelvin (Double celsius ) {
3146 if (celsius < -273.15 ) {
3247 throw new IllegalArgumentException ("Temperature below absolute zero (-273.15°C)." );
@@ -43,9 +58,10 @@ public static String celsiusToKelvin(String celsius) {
4358 return (value + 273.15 ) + " K" ;
4459 }
4560
46- // =============================================================
47- // Fahrenheit to Celsius
48- // =============================================================
61+ /**
62+ * Converts: Fahrenheit to Celsius.
63+ * Formula: (°F - 32) * 5/9
64+ */
4965 public static Double fahrenheitToCelsius (Double fahrenheit ) {
5066 if (fahrenheit < -459.67 ) {
5167 throw new IllegalArgumentException ("Temperature below absolute zero (-459.67°F)." );
@@ -62,9 +78,10 @@ public static String fahrenheitToCelsius(String fahrenheit) {
6278 return ((value - 32 ) * (5.0 / 9.0 )) + "°C" ;
6379 }
6480
65- // =============================================================
66- // Fahrenheit to Kelvin
67- // =============================================================
81+ /**
82+ * Converts: Fahrenheit to Kelvin.
83+ * Formula: (°F - 32) * 5/9 + 273.15
84+ */
6885 public static Double fahrenheitToKelvin (Double fahrenheit ) {
6986 if (fahrenheit < -459.67 ) {
7087 throw new IllegalArgumentException ("Temperature below absolute zero (-459.67°F)." );
@@ -81,9 +98,10 @@ public static String fahrenheitToKelvin(String fahrenheit) {
8198 return ((value - 32 ) * (5.0 / 9.0 ) + 273.15 ) + " K" ;
8299 }
83100
84- // =============================================================
85- // Kelvin to Celsius
86- // =============================================================
101+ /**
102+ * Converts: Kelvin to Celsius.
103+ * Formula: K - 273.15
104+ */
87105 public static Double kelvinToCelsius (Double kelvin ) {
88106 if (kelvin < 0 ) {
89107 throw new IllegalArgumentException ("Temperature below absolute zero (0 K)." );
@@ -100,14 +118,15 @@ public static String kelvinToCelsius(String kelvin) {
100118 return (value - 273.15 ) + "°C" ;
101119 }
102120
103- // =============================================================
104- // Kelvin to Fahrenheit
105- // =============================================================
121+ /**
122+ * Converts: Kelvin to Fahrenheit.
123+ * Formula: (K - 273.15) * 9/5 + 32
124+ */
106125 public static Double kelvinToFahrenheit (Double kelvin ) {
107126 if (kelvin < 0 ) {
108127 throw new IllegalArgumentException ("Temperature below absolute zero (0 K)." );
109128 }
110- return 1.8 * (kelvin - 273 ) + 32 ;
129+ return 1.8 * (kelvin - 273.15 ) + 32 ;
111130 }
112131
113132 public static String kelvinToFahrenheit (String kelvin ) {
@@ -116,7 +135,7 @@ public static String kelvinToFahrenheit(String kelvin) {
116135 if (value < 0 ) {
117136 throw new IllegalArgumentException ("Temperature below absolute zero (0 K)." );
118137 }
119- return (1.8 * (value - 273 ) + 32 ) + "°F" ;
138+ return (1.8 * (value - 273.15 ) + 32 ) + "°F" ;
120139 }
121140
122141}
0 commit comments