-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemperatureConverter.java
61 lines (49 loc) · 2.06 KB
/
TemperatureConverter.java
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
import java.util.Scanner;
public class TemperatureConverter {
// Convert Celsius to Fahrenheit and Kelvin
public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}
public static double celsiusToKelvin(double celsius) {
return celsius + 273.15;
}
// Convert Fahrenheit to Celsius and Kelvin
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
public static double fahrenheitToKelvin(double fahrenheit) {
return (fahrenheitToCelsius(fahrenheit) + 273.15);
}
// Convert Kelvin to Celsius and Fahrenheit
public static double kelvinToCelsius(double kelvin) {
return kelvin - 273.15;
}
public static double kelvinToFahrenheit(double kelvin) {
return (kelvinToCelsius(kelvin) * 9 / 5) + 32;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter temperature value: ");
double temperature = scanner.nextDouble();
System.out.print("Enter unit (C for Celsius, F for Fahrenheit, K for Kelvin): ");
char unit = scanner.next().toUpperCase().charAt(0);
switch (unit) {
case 'C':
System.out.printf("%.2f°C is %.2f°F and %.2fK.\n", temperature,
celsiusToFahrenheit(temperature), celsiusToKelvin(temperature));
break;
case 'F':
System.out.printf("%.2f°F is %.2f°C and %.2fK.\n", temperature,
fahrenheitToCelsius(temperature), fahrenheitToKelvin(temperature));
break;
case 'K':
System.out.printf("%.2fK is %.2f°C and %.2f°F.\n", temperature,
kelvinToCelsius(temperature), kelvinToFahrenheit(temperature));
break;
default:
System.out.println("Invalid unit entered.");
break;
}
scanner.close();
}
}