@@ -24,46 +24,89 @@ public TemperatureUnit(double value, String name) {
24
24
}
25
25
26
26
/**
27
- * <p>
28
- * getConversionFactor.
29
- * </p>
27
+ * Get conversion factor for temperature unit conversions to Kelvin. Note: This is primarily for
28
+ * understanding scale, not for direct conversions including offsets.
30
29
*
31
- * @param name a {@link java.lang.String} object
32
- * @return a double
30
+ * @param name a {@link java.lang.String} object representing the temperature unit
31
+ * @return a double representing the conversion factor relative to Kelvin
33
32
*/
34
33
public double getConversionFactor (String name ) {
35
- double conversionFactor = 1.0 ;
36
34
switch (name ) {
37
35
case "K" :
38
- conversionFactor = 1.0 ;
39
- break ;
40
- case "R" :
41
- conversionFactor = 5.0 / 9.0 ;
42
- break ;
36
+ return 1.0 ;
37
+ case "C" :
38
+ return 1.0 ; // Same scale as Kelvin
43
39
case "F" :
44
- conversionFactor = 5.0 / 9.0 ;
45
- break ;
40
+ return 5.0 / 9.0 ; // Scale factor for Fahrenheit to Kelvin
41
+ case "R" :
42
+ return 5.0 / 9.0 ; // Scale factor for Rankine to Kelvin
43
+ default :
44
+ throw new IllegalArgumentException ("Unknown unit: " + name );
46
45
}
47
- return conversionFactor ;
48
46
}
49
47
50
- /** {@inheritDoc} */
48
+ /**
49
+ * Convert a value from one temperature unit to another.
50
+ *
51
+ * @param value the temperature value to convert
52
+ * @param fromUnit the unit of the input temperature
53
+ * @param toUnit the unit to convert the temperature to
54
+ * @return the converted temperature value
55
+ */
51
56
@ Override
52
- public double getValue (double val , String fromunit , String tounit ) {
53
- invalue = val ;
54
- return getConversionFactor (fromunit ) / getConversionFactor (tounit ) * invalue ;
57
+ public double getValue (double value , String fromUnit , String toUnit ) {
58
+ if (fromUnit .equals (toUnit )) {
59
+ return value ;
60
+ }
61
+
62
+ // Convert input to Kelvin first
63
+ double tempInKelvin = value ;
64
+ if (fromUnit .equals ("C" )) {
65
+ tempInKelvin += 273.15 ;
66
+ } else if (fromUnit .equals ("F" )) {
67
+ tempInKelvin = (value - 32 ) * 5.0 / 9.0 + 273.15 ;
68
+ } else if (fromUnit .equals ("R" )) {
69
+ tempInKelvin = value * 5.0 / 9.0 ;
70
+ }
71
+
72
+ // Convert from Kelvin to target unit
73
+ if (toUnit .equals ("K" )) {
74
+ return tempInKelvin ;
75
+ } else if (toUnit .equals ("C" )) {
76
+ return tempInKelvin - 273.15 ;
77
+ } else if (toUnit .equals ("F" )) {
78
+ return (tempInKelvin - 273.15 ) * 9.0 / 5.0 + 32 ;
79
+ } else if (toUnit .equals ("R" )) {
80
+ return tempInKelvin * 9.0 / 5.0 ;
81
+ }
82
+
83
+ throw new IllegalArgumentException ("Unsupported unit: " + toUnit );
55
84
}
56
85
57
- /** {@inheritDoc} */
86
+ /**
87
+ * Convert a given temperature value from Kelvin to a specified unit.
88
+ *
89
+ * @param toUnit the target unit to convert the temperature to ("C", "F", "R")
90
+ * @return the converted temperature value in the target unit
91
+ */
58
92
@ Override
59
- public double getValue (String tounit ) {
60
- if (tounit .equals ("C" )) {
61
- return getConversionFactor (inunit ) / getConversionFactor ("K" ) * invalue - 273.15 ;
62
- }
63
- if (tounit .equals ("F" )) {
64
- return (getConversionFactor (inunit ) / getConversionFactor ("K" ) * invalue - 273.15 ) * 1.8
65
- + 32.0 ;
93
+ public double getValue (String toUnit ) {
94
+ switch (toUnit ) {
95
+ case "K" :
96
+ // Convert from Kelvin to Kelvin
97
+ return invalue ;
98
+ case "C" :
99
+ // Convert from Kelvin to Celsius
100
+ return invalue - 273.15 ;
101
+ case "F" :
102
+ // Convert from Kelvin to Fahrenheit
103
+ return invalue * 9.0 /5.0 - 459.67 ;
104
+ case "R" :
105
+ // Convert from Kelvin to Rankine
106
+ return invalue * 9.0 / 5.0 ;
107
+ default :
108
+ // Handle unsupported units
109
+ throw new IllegalArgumentException ("Unsupported conversion unit: " + toUnit );
66
110
}
67
- return getConversionFactor (inunit ) / getConversionFactor (tounit ) * invalue ;
68
111
}
69
112
}
0 commit comments