1) double fahrenheitToCelsius(double f) This function should convert the supplie
ID: 3541716 • Letter: 1
Question
1) double fahrenheitToCelsius(double f)
This function should convert the supplied Fahrenheit value into its Celsius equivalent. The formula for doing so is:
2) double kelvinToCelsius(double k)
This function should convert the supplied Kelvin value into its Celsius equivalent. The formula for doing so is:
3) double getTemperature(TemperatureScale_t scale) const
This function returns the internal representation of the temperature, stored in Celsius, to the requested temperature scale.
4) void addTemperature(double temperature, TemperatureScale_t scale)
This function adds the specified temperature, using the specified scale, to the current temperature.
5) void subtractTemperature(double temperature, TemperatureScale_t scale)
This function subtracts the specified temperature, using the specified scale, to the from the current temperature.
6) string toString(TemperatureScale_t output_scale = Celsius)
This function converts the Temperature into its string representation. The conversion to Celsius is already provided and can be used as a template for outputting as Fahrenheit and Kelvin.
Explanation / Answer
#include<iostream>
using namespace std;
enum TemperatureScale_t{Celsius,Fahrenheit, Kelvin};
class Temperature
{
private:
double temperature;
public:
Temperature(double p=0)
{
temperature = p;
}
double fahrenheitToCelsius(double f)
{
return ((5.0/9.0)*(f-32));
}
double kelvinToCelsius(double k)
{
return (k-273.15);
}
double getTemperature(TemperatureScale_t scale) const
{
if(scale==Celsius) return temperature; // temperature stored in celsius.
else if(scale == Fahrenheit) return (32+ 9*temperature/5);
else if(scale == Kelvin) return temperature+273.15;
}
void addTemperature(double temperature, TemperatureScale_t scale)
{
if(scale==Celsius)
this->temperature = this->temperature+temperature;
else if(scale == Fahrenheit)
this->temperature = this->temperature + (32+ 9*temperature/5);
else if(scale == Kelvin)
this->temperature = this->temperature+temperature+273.15;
}
void subtractTemperature(double temperature, TemperatureScale_t scale)
{
if(scale==Celsius)
this->temperature = this->temperature-temperature;
else if(scale == Fahrenheit)
this->temperature = this->temperature - (32+ 9*temperature/5);
else if(scale == Kelvin)
this->temperature = this->temperature-temperature+273.15;
}
string toString(TemperatureScale_t output_scale = Celsius)
{
if(output_scale==Celsius)
cout << this->temperature << Celsius << endl;
else if(output_scale == Fahrenheit)
cout << this->temperature << Fahrenheit << endl;
else if(output_scale == Kelvin)
cout << this->temperature << Kelvin << endl;
}
};
int main()
{
Temperature T1(234);
cout << T1.getTemperature(Kelvin);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.