Skip to content

Commit 8a339ef

Browse files
Add Temperature Conversion Utility (#7066)
- Implements conversions between Celsius, Fahrenheit, and Kelvin - Includes all 6 conversion methods - Adds comprehensive unit tests with edge cases - Fixes #6936
1 parent a008cc2 commit 8a339ef

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* A utility class to convert between different temperature units.
5+
*
6+
* <p>This class supports conversions between the following units:
7+
* <ul>
8+
* <li>Celsius</li>
9+
* <li>Fahrenheit</li>
10+
* <li>Kelvin</li>
11+
* </ul>
12+
*
13+
* <p>This class is final and cannot be instantiated.
14+
*
15+
* @author krishna-medapati (https://github.com/krishna-medapati)
16+
* @see <a href="https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature">Wikipedia: Temperature Conversion</a>
17+
*/
18+
public final class TemperatureConverter {
19+
20+
private TemperatureConverter() {
21+
}
22+
23+
public static double celsiusToFahrenheit(double celsius) {
24+
return celsius * 9.0 / 5.0 + 32.0;
25+
}
26+
27+
public static double celsiusToKelvin(double celsius) {
28+
return celsius + 273.15;
29+
}
30+
31+
public static double fahrenheitToCelsius(double fahrenheit) {
32+
return (fahrenheit - 32.0) * 5.0 / 9.0;
33+
}
34+
35+
public static double fahrenheitToKelvin(double fahrenheit) {
36+
return (fahrenheit - 32.0) * 5.0 / 9.0 + 273.15;
37+
}
38+
39+
public static double kelvinToCelsius(double kelvin) {
40+
return kelvin - 273.15;
41+
}
42+
43+
public static double kelvinToFahrenheit(double kelvin) {
44+
return (kelvin - 273.15) * 9.0 / 5.0 + 32.0;
45+
}
46+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class TemperatureConverterTest {
8+
9+
private static final double DELTA = 0.01;
10+
11+
@Test
12+
void testCelsiusToFahrenheit() {
13+
assertEquals(32.0, TemperatureConverter.celsiusToFahrenheit(0.0), DELTA);
14+
assertEquals(212.0, TemperatureConverter.celsiusToFahrenheit(100.0), DELTA);
15+
assertEquals(-40.0, TemperatureConverter.celsiusToFahrenheit(-40.0), DELTA);
16+
assertEquals(98.6, TemperatureConverter.celsiusToFahrenheit(37.0), DELTA);
17+
}
18+
19+
@Test
20+
void testCelsiusToKelvin() {
21+
assertEquals(273.15, TemperatureConverter.celsiusToKelvin(0.0), DELTA);
22+
assertEquals(373.15, TemperatureConverter.celsiusToKelvin(100.0), DELTA);
23+
assertEquals(233.15, TemperatureConverter.celsiusToKelvin(-40.0), DELTA);
24+
}
25+
26+
@Test
27+
void testFahrenheitToCelsius() {
28+
assertEquals(0.0, TemperatureConverter.fahrenheitToCelsius(32.0), DELTA);
29+
assertEquals(100.0, TemperatureConverter.fahrenheitToCelsius(212.0), DELTA);
30+
assertEquals(-40.0, TemperatureConverter.fahrenheitToCelsius(-40.0), DELTA);
31+
assertEquals(37.0, TemperatureConverter.fahrenheitToCelsius(98.6), DELTA);
32+
}
33+
34+
@Test
35+
void testFahrenheitToKelvin() {
36+
assertEquals(273.15, TemperatureConverter.fahrenheitToKelvin(32.0), DELTA);
37+
assertEquals(373.15, TemperatureConverter.fahrenheitToKelvin(212.0), DELTA);
38+
assertEquals(233.15, TemperatureConverter.fahrenheitToKelvin(-40.0), DELTA);
39+
}
40+
41+
@Test
42+
void testKelvinToCelsius() {
43+
assertEquals(0.0, TemperatureConverter.kelvinToCelsius(273.15), DELTA);
44+
assertEquals(100.0, TemperatureConverter.kelvinToCelsius(373.15), DELTA);
45+
assertEquals(-273.15, TemperatureConverter.kelvinToCelsius(0.0), DELTA);
46+
}
47+
48+
@Test
49+
void testKelvinToFahrenheit() {
50+
assertEquals(32.0, TemperatureConverter.kelvinToFahrenheit(273.15), DELTA);
51+
assertEquals(212.0, TemperatureConverter.kelvinToFahrenheit(373.15), DELTA);
52+
assertEquals(-40.0, TemperatureConverter.kelvinToFahrenheit(233.15), DELTA);
53+
}
54+
}

0 commit comments

Comments
 (0)