Skip to content

Commit e84202a

Browse files
added all the temperature converter static methods
1 parent 0837424 commit e84202a

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* A utility class for converting temperatures between
5+
* Celsius, Fahrenheit, and Kelvin scales.
6+
*
7+
* This class provides static methods, so it can be used directly
8+
* without creating an instance.
9+
*/
10+
public class TemperatureConverter {
11+
12+
/**
13+
* Converts temperature from Celsius to Fahrenheit.
14+
*
15+
* Formula: (°C × 9/5) + 32 = °F
16+
*
17+
// * @param celsius temperature in Celsius
18+
* @return equivalent temperature in Fahrenheit
19+
*/
20+
static double celsiusToFahrenheit(double celsius) {
21+
return (celsius * 9.0) / 5.0 + 32.0;
22+
}
23+
24+
/**
25+
* Converts temperature from Celsius to Kelvin.
26+
*
27+
* Formula: °C + 273.15 = K
28+
*
29+
// * @param celsius temperature in Celsius
30+
* @return equivalent temperature in Kelvin
31+
*/
32+
static double celsiusToKelvin(double celsius) {
33+
return celsius + 273.15;
34+
}
35+
36+
/**
37+
* Converts temperature from Fahrenheit to Celsius.
38+
*
39+
* Formula: (°F − 32) × 5/9 = °C
40+
*
41+
// * @param fahrenheit temperature in Fahrenheit
42+
* @return equivalent temperature in Celsius
43+
*/
44+
static double fahrenheitToCelsius(double fahrenheit) {
45+
return (fahrenheit - 32.0) * 5.0 / 9.0;
46+
}
47+
48+
/**
49+
* Converts temperature from Fahrenheit to Kelvin.
50+
*
51+
* Formula: (°F − 32) × 5/9 + 273.15 = K
52+
*
53+
// * @param fahrenheit temperature in Fahrenheit
54+
* @return equivalent temperature in Kelvin
55+
*/
56+
static double fahrenheitToKelvin(double fahrenheit) {
57+
return (fahrenheit - 32.0) * 5.0 / 9.0 + 273.15;
58+
}
59+
60+
/**
61+
* Converts temperature from Kelvin to Celsius.
62+
*
63+
* Formula: K − 273.15 = °C
64+
*
65+
// * @param kelvin temperature in Kelvin
66+
* @return equivalent temperature in Celsius
67+
*/
68+
static double kelvinToCelsius(double kelvin) {
69+
return kelvin - 273.15;
70+
}
71+
72+
/**
73+
* Converts temperature from Kelvin to Fahrenheit.
74+
*
75+
* Formula: (K − 273.15) × 9/5 + 32 = °F
76+
*
77+
// * @param kelvin temperature in Kelvin
78+
* @return equivalent temperature in Fahrenheit
79+
*/
80+
static double kelvinToFahrenheit(double kelvin) {
81+
return (kelvin - 273.15) * 9.0 / 5.0 + 32.0;
82+
}
83+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class TemperatureConverterTest {
7+
8+
@Test
9+
public void testCelsiusToFahrenheit() {
10+
assertEquals(32.0, TemperatureConverter.celsiusToFahrenheit(0), 0.001);
11+
assertEquals(212.0, TemperatureConverter.celsiusToFahrenheit(100), 0.001);
12+
}
13+
14+
@Test
15+
public void testCelsiusToKelvin() {
16+
assertEquals(273.15, TemperatureConverter.celsiusToKelvin(0), 0.001);
17+
assertEquals(373.15, TemperatureConverter.celsiusToKelvin(100), 0.001);
18+
}
19+
20+
@Test
21+
public void testFahrenheitToCelsius() {
22+
assertEquals(0.0, TemperatureConverter.fahrenheitToCelsius(32.0), 0.001);
23+
assertEquals(100.0, TemperatureConverter.fahrenheitToCelsius(212.0), 0.001);
24+
}
25+
26+
@Test
27+
public void testFahrenheitToKelvin() {
28+
assertEquals(273.15, TemperatureConverter.fahrenheitToKelvin(32.0), 0.001);
29+
assertEquals(373.15, TemperatureConverter.fahrenheitToKelvin(212.0), 0.001);
30+
}
31+
32+
@Test
33+
public void testKelvinToCelsius() {
34+
assertEquals(0.0, TemperatureConverter.kelvinToCelsius(273.15), 0.001);
35+
assertEquals(100.0, TemperatureConverter.kelvinToCelsius(373.15), 0.001);
36+
}
37+
38+
@Test
39+
public void testKelvinToFahrenheit() {
40+
assertEquals(32.0, TemperatureConverter.kelvinToFahrenheit(273.15), 0.001);
41+
assertEquals(212.0, TemperatureConverter.kelvinToFahrenheit(373.15), 0.001);
42+
}
43+
}

0 commit comments

Comments
 (0)