-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathTemperature.java
153 lines (129 loc) · 4.5 KB
/
Temperature.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
/**
* @author Aditya Mahajan <aditya.mahajan@mcgill.ca>
* @version 2013.10.06
* Program for ECSE 321, Assignment 2, Fall 2013
*/
/**
* The {@code Temperature} class allows a user to convert temperature from on
* unit to another.
*
* Sample usage
* <pre><code>
* Temperature averageHighInOctober = new Temperature (15, Temperature.CELSIUS);
* Temperature converted = new Temperature(averageHighInOctober);
* converted.changeUnits(Temperature.FAHRENHEIT);
* System.out.println(
* String.format(
* "The average temperature of Montreal in October 2012 was %s (%s)",
* averageHighInOctober.toString(),
* converted.toString()));
* </code></pre>
*/
public class Temperature {
/** Enumerator for different temperature units */
public static enum Units { FAHRENHEIT, CELSIUS, KELVIN }
private final double valueInKelvin;
private Units units;
/**
* Create a new {@code Temperature} with given attributes
* @param value numerical value of {@code Temperature}
* @param units {@code Units} of {@code Temperature}
*/
public Temperature (double value, Temperature.Units units) {
this.units = units;
valueInKelvin = convertToKelvin(value);
}
/**
* Create a copy of existing {@code Temperature} object
* @param temperature an existing {@code Temperature} object
*/
public Temperature (Temperature temperature) {
this.valueInKelvin = temperature.valueInKelvin;
this.units = temperature.units;
}
/** Convert a {@code Temperature} value from {@code Units} to Kelvin
* @param value numerical value of Temperature
*/
protected double convertToKelvin(double value) {
double convertedValue;
switch (units) {
case KELVIN: convertedValue = value;
break;
case CELSIUS: convertedValue = value + 273.15;
break;
case FAHRENHEIT: convertedValue = (value + 459.67) * 5.0/9.0;
break;
default: throw new IllegalArgumentException();
}
return convertedValue;
}
/** Convert a {@code Temperature} value from Kelvin to {@code Units}
* @param value numerical value of Temperature
*/
protected double convertFromKelvin(double value) {
double convertedValue;
switch (units) {
case KELVIN: convertedValue = value;
break;
case CELSIUS: convertedValue = value - 273.15;
break;
case FAHRENHEIT: convertedValue = value * 9.0/5.0 - 459.67;
break;
default: throw new IllegalArgumentException();
}
return convertedValue;
}
/** Format {@code Units}
*/
protected String unitsToString() {
String formattedUnits;
switch (units) {
case KELVIN: formattedUnits = "K";
break;
case CELSIUS: formattedUnits = "°C";
break;
case FAHRENHEIT: formattedUnits = "°F";
break;
default: throw new IllegalArgumentException();
}
return formattedUnits;
}
/**
* Get the value of the {@code Temperature}
* @return numerical value of the {@code Temperature} in the current {@code Units}
*/
public double getValue() {
return convertFromKelvin(valueInKelvin);
}
/**
* Get the current {@code Units} of the {@code Temperature}
* @return current {@code Units} of {@code Temperature}
*/
public Units getUnits() {
return units;
}
/**
* Change the current {@code Units} of the {@code Temperature}.
* Changing the {@code Units} also changes the numerical value
* in a consistent manner.
* @param units the new {@code Units}
*/
public void changeUnits(Units units) {
this.units = units;
}
/**
* Convert the {@code Temperature} to {@code String}. The output is
* as follows
* <pre><code>
* Temperature temperature = new Temperature(0, Temperature.Units.CELSIUS);
* System.out.println(temperature.toString()); // prints "0 °C"
* temperature.changeUnits(Temperature.Units.FAHRENHEIT);
* System.out.println(temperature.toString()); // prints "32 °F"
* temperature.changeUnits(Temperature.Units.KELVIN);
* System.out.println(temperature.toString()); // prints "273.15 K"
* </code></pre>
*/
public String toString() {
return getValue() + " " + unitsToString();
}
}