-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThermoCalc.java
151 lines (130 loc) · 4.66 KB
/
ThermoCalc.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ThermoCalc {
private JPanel panel1;
private JComboBox ThermoOp;
private JTextField tfValue;
private JTextField tfResult;
private JButton btnReset;
private JButton btnCalculate;
private JButton btnExit;
public static void main(String[] args) {
JFrame frame = new JFrame("Thermo Calculator");
frame.setContentPane(new ThermoCalc().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public ThermoCalc() {
String[] conversions = {"Celcius to Reamur", "Celcius to Fahrenheit", "Celcius to Kelvin", "Reamur to Celcius",
"Reamur to Fahrenheit", "Reamur to Kelvin", "Fahrenheit to Celcius", "Fahrenheit to Reamur", "Fahrenheit to Kelvin",
"Kelvin to Celcius", "Kelvin to Reamur", "Kelvin to Fahrenheit"};
ThermoOp.setModel(new DefaultComboBoxModel<>(conversions));
btnReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tfValue.setText("");
tfResult.setText("");
tfValue.requestFocus();
}
});
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnCalculate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedOp = (String) ThermoOp.getSelectedItem();
double Value;
try {
Value = Double.parseDouble(tfValue.getText());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid number.");
return;
}
double result = 0;
switch (selectedOp) {
case "Celcius to Reamur":
result = CtR(Value);
break;
case "Celcius to Fahrenheit":
result = CtF(Value);
break;
case "Celcius to Kelvin":
result = CtK(Value);
break;
case "Reamur to Celcius":
result = RtC(Value);
break;
case "Reamur to Fahrenheit":
result = RtF(Value);
break;
case "Reamur to Kelvin":
result = RtK(Value);
break;
case "Fahrenheit to Celcius":
result = FtC(Value);
break;
case "Fahrenheit to Reamur":
result = FtR(Value);
break;
case "Fahrenheit to Kelvin":
result = FtK(Value);
break;
case "Kelvin to Celcius":
result = KtC(Value);
break;
case "Kelvin to Reamur":
result = KtR(Value);
break;
case "Kelvin to Fahrenheit":
result = KtF (Value);
break;
default:
break;
}
tfResult.setText(String.valueOf(result));
}
});
}
public double CtR(double value) {
return value * (4.0/5.0);
}
public double CtF(double value){
return (value * 9.0/5.0) + 32.0;
}
public double CtK(double value){
return value + 273.15;
}
public double RtC(double value){
return value * 5.0/4.0;
}
public double RtF(double value){
return (value*9.0/4.0) + 32.0;
}
public double RtK(double value){
return (value*5.0/4.0) + 273.15;
}
public double FtC(double value){
return (value - 32.0) * 5.0/9.0;
}
public double FtR(double value){
return (value -32.0) * 4.0/9.0;
}
public double FtK(double value){
return (value-32.0) * 5.0/9.0 + 273.15;
}
public double KtC(double value){
return value-273.15;
}
public double KtR (double value){
return (value-273.15) * 4.0/5.0;
}
public double KtF(double value){
return (value-273.15) * 9.0/5.0 +32.0;
}
}