Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.thealgorithms.conversions;

/**
* A utility class for converting temperatures between
* Celsius, Fahrenheit, and Kelvin scales.
*
* This class provides static methods, so it can be used directly
* without creating an instance.
*/
public class TemperatureConverter {

/**
* Converts temperature from Celsius to Fahrenheit.
*
* Formula: (°C × 9/5) + 32 = °F
*
// * @param celsius temperature in Celsius
* @return equivalent temperature in Fahrenheit
*/
static double celsiusToFahrenheit(double celsius) {
return (celsius * 9.0) / 5.0 + 32.0;
}

/**
* Converts temperature from Celsius to Kelvin.
*
* Formula: °C + 273.15 = K
*
// * @param celsius temperature in Celsius
* @return equivalent temperature in Kelvin
*/
static double celsiusToKelvin(double celsius) {
return celsius + 273.15;
}

/**
* Converts temperature from Fahrenheit to Celsius.
*
* Formula: (°F − 32) × 5/9 = °C
*
// * @param fahrenheit temperature in Fahrenheit
* @return equivalent temperature in Celsius
*/
static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * 5.0 / 9.0;
}

/**
* Converts temperature from Fahrenheit to Kelvin.
*
* Formula: (°F − 32) × 5/9 + 273.15 = K
*
// * @param fahrenheit temperature in Fahrenheit
* @return equivalent temperature in Kelvin
*/
static double fahrenheitToKelvin(double fahrenheit) {
return (fahrenheit - 32.0) * 5.0 / 9.0 + 273.15;
}

/**
* Converts temperature from Kelvin to Celsius.
*
* Formula: K − 273.15 = °C
*
// * @param kelvin temperature in Kelvin
* @return equivalent temperature in Celsius
*/
static double kelvinToCelsius(double kelvin) {
return kelvin - 273.15;
}

/**
* Converts temperature from Kelvin to Fahrenheit.
*
* Formula: (K − 273.15) × 9/5 + 32 = °F
*
// * @param kelvin temperature in Kelvin
* @return equivalent temperature in Fahrenheit
*/
static double kelvinToFahrenheit(double kelvin) {
return (kelvin - 273.15) * 9.0 / 5.0 + 32.0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.thealgorithms.conversions;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class TemperatureConverterTest {

@Test
public void testCelsiusToFahrenheit() {
assertEquals(32.0, TemperatureConverter.celsiusToFahrenheit(0), 0.001);
assertEquals(212.0, TemperatureConverter.celsiusToFahrenheit(100), 0.001);
}

@Test
public void testCelsiusToKelvin() {
assertEquals(273.15, TemperatureConverter.celsiusToKelvin(0), 0.001);
assertEquals(373.15, TemperatureConverter.celsiusToKelvin(100), 0.001);
}

@Test
public void testFahrenheitToCelsius() {
assertEquals(0.0, TemperatureConverter.fahrenheitToCelsius(32.0), 0.001);
assertEquals(100.0, TemperatureConverter.fahrenheitToCelsius(212.0), 0.001);
}

@Test
public void testFahrenheitToKelvin() {
assertEquals(273.15, TemperatureConverter.fahrenheitToKelvin(32.0), 0.001);
assertEquals(373.15, TemperatureConverter.fahrenheitToKelvin(212.0), 0.001);
}

@Test
public void testKelvinToCelsius() {
assertEquals(0.0, TemperatureConverter.kelvinToCelsius(273.15), 0.001);
assertEquals(100.0, TemperatureConverter.kelvinToCelsius(373.15), 0.001);
}

@Test
public void testKelvinToFahrenheit() {
assertEquals(32.0, TemperatureConverter.kelvinToFahrenheit(273.15), 0.001);
assertEquals(212.0, TemperatureConverter.kelvinToFahrenheit(373.15), 0.001);
}
}
Loading