/
TempConvert.java
242 lines (238 loc) · 10.7 KB
/
TempConvert.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*************************************************************************
* Temperature Conversion Project for CMSC 255
**************************************************************************
* This is Project 4 for CMSC 255. The purpose is to convert a given
* temperature in a particular unit to the equivalent temperature of
* another user-chosen unit. The program outputs the converted
* equivalent temperature
*_________________________________________________________________________
* Gurumanpreet Singh
* Project Version 1.0, October 11th, 2019
* CMSC 255 Section 002
*************************************************************************/
import java.util.*;
public class TempConvert {
//Main method
public static void main(String[] args) {
processData();
}
public static void processData(){
String choice = "";
Scanner input = new Scanner(System.in);
/*
* The following line of code creates the while loop that will run until the user
* inputs "quit".
*/
while (!choice.equals("quit")){
/*
* The following lines of code ask for user input and store the input into a double.
* The double stored is named "temperature" (starting temperature).
*/
System.out.println("Enter the temperature to convert:");
double temperature = input.nextDouble();
input.nextLine();
/*
* The following lines of code ask for user input and store the input into a string.
* The string stored is named "current" (starting temperature unit).
*/
System.out.println("Enter the current scale of the temperature:");
String current = input.nextLine();
/*
* The following lines of code ask for user input and store the input into a string.
* The string stored is named "wanted" (the converted temperature unit).
*/
System.out.println("Enter the scale you want to convert the temperature to:");
String wanted = input.nextLine();
/*
* The following lines of code set the returned boolean value from the
* checkValidity method and store it in a boolean called "decision".
*/
boolean decision = checkValidity(temperature, current);
/*
* The following lines of code check if the checkValidity method returned
* true and perform actions based on that
*/
if (decision == true) {
/*
* The following lines of code convert a Fahrenheit temperature to Celsius
* The output temperature is rounded to 2 decimal places
*/
if (current.equalsIgnoreCase("Fahrenheit") && wanted.equalsIgnoreCase("Celsius")){
convertFahtoCel(temperature);
double converted = convertFahtoCel(temperature);
System.out.printf("%.2f", temperature);
System.out.print(" degrees " + current + " is ");
System.out.printf("%.2f", converted);
System.out.print(" degrees " + wanted);
System.out.println("");
}
/*
* The following lines of code convert a Fahrenheit temperature to Kelvin
* The output temperature is rounded to 2 decimal places
*/
else if (current.equalsIgnoreCase("Fahrenheit") && wanted.equalsIgnoreCase("Kelvin")){
convertFahtoKel(temperature);
double converted = convertFahtoKel(temperature);
System.out.printf("%.2f", temperature);
System.out.print(" degrees " + current + " is ");
System.out.printf("%.2f", converted);
System.out.print(" degrees " + wanted);
System.out.println("");
}
/*
* The following lines of code convert a Celsius temperature to Fahrenheit
* The output temperature is rounded to 2 decimal places
*/
else if (current.equalsIgnoreCase("Celsius") && wanted.equalsIgnoreCase("Fahrenheit")){
convertCeltoFah(temperature);
double converted = convertCeltoFah(temperature);
System.out.printf("%.2f", temperature);
System.out.print(" degrees " + current + " is ");
System.out.printf("%.2f", converted);
System.out.print(" degrees " + wanted);
System.out.println("");
}
/*
* The following lines of code convert a Celsius temperature to Kelvin
* The output temperature is rounded to 2 decimal places
*/
else if (current.equalsIgnoreCase("Celsius") && wanted.equalsIgnoreCase("Kelvin")){
convertCeltoKel(temperature);
double converted = convertCeltoKel(temperature);
System.out.printf("%.2f", temperature);
System.out.print(" degrees " + current + " is ");
System.out.printf("%.2f", converted);
System.out.print(" degrees " + wanted);
System.out.println("");
}
/*
* The following lines of code convert a Kelvin temperature to Celsius
* The output temperature is rounded to 2 decimal places
*/
else if (current.equalsIgnoreCase("Kelvin") && wanted.equalsIgnoreCase("Celsius")){
convertKeltoCel(temperature);
double converted = convertKeltoCel(temperature);
System.out.printf("%.2f", temperature);
System.out.print(" degrees " + current + " is ");
System.out.printf("%.2f", converted);
System.out.print(" degrees " + wanted);
System.out.println("");
}
/*
* The following lines of code convert a Kelvin temperature to Fahrenheit
* The output temperature is rounded to 2 decimal places
*/
else if (current.equalsIgnoreCase("Kelvin") && wanted.equalsIgnoreCase("Fahrenheit")){
convertKeltoFah(temperature);
double converted = convertKeltoFah(temperature);
System.out.printf("%.2f", temperature);
System.out.print(" degrees " + current + " is ");
System.out.printf("%.2f", converted);
System.out.print(" degrees " + wanted);
System.out.println("");
}
/*
* The following lines of code don't perform a conversion
* If the wanted unit matches the current temperature unit
* The output temperature is rounded to 2 decimal places
*/
else if (current.equalsIgnoreCase(wanted)){
System.out.printf("%.2f", temperature);
System.out.print(" degrees " + current + " is ");
System.out.printf("%.2f", temperature);
System.out.print(" degrees " + wanted);
System.out.println("");
}
}
/*
* The following lines of code are run when the user-inputted
* temperature fails to pass the checkValidity method
* (if the inputted temperature is out of the valid range
*/
else if(decision == false){
System.out.println(temperature + " degrees " + current + " is not a valid temperature");
}
/*
* The following lines of code ask if the user would like to
* convert another temperature. The program ends iff the user
* inputs "quit".
*/
System.out.println("Would you like to convert another temperature? Enter quit to exit.");
choice = input.nextLine();
}
}
/*
* The following checkValidity method checks to see if the user
* inputted temperature is in the valid range
* The method returns true if it is, false if it's not
*/
public static boolean checkValidity(double temp, String scale){
boolean value = false;
if (scale.equalsIgnoreCase("Fahrenheit")){
if (temp >= -459.40 && temp <= 1000){
value = true;
}
}
else if (scale.equalsIgnoreCase("Celsius")){
if (temp >= -273 && temp <= 1000) {
value = true;
}
}
else if (scale.equalsIgnoreCase("Kelvin")){
if (temp >= 0 && temp <= 1000){
value = true;
}
}
return value;
}
/*
* The following method converts Fahrenheit to Celsius
* The method returns the converted temperature
*/
public static double convertFahtoCel(double temp){
double newtemp = ((temp-32)*5/9);
return newtemp;
}
/*
* The following method converts Fahrenheit to Kelvin
* The method returns the converted temperature
*/
public static double convertFahtoKel(double temp){
double temporarytemp = ((temp-32)*5/9);
double newtemp = temporarytemp + 273;
return newtemp;
}
/*
* The following method converts Celsius to Fahrenheit
* The method returns the converted temperature
*/
public static double convertCeltoFah(double temp){
double newtemp = (temp * 9/5) + 32;
return newtemp;
}
/*
* The following method converts Celsius to Kelvin
* The method returns the converted temperature
*/
public static double convertCeltoKel (double temp){
double newtemp = temp + 273;
return newtemp;
}
/*
* The following method converts Kelvin to Celsius
* The method returns the converted temperature
*/
public static double convertKeltoCel(double temp){
double newtemp = temp - 273;
return newtemp;
}
/*
* The following method converts Kelvin to Fahrenheit
* The method returns the converted temperature
*/
public static double convertKeltoFah(double temp){
double temporarytemp = temp - 273;
double newtemp = (temporarytemp * 9/5) + 32;
return newtemp;
}
}