Note: Plesae put comment and explain how you go about it Thanks. Create a Temper
ID: 3765256 • Letter: N
Question
Note: Plesae put comment and explain how you go about it Thanks.
Create a Temperature class that internally stores a temperature in degrees Kelvin. Create functions named setTempKelvin, setTempFarenheit, and setTemCelcius that an input in the specified scale, converts the temperature to Kelvin, and store the temperature in the class member variable. Also create functions that return and stored temperature in Kelvin, Fahrenheit, or Celsius. Write the main functions to test your class. Use the equations:
Kelvin = Celsius + 273.15
Celsius = (5.0/9) X (Fahrenheit -32.0)
Explanation / Answer
Using the above two equatins we dervie other equation for converting kelvin to celsius etc. See the convertion function the below class
package current;
//Kelvin = Celsius + 273.15
// Celsius = (5.0/9) X (Fahrenheit -32.0)
public class Temperature {
public static void main(String[] args) {
Temperature tempCl = new Temperature();
double temp = 273.15;
tempCl.setTemKelvin(temp);
System.out.println("Input = "+temp+" Kelvin");
System.out.println("Kelvin : "+tempCl.getTemKelvin()+" Celsius: "+tempCl.getTemCelcius()+" Farenheit: "+tempCl.getTemFarenheit());
tempCl.setTemCelcius(temp);
System.out.println("Input = "+temp+" Celsius");
System.out.println("Kelvin : "+tempCl.getTemKelvin()+" Celsius: "+tempCl.getTemCelcius()+" Farenheit: "+tempCl.getTemFarenheit());
tempCl.setTemFarenheit(temp);
System.out.println("Input = "+temp+" Farenheit");
System.out.println("Kelvin : "+tempCl.getTemKelvin()+" Celsius: "+tempCl.getTemCelcius()+" Farenheit: "+tempCl.getTemFarenheit());
}
public static int TEMP_KELVIN = 1;
public static int TEMP_FARENHEIT = 2;
public static int TEMP_CELCIUS = 3;
private double temKelvin;
private double temFarenheit;
private double temCelcius;
private double convertToCelcius(double temp, int type) {
// Celsius = Kelvin - 273.15
double reqTemp = 0;
if(type == TEMP_KELVIN) {
reqTemp = temp - 273.15;
} else if(type == TEMP_FARENHEIT) {
//// Celsius = (5.0/9) X (Fahrenheit -32.0)
reqTemp = (temp-32) * 5.0/9.0;
}
return reqTemp;
}
private double convertToFarenheit(double temp, int type) {
// Celsius = Kelvin - 273.15
double reqTemp = 0;
if(type == TEMP_KELVIN) {
double celcius = temp - 273.15;
reqTemp = (temp * 9.0/5.0) + 32;
} else if(type == TEMP_CELCIUS) {
//Celsius = (5.0/9) X (Fahrenheit -32.0)
//Fahrenheit = ( Celsius * 9.0/5)+32;
reqTemp = (temp * 9.0/5.0) + 32;
}
return reqTemp;
}
private double convertToKelvin(double temp, int type) {
// Celsius = Kelvin - 273.15
// kelvin = celsius+273.15
double reqTemp = 0;
if(type == TEMP_CELCIUS) {
reqTemp = temp + 273.15;
} else if(type == TEMP_FARENHEIT) {
//convert to celsius first and then do convertion to kelvin
reqTemp = convertToCelcius(temp, TEMP_FARENHEIT)+ 273.15;
}
return reqTemp;
}
public double getTemKelvin() {
return temKelvin;
}
public void setTemKelvin(double temKelvin) {
this.temKelvin = temKelvin;
temCelcius = convertToCelcius(temKelvin, TEMP_KELVIN);
temFarenheit = convertToFarenheit(temKelvin, TEMP_KELVIN);
}
public double getTemFarenheit() {
return temFarenheit;
}
public void setTemFarenheit(double temFarenheit) {
this.temFarenheit = temFarenheit;
temCelcius = convertToCelcius(temFarenheit, TEMP_FARENHEIT);
temKelvin = convertToKelvin(temFarenheit, TEMP_FARENHEIT);
}
public double getTemCelcius() {
return temCelcius;
}
public void setTemCelcius(double temCelcius) {
this.temCelcius = temCelcius;
temFarenheit = convertToFarenheit(temCelcius, TEMP_CELCIUS);
temKelvin = convertToKelvin(temCelcius, TEMP_CELCIUS);
}
}
--------------output-------------
Input = 273.15 Kelvin
Kelvin : 273.15 Celsius: 0.0 Farenheit: 523.67
Input = 273.15 Celsius
Kelvin : 546.3 Celsius: 273.15 Farenheit: 523.67
Input = 273.15 Farenheit
Kelvin : 407.1222222222222 Celsius: 133.97222222222223 Farenheit: 273.15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.