|
1 | 1 | package com.thealgorithms.conversions;
|
2 | 2 |
|
3 |
| -import java.text.DecimalFormat; |
4 |
| -import java.util.Scanner; |
5 | 3 | /**
|
6 | 4 | * Convets the temperatures from user to Celsius/Fahrenheit/Kelvin
|
7 | 5 | *
|
8 | 6 | * @author Tharun S.M.
|
9 | 7 | */
|
10 | 8 |
|
11 | 9 | public class TemperatureConversion {
|
12 |
| - /* |
13 |
| - * Object of DecimalFormat Class |
14 |
| - * to round the output to 2 decimal point value |
15 |
| - */ |
16 |
| - private static final DecimalFormat rnd = new DecimalFormat("0.00"); |
17 |
| - /* |
18 |
| - * Main method |
19 |
| - * |
20 |
| - * @param args Command line arguments |
21 |
| - */ |
22 |
| - |
23 |
| - public static void main(String[] args) { |
24 |
| - |
25 |
| - Scanner sc = new Scanner(System.in); |
26 |
| - |
27 |
| - // Get the type of temprature input |
28 |
| - System.out.println("Enter the temperature input type (use numbers)"); |
29 |
| - System.out.println("1 => Celsius"); |
30 |
| - System.out.println("2 => Fahrenheit"); |
31 |
| - System.out.println("3 => Kelvin"); |
32 |
| - int fromType = sc.nextInt(); |
33 |
| - |
34 |
| - // get the actual temprature |
35 |
| - System.out.println("Enter the temperature :"); |
36 |
| - String a = sc.next(); |
37 |
| - float inTemp = Float.parseFloat(a); |
38 |
| - |
39 |
| - // get the type you want to convert your temperature to |
40 |
| - System.out.println("Type you want to convert your temperature to (use numbers)"); |
41 |
| - System.out.println("1 => Celsius"); |
42 |
| - System.out.println("2 => Fahrenheit"); |
43 |
| - System.out.println("3 => Kelvin"); |
44 |
| - int toType = sc.nextInt(); |
45 |
| - |
46 |
| - if (fromType == toType) { |
47 |
| - System.out.println("Your Temperature is the same !!"); |
48 |
| - } else if (fromType == 1 && toType == 2) { |
49 |
| - System.out.println("Converted value: " + convertCelsiusToFahrenheit(inTemp) + " °F"); |
50 |
| - } else if (fromType == 1 && toType == 3) { |
51 |
| - System.out.println("Converted value: " + convertCelsiusToKelvin(inTemp) + " K"); |
52 |
| - } else if (fromType == 2 && toType == 1) { |
53 |
| - System.out.println("Converted value: " + convertFahrenheitToCelsius(inTemp) + " °C"); |
54 |
| - } else if (fromType == 2 && toType == 3) { |
55 |
| - System.out.println("Converted value: " + convertFahrenheitToKelvin(inTemp) + " K"); |
56 |
| - } else if (fromType == 3 && toType == 1) { |
57 |
| - System.out.println("Converted value: " + convertKelvinToCelsius(inTemp) + " °C"); |
58 |
| - } else if (fromType == 3 && toType == 2) { |
59 |
| - System.out.println("Converted value: " + convertKelvinToFahrenheit(inTemp) + " °F"); |
60 |
| - } else { |
61 |
| - System.out.println("Please check your input and output types"); |
62 |
| - } |
63 |
| - sc.close(); |
64 |
| - } |
65 |
| - |
66 |
| - /** |
67 |
| - * This method converts a Kelvin to Celsius. |
68 |
| - * |
69 |
| - * @param param float paramter |
70 |
| - * @return String |
71 |
| - */ |
72 |
| - public static String convertKelvinToCelsius(float inTemp) { |
73 |
| - |
74 |
| - double result = 0; |
75 |
| - result = inTemp - 273.15; |
76 |
| - return rnd.format(result); |
77 |
| - } |
| 10 | + private static final double CELSIUS_SLOPE = 1.0; |
| 11 | + private static final double CELSIUS_INTERCEPT = 0.0; |
| 12 | + private static final double FAHRENHEIT_SLOPE = 1.8; |
| 13 | + private static final double FAHRENHEIT_INTERCEPT = 32.0; |
| 14 | + private static final double KELVIN_SLOPE = 1.0; |
| 15 | + private static final double KELVIN_INTERCEPT = 273.15; |
78 | 16 |
|
79 | 17 | /**
|
80 |
| - * This method converts a Kelvin to Fahrenheit. |
| 18 | + * This function convert current temperature type to required temperature type |
81 | 19 | *
|
82 |
| - * @param param float paramter |
83 |
| - * @return String |
| 20 | + * @param temperature double, fromType String & toType String |
| 21 | + * @return double |
84 | 22 | */
|
85 |
| - public static String convertKelvinToFahrenheit(float inTemp) { |
| 23 | + public static double convertTemperature(double temperature, String fromType, String toType) { |
86 | 24 |
|
87 |
| - double result = 0; |
88 |
| - result = (inTemp * 1.8) - 459.67; |
89 |
| - return rnd.format(result); |
90 |
| - } |
| 25 | + if (fromType.equalsIgnoreCase(toType)) { |
| 26 | + return temperature; |
| 27 | + } |
91 | 28 |
|
92 |
| - /** |
93 |
| - * This method converts a Fahrenheit to Kelvin. |
94 |
| - * |
95 |
| - * @param param float paramter |
96 |
| - * @return String |
97 |
| - */ |
98 |
| - public static String convertFahrenheitToKelvin(float inTemp) { |
| 29 | + double[] slopeAndIntercept = getSlopeAndIntercept(fromType, toType); |
99 | 30 |
|
100 |
| - double result = 0; |
101 |
| - result = (inTemp + 459.67) / 1.8; |
102 |
| - return rnd.format(result); |
| 31 | + double fromSlope = slopeAndIntercept[0]; |
| 32 | + double fromIntercept = slopeAndIntercept[1]; |
| 33 | + double toSlope = slopeAndIntercept[2]; |
| 34 | + double toIntercept = slopeAndIntercept[3]; |
| 35 | + // converting to celsius temperature first, then to our required temperature |
| 36 | + double celsiusTemperature = (temperature - fromIntercept) / fromSlope; |
| 37 | + return celsiusTemperature * toSlope + toIntercept; |
103 | 38 | }
|
104 | 39 |
|
105 | 40 | /**
|
106 |
| - * This method converts a Fahrenheit to Kelvin. |
| 41 | + * This function returns Slopes and Intercepts of from and to temperature types |
107 | 42 | *
|
108 |
| - * @param param float paramter |
109 |
| - * @return String |
| 43 | + * @param fromType String & toType String |
| 44 | + * @return double array of Slopes and Intercepts |
110 | 45 | */
|
111 |
| - public static String convertFahrenheitToCelsius(float inTemp) { |
112 |
| - |
113 |
| - double result = 0; |
114 |
| - result = (inTemp - 32) / 1.8; |
115 |
| - return rnd.format(result); |
| 46 | + public static double[] getSlopeAndIntercept(String fromType, String toType) { |
| 47 | + |
| 48 | + double[] slopeAndIntercept = new double[4]; |
| 49 | + if (fromType.equalsIgnoreCase("Celsius") && toType.equalsIgnoreCase("Fahrenheit")) { |
| 50 | + slopeAndIntercept[0] = CELSIUS_SLOPE; |
| 51 | + slopeAndIntercept[1] = CELSIUS_INTERCEPT; |
| 52 | + slopeAndIntercept[2] = FAHRENHEIT_SLOPE; |
| 53 | + slopeAndIntercept[3] = FAHRENHEIT_INTERCEPT; |
| 54 | + return slopeAndIntercept; |
| 55 | + } else if (fromType.equalsIgnoreCase("Fahrenheit") && toType.equalsIgnoreCase("Celsius")) { |
| 56 | + slopeAndIntercept[0] = FAHRENHEIT_SLOPE; |
| 57 | + slopeAndIntercept[1] = FAHRENHEIT_INTERCEPT; |
| 58 | + slopeAndIntercept[2] = CELSIUS_SLOPE; |
| 59 | + slopeAndIntercept[3] = CELSIUS_INTERCEPT; |
| 60 | + return slopeAndIntercept; |
| 61 | + } else if (fromType.equalsIgnoreCase("Celsius") && toType.equalsIgnoreCase("Kelvin")) { |
| 62 | + slopeAndIntercept[0] = CELSIUS_SLOPE; |
| 63 | + slopeAndIntercept[1] = CELSIUS_INTERCEPT; |
| 64 | + slopeAndIntercept[2] = KELVIN_SLOPE; |
| 65 | + slopeAndIntercept[3] = KELVIN_INTERCEPT; |
| 66 | + return slopeAndIntercept; |
| 67 | + } else if (fromType.equalsIgnoreCase("Kelvin") && toType.equalsIgnoreCase("Celsius")) { |
| 68 | + slopeAndIntercept[0] = KELVIN_SLOPE; |
| 69 | + slopeAndIntercept[1] = KELVIN_INTERCEPT; |
| 70 | + slopeAndIntercept[2] = CELSIUS_SLOPE; |
| 71 | + slopeAndIntercept[3] = CELSIUS_INTERCEPT; |
| 72 | + return slopeAndIntercept; |
| 73 | + } else if (fromType.equalsIgnoreCase("Kelvin") && toType.equalsIgnoreCase("Fahrenheit")) { |
| 74 | + slopeAndIntercept[0] = KELVIN_SLOPE; |
| 75 | + slopeAndIntercept[1] = KELVIN_INTERCEPT; |
| 76 | + slopeAndIntercept[2] = FAHRENHEIT_SLOPE; |
| 77 | + slopeAndIntercept[3] = FAHRENHEIT_INTERCEPT; |
| 78 | + return slopeAndIntercept; |
| 79 | + } else if (fromType.equalsIgnoreCase("Fahrenheit") && toType.equalsIgnoreCase("Kelvin")) { |
| 80 | + slopeAndIntercept[0] = FAHRENHEIT_SLOPE; |
| 81 | + slopeAndIntercept[1] = FAHRENHEIT_INTERCEPT; |
| 82 | + slopeAndIntercept[2] = KELVIN_SLOPE; |
| 83 | + slopeAndIntercept[3] = KELVIN_INTERCEPT; |
| 84 | + return slopeAndIntercept; |
| 85 | + } else { |
| 86 | + throw new IllegalArgumentException("Unsupported temperature conversion"); |
| 87 | + } |
116 | 88 | }
|
117 | 89 |
|
118 |
| - /** |
119 |
| - * This method converts a Celsius to Fahrenheit. |
120 |
| - * |
121 |
| - * @param param float paramter |
122 |
| - * @return String |
123 |
| - */ |
124 |
| - public static String convertCelsiusToFahrenheit(float inTemp) { |
| 90 | + public static void main(String[] args) { |
125 | 91 |
|
126 |
| - double result = 0; |
127 |
| - result = (inTemp * 1.8) + 32; |
128 |
| - return rnd.format(result); |
129 |
| - } |
| 92 | + double temperature = 100; |
| 93 | + String fromType = "Celsius"; |
| 94 | + String toType = "Fahrenheit"; |
130 | 95 |
|
131 |
| - /** |
132 |
| - * This method converts a Celsius to Kelvin. |
133 |
| - * |
134 |
| - * @param param float paramter |
135 |
| - * @return String |
136 |
| - */ |
137 |
| - public static String convertCelsiusToKelvin(float inTemp) { |
| 96 | + double convertedTemperature = convertTemperature(temperature, fromType, toType); |
138 | 97 |
|
139 |
| - double result = 0; |
140 |
| - result = inTemp + 273.15; |
141 |
| - return rnd.format(result); |
| 98 | + System.out.println("Converted temperature: " + convertedTemperature); |
142 | 99 | }
|
143 |
| - |
144 |
| - /* |
145 |
| - * Reference URL : https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature |
146 |
| - * This is not the most effecient method for Temp conversion But its easy to |
147 |
| - * understand and straight forward |
148 |
| - */ |
149 | 100 | }
|
0 commit comments