forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureConverter.java
More file actions
83 lines (76 loc) · 2.21 KB
/
TemperatureConverter.java
File metadata and controls
83 lines (76 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;
}
}