|
8 | 8 | * @author Tharun S.M.
|
9 | 9 | */
|
10 | 10 |
|
11 |
| -public |
12 |
| -class TemperatureConversion { |
13 |
| - /* |
14 |
| - * Object of DecimalFormat Class |
15 |
| - * to round the output to 2 decimal point value |
16 |
| - */ |
17 |
| -private |
18 |
| - static final DecimalFormat rnd = new DecimalFormat("0.00"); |
19 |
| - /* |
20 |
| - * Main method |
21 |
| - * |
22 |
| - * @param args Command line arguments |
23 |
| - */ |
24 |
| - |
25 |
| -public |
26 |
| - static void main(String[] args) { |
27 |
| - |
28 |
| - Scanner sc = new Scanner(System.in); |
29 |
| - |
30 |
| - // Get the type of temprature input |
31 |
| - System.out.println("Enter the temperature input type (use numbers)"); |
32 |
| - System.out.println("1 => Celsius"); |
33 |
| - System.out.println("2 => Fahrenheit"); |
34 |
| - System.out.println("3 => Kelvin"); |
35 |
| - int fromType = sc.nextInt(); |
36 |
| - |
37 |
| - // get the actual temprature |
38 |
| - System.out.println("Enter the temperature :"); |
39 |
| - String a = sc.next(); |
40 |
| - float inTemp = Float.parseFloat(a); |
41 |
| - |
42 |
| - // get the type you want to convert your temperature to |
43 |
| - System.out.println( |
44 |
| - "Type you want to convert your temperature to (use numbers)"); |
45 |
| - System.out.println("1 => Celsius"); |
46 |
| - System.out.println("2 => Fahrenheit"); |
47 |
| - System.out.println("3 => Kelvin"); |
48 |
| - int toType = sc.nextInt(); |
49 |
| - |
50 |
| - if (fromType == toType) { |
51 |
| - System.out.println("Your Temperature is the same !!"); |
52 |
| - } else if (fromType == 1 && toType == 2) { |
53 |
| - System.out.println("Converted value: " + |
54 |
| - rnd.format(convertCelsiusToFahrenheit(inTemp)) + |
55 |
| - " °F"); |
56 |
| - } else if (fromType == 1 && toType == 3) { |
57 |
| - System.out.println("Converted value: " + |
58 |
| - rnd.format(convertCelsiusToKelvin(inTemp)) + " K"); |
59 |
| - } else if (fromType == 2 && toType == 1) { |
60 |
| - System.out.println("Converted value: " + |
61 |
| - rnd.format(convertFahrenheitToCelsius(inTemp)) + |
62 |
| - " °C"); |
63 |
| - } else if (fromType == 2 && toType == 3) { |
64 |
| - System.out.println("Converted value: " + |
65 |
| - rnd.format(convertFahrenheitToKelvin(inTemp)) + " K"); |
66 |
| - } else if (fromType == 3 && toType == 1) { |
67 |
| - System.out.println("Converted value: " + |
68 |
| - rnd.format(convertKelvinToCelsius(inTemp)) + " °C"); |
69 |
| - } else if (fromType == 3 && toType == 2) { |
70 |
| - System.out.println("Converted value: " + |
71 |
| - rnd.format(convertKelvinToFahrenheit(inTemp)) + " °F"); |
72 |
| - } else { |
73 |
| - System.out.println("Please check your input and output types"); |
| 11 | +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: " + rnd.format(convertCelsiusToFahrenheit(inTemp)) + " °F"); |
| 50 | + } else if (fromType == 1 && toType == 3) { |
| 51 | + System.out.println("Converted value: " + rnd.format(convertCelsiusToKelvin(inTemp)) + " K"); |
| 52 | + } else if (fromType == 2 && toType == 1) { |
| 53 | + System.out.println("Converted value: " + rnd.format(convertFahrenheitToCelsius(inTemp)) + " °C"); |
| 54 | + } else if (fromType == 2 && toType == 3) { |
| 55 | + System.out.println("Converted value: " + rnd.format(convertFahrenheitToKelvin(inTemp)) + " K"); |
| 56 | + } else if (fromType == 3 && toType == 1) { |
| 57 | + System.out.println("Converted value: " + rnd.format(convertKelvinToCelsius(inTemp)) + " °C"); |
| 58 | + } else if (fromType == 3 && toType == 2) { |
| 59 | + System.out.println("Converted value: " + rnd.format(convertKelvinToFahrenheit(inTemp)) + " °F"); |
| 60 | + } else { |
| 61 | + System.out.println("Please check your input and output types"); |
| 62 | + } |
| 63 | + sc.close(); |
74 | 64 | }
|
75 |
| - sc.close(); |
76 |
| - } |
77 |
| - |
78 |
| - /** |
79 |
| - * This method converts a Kelvin to Celsius. |
80 |
| - * |
81 |
| - * @param param float paramter |
82 |
| - * @return double |
83 |
| - */ |
84 |
| -private |
85 |
| - static double convertKelvinToCelsius(float inTemp) { |
86 |
| - |
87 |
| - double result = 0; |
88 |
| - result = inTemp - 273.15; |
89 |
| - return result; |
90 |
| - } |
91 |
| - |
92 |
| - /** |
93 |
| - * This method converts a Kelvin to Fahrenheit. |
94 |
| - * |
95 |
| - * @param param float paramter |
96 |
| - * @return double |
97 |
| - */ |
98 |
| -private |
99 |
| - static double convertKelvinToFahrenheit(float inTemp) { |
100 |
| - |
101 |
| - double result = 0; |
102 |
| - result = (inTemp * 1.8) - 459.67; |
103 |
| - return result; |
104 |
| - } |
105 |
| - |
106 |
| - /** |
107 |
| - * This method converts a Fahrenheit to Kelvin. |
108 |
| - * |
109 |
| - * @param param float paramter |
110 |
| - * @return double |
111 |
| - */ |
112 |
| -private |
113 |
| - static double convertFahrenheitToKelvin(float inTemp) { |
114 |
| - |
115 |
| - double result = 0; |
116 |
| - result = (inTemp + 459.67) / 1.8; |
117 |
| - return result; |
118 |
| - } |
119 |
| - |
120 |
| - /** |
121 |
| - * This method converts a Fahrenheit to Kelvin. |
122 |
| - * |
123 |
| - * @param param float paramter |
124 |
| - * @return double |
125 |
| - */ |
126 |
| -private |
127 |
| - static double convertFahrenheitToCelsius(float inTemp) { |
128 |
| - |
129 |
| - double result = 0; |
130 |
| - result = (inTemp - 32) / 1.8; |
131 |
| - return result; |
132 |
| - } |
133 |
| - |
134 |
| - /** |
135 |
| - * This method converts a Celsius to Fahrenheit. |
136 |
| - * |
137 |
| - * @param param float paramter |
138 |
| - * @return double |
139 |
| - */ |
140 |
| -private |
141 |
| - static double convertCelsiusToFahrenheit(float inTemp) { |
142 |
| - |
143 |
| - double result = 0; |
144 |
| - result = (inTemp * 1.8) + 32; |
145 |
| - return result; |
146 |
| - } |
147 |
| - |
148 |
| - /** |
149 |
| - * This method converts a Celsius to Kelvin. |
150 |
| - * |
151 |
| - * @param param float paramter |
152 |
| - * @return double |
153 |
| - */ |
154 |
| -private |
155 |
| - static double convertCelsiusToKelvin(float inTemp) { |
156 |
| - |
157 |
| - double result = 0; |
158 |
| - result = inTemp + 273.15; |
159 |
| - return result; |
160 |
| - } |
161 |
| - |
162 |
| - /* |
163 |
| - * Reference URL |
164 |
| - * https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature This is |
165 |
| - * not the most effecient method for Temp conversion But its easy to |
166 |
| - * understand and straight forward |
167 |
| - */ |
| 65 | + |
| 66 | + /** |
| 67 | + * This method converts a Kelvin to Celsius. |
| 68 | + * |
| 69 | + * @param param float paramter |
| 70 | + * @return double |
| 71 | + */ |
| 72 | + private static double convertKelvinToCelsius(float inTemp) { |
| 73 | + |
| 74 | + double result = 0; |
| 75 | + result = inTemp - 273.15; |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * This method converts a Kelvin to Fahrenheit. |
| 81 | + * |
| 82 | + * @param param float paramter |
| 83 | + * @return double |
| 84 | + */ |
| 85 | + private static double convertKelvinToFahrenheit(float inTemp) { |
| 86 | + |
| 87 | + double result = 0; |
| 88 | + result = (inTemp * 1.8) - 459.67; |
| 89 | + return result; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * This method converts a Fahrenheit to Kelvin. |
| 94 | + * |
| 95 | + * @param param float paramter |
| 96 | + * @return double |
| 97 | + */ |
| 98 | + private static double convertFahrenheitToKelvin(float inTemp) { |
| 99 | + |
| 100 | + double result = 0; |
| 101 | + result = (inTemp + 459.67) / 1.8; |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * This method converts a Fahrenheit to Kelvin. |
| 107 | + * |
| 108 | + * @param param float paramter |
| 109 | + * @return double |
| 110 | + */ |
| 111 | + private static double convertFahrenheitToCelsius(float inTemp) { |
| 112 | + |
| 113 | + double result = 0; |
| 114 | + result = (inTemp - 32) / 1.8; |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * This method converts a Celsius to Fahrenheit. |
| 120 | + * |
| 121 | + * @param param float paramter |
| 122 | + * @return double |
| 123 | + */ |
| 124 | + private static double convertCelsiusToFahrenheit(float inTemp) { |
| 125 | + |
| 126 | + double result = 0; |
| 127 | + result = (inTemp * 1.8) + 32; |
| 128 | + return result; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * This method converts a Celsius to Kelvin. |
| 133 | + * |
| 134 | + * @param param float paramter |
| 135 | + * @return double |
| 136 | + */ |
| 137 | + private static double convertCelsiusToKelvin(float inTemp) { |
| 138 | + |
| 139 | + double result = 0; |
| 140 | + result = inTemp + 273.15; |
| 141 | + return result; |
| 142 | + } |
| 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 | + */ |
168 | 149 | }
|
0 commit comments