In: Computer Science
PLEASE MAKE THIS TWO DIFFERENT FILES. ONE FOR CLASS (temperature.h) AND ONE FOR temperature.cpp
Objective: This assignment will provide further practice with implementing classes.
Task: For this homework, you will write a class called Temperature, in the files temperature.h and temperature.cpp, for creating and using objects that will store temperatures (like for weather forecasting programs).
This class should be portable, so it should work with any up-to-date C++ compiler.
Program Details and Requirements:
1) An object of type Temperature should represent a temperature in terms of degrees and scale. Degrees should allow decimal precision (so use type double). The three scales possible are Celsius, Fahrenheit, and Kelvin. You may store the scale however you like inside the object, but for any keyboard input or function parameters, scales will come in as type char, where 'C', 'F', 'K' (as well as the lower case versions) are valid options. Your object must always represent a valid temperature -- remember that 0 Kelvin represents the lowest possible temperature in existence (the complete absence of heat). Your object should also store a format setting, to be used for display of temperatures to the screen. There will be more than one possible format. The class features (public interface) should work exactly as specified, regardless of what program might be using Temperature objects.
Note: For purposes of conversions between scales, please remember the following conversion relationships betweeen temperatures:
2) Your Temperature class must provide the following services (i.e. member functions) in its public section. These functions will make up the interface of the Temperature class. Make sure you use function prototypes as specified here. (You may write any other private functions you feel necessary, but the public interface must include all the functionality described here).
Examples: These declarations should be legal, and the comment gives the initialized temperature
Temperature t1; // initializes to 0 Celsius Temperature t2(23.5, 'F'); // initializes to 23.5 Fahrenheit Temperature t3(12.6, 'Z'); // invalid scale, initializes to 0 Celsius Temperature t4(-300, 'c'); // this is below 0 Kelvin, inits to 0 Celsius Temperature t5(15, 'k'); // initializes to 15 Kelvin
Legal: 43.6 k , 53.5 C , 100 F , -273.15 c Illegal: 12.3 q , -5 K , -278 C , -500 F // last 3 are below absolute zero
You may assume that the user entry will always be of the
form:
D S where D is a numeric value and S is a
character
Name | Format | Example | Explanation |
---|---|---|---|
Default | D S | 50.4316 C | This will look mostly like the input from the Input
function. Print the degrees and scale as double and char, with default precision on the degrees, and the scale as an uppercase letter |
Precision-1 | D.d S | 50.4 C | Degrees printed to 1 place after the decimal, fixed format, and scale printed as an uppercase letter. This output will need to make sure to put the output stream BACK to its original format settings when you are done, (so that output from a main program isn't now set to 1 decimal place for the caller, for example). See this notes addendum for more details on this kind of thing |
Long | D scale | 50.4316 Celsius | This display format should show the degrees in default precision, and the scale as the full word "Celsius", "Fahrenheit", or "Kelvin" |
'D' = Default format 'P' = Precision-1 format 'L' = Long format
If an invalid setting code is passed in, do not alter the current format setting. This function should return true for successful format change, and false for failure (invalid setting given).
Temperature t1(68.9, 'F'); // 68.9 Fahrenheit t1.Convert('T'); // invalid scale, no change. Returns false t1.Convert('c'); // t1 is now 20.5 Celsius t1.Convert('K'); // t1 is now 293.65 Kelvin
Temperature t1(0, 'C'); // 0 Celsius Temperature t2(31.5, 'F'); // 31.5 Fahrenheit t1.Compare(t2); // returns 1 (since t2 comes first) t2.Compare(t1); // returns -1 (calling object is t2, comes first)
3) General Requirements:
Testing Your Class:
You will need to test your class, which means you will need to write one or more main programs that will call upon the functionality (i.e. the public member functions) of the class and exercise all of the different cases for each function. You do not need to turn any test programs in, but you should write them to verify your class' features.
Here is the beginning of a sample test program to get you started:
// sample.cpp -- sample test program starter for Temperature class ///////////////////////////////////////////////////////// #include <iostream> #include "temperature.h" using namespace std; int main() { Temperature t1; // should default to 0 Celsius Temperature t2(34.5, 'F'); // should init to 34.5 Fahrenheit // display dates to the screen cout << "\nTemperature t1 is: "; t1.Show(); cout << "\nTemperature t2 is: "; t2.Show(); t1.Input(); // Allow user to enter a temperature for t1 cout << "\nTemperature t1 is: "; t1.Show(); t1.SetFormat('L'); // change format of t1 to "Long" format cout << "\nTemperature t1 is: "; t1.Show(); // and so on. Add your own tests to fully test the class' // functionality. }
Temperature.h
//==============================================
#ifndef TEMPERATURE_H
#define TEMPERATURE_H
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class Temperature
{
private:
double degree;
char scale;
char format;
public:
Temperature();
Temperature(double deg, char sc);
void Input();
void Show() const;
bool Set(double deg, char s);
double GetDegrees() const;
char GetScale() const;
bool SetFormat(char f) ;
bool Convert(char sc) ;
int Compare(const Temperature& d) const;
};
#endif
//==============================================
Temperature.cpp
//==============================================
#ifndef TEMPERATURE_H
#include"temperature.h"
#endif
Temperature::Temperature()
{
degree = 0;
scale = 'C';
format = 'D';
}
Temperature::Temperature(double deg, char s)
{
format = 'D';
// check for valid scale input
if (!(s == 'F' || s == 'K' || s == 'C' || s == 'f' ||
s == 'k' || s == 'c'))
{
cout << "\nInvalid scale"
;
degree = 0.0;
scale = 'C';
}
else
{
// check if scale is
fehrenheit
if (s == 'F' || s=='f')
{
// conversion of
fehrenheit to celsius then celsius to kelvin
double in_kalvin
= ((deg - 32) * 5) / 9.0 + 273.15;
// if input is
not valid degree value
if (in_kalvin
< 0.0)
{
cout << "\nThis is below 0 Kelvin" ;
degree = 0;
scale = 'C';
}
else
{
degree = deg;
//comversion to capital letter
if (s >= 'a' && s <= 'z')
{
scale = s - 'a' + 'A';
}
else scale = s;
}
}
// check if scale is kelvin and
degree is invalid
// or scale is celsius and degree
is less than -273.155 which is less than 0 in kelvin
if (((s == 'K' || s=='k')
&& deg < 0) || ((s == 'C'|| s=='c') && deg <
-273.15))
{
cout <<
"\nThis is below 0 Kelvin";
degree =
0;
scale =
'C';
}
else
{
degree =
deg;
//comversion to
capital letter
if (s >= 'a'
&& s <= 'z')
{
scale = s - 'a' + 'A';
}
else scale =
s;
}
}
}
void Temperature::Input()
{
double deg;
char s;
bool flag = 1; // check for not valid input
while (flag)
{
cout << "\nPlease provide
temperature: ";
cin >> deg >> s;
// check for valid scale
input
if (!(s == 'F' || s == 'K' || s ==
'C' || s == 'f' || s == 'k' || s == 'c'))
{
cout <<
"Invalid scale in Temprature, please try again ";
}
else
{
// check if
scale is fehrenheit
if (s == 'F' ||
s == 'f')
{
// conversion of fehrenheit to celsius then
celsius to kelvin
double in_kalvin = ((deg - 32) * 5) / 9.0 +
273.15;
// if input is not valid degree value
if (in_kalvin < 0.0)
{
cout << "Invalid degree
in Temprature, please try again";
}
else
{
degree = deg;
//comversion to capital
letter
if (s >= 'a' && s
<= 'z')
{
scale = s
- 'a' + 'A';
}
else scale = s;
flag = 0;
}
}
// check if
scale is kelvin and degree is invalid
// or scale is
celsius and degree is less than -273.155 which is less than 0 in
kelvin
if (((s == 'K'
|| s == 'k') && deg < 0) || ((s == 'C' || s == 'c')
&& deg < -273.15))
{
cout << "Invalid degree in Temprature,
please try again";
}
else
{
degree = deg;
//comversion to capital letter
if (s >= 'a' && s <= 'z')
{
scale = s - 'a' + 'A';
}
else scale = s;
flag = 0;
}
}
}
}
// print temprature also tested getter member function
void Temperature::Show() const
{
if (format == 'D')
{
cout << GetDegrees() <<
" " << GetScale() ;
}
else if (format == 'L')
{
string sc; // creating
full name string
if (GetScale() == 'C') sc =
"Celsius";
else if (GetScale() == 'F') sc =
"Fahrenheit";
else sc = "Kelvin";
cout << GetDegrees() <<
" " << sc ;
}
else
{
cout <<
fixed<<setprecision(1) << GetDegrees() << " "
<< GetScale() ;
}
}
// set degree and scale if valid
bool Temperature::Set(double deg, char s)
{
// check for valid scale input
if (!(s == 'F' || s == 'K' || s == 'C' || s == 'f' ||
s == 'k' || s == 'c'))
{
return false;
}
else
{
// check if scale is
fehrenheit
if (s == 'F' || s == 'f')
{
// conversion of
fehrenheit to celsius then celsius to kelvin
double in_kalvin
= ((deg - 32) * 5) / 9.0 + 273.15;
// if input is
not valid degree value
if (in_kalvin
< 0.0)
{
return false;
}
else
{
degree = deg;
//comversion to capital letter
if (s >= 'a' && s <= 'z')
{
scale = s - 'a' + 'A';
}
else scale = s;
return true;
}
}
// check if scale is kelvin and
degree is invalid
// or scale is celsius and degree
is less than -273.155 which is less than 0 in kelvin
else if (((s == 'K' || s == 'k')
&& deg < 0) || ((s == 'C' || s == 'c') && deg
< -273.15))
{
return
false;
}
else
{
degree =
deg;
//comversion to
capital letter
if (s >= 'a'
&& s <= 'z')
{
scale = s - 'a' + 'A';
}
else scale =
s;
return
true;
}
}
}
// return degree of tempreture
double Temperature::GetDegrees() const
{
return degree;
}
// return scale of tempreture
char Temperature::GetScale() const
{
return scale;
}
// update format if input is valid format
bool Temperature::SetFormat(char f)
{
//check if valid formate is input
if (f == 'D' || f == 'P' || f == 'L')
{
format = f;
return true;
}
//check if valid formate is input in small letter if
allowed
/*
if (f == 'd' || f == 'p' || f == 'l')
{
format = f -'a' + 'A';
return false;
}
*/
// not valid input
return false;
}
bool Temperature::Convert(char sc)
{
char c_sc = sc; // convert to capital scale
if (sc >= 'a' && sc <= 'z') c_sc = sc -
'a' + 'A';
// check for valid scale input ( capital or small
letter)
if (!(c_sc == 'F' || c_sc == 'K' || c_sc == 'C'
))
{
return false;
}
// convert degree accordint to present scale and
required scale
if (c_sc == 'F' && scale == 'C')
{
degree = ((degree * 9) / 5.0) +
32;
}
else if (c_sc == 'F' && scale == 'K')
{
// first conversion to celsius then
converted to fehrenheit
degree = (((degree-273.15) * 9) /
5.0) + 32;
}
else if (c_sc == 'C' && scale == 'F')
{
degree = ((degree - 32) * 5) /
9.0;
}
else if (c_sc == 'C' && scale == 'K')
{
degree = degree - 273.15;
}
else if (c_sc == 'K' && scale == 'F')
{
// first converted to celsius than
converted to kelvin
degree = ((degree - 32) * 5) / 9.0
+ 273.15;
}
else if (c_sc == 'K' && scale == 'C')
{
degree = degree + 273.15;
}
//update scale and return true;
scale = c_sc;
return true;
}
// compare degree to calling object and input object
int Temperature::Compare(const Temperature& d) const
{
double degr_d = d.GetDegrees();
char scale_d = d.GetScale();
// if scale of d is not matching with calling
object
// convert degree to same scale locally;
if (scale == 'C' && scale_d == 'F')
{
degr_d = ((degr_d - 32) * 5) /
9.0;
}
else if (scale == 'C' && scale_d == 'K')
{
degr_d = degr_d - 273.15;
}
else if (scale == 'F' && scale_d == 'C')
{
degr_d = ((degr_d *9)/5.0) +
32;
}
else if (scale == 'F' && scale_d == 'K')
{
// first convert to celsius then to
fehrenhit
degr_d = (((degr_d - 273.15) * 9) /
5.0) + 32;
}
else if (scale == 'K' && scale_d == 'F')
{
// first convert to celsius then to
kelvin
degr_d = ((degr_d - 32) * 5) / 9.0
+ 273.15;
}
else if (scale == 'K' && scale_d == 'C')
{
degr_d = degr_d + 273.15;
}
// if calling object is having higher degree
if (degree > degr_d) return 1;
// if calling object is having less degree
if (degree < degr_d) return -1;
// both are having same tempraturee
return 0;
}
//=====================================================
main.cpp
//=====================================================
#include <iostream>
#include "temperature.h"
using namespace std;
int main()
{
Temperature
t1;
// should default to 0 Celsius
Temperature t2(34.5, 'F'); // should init to 34.5
Fahrenheit
Temperature t3(-5, 'K'); // invalid degree in
kalvin
Temperature t4(-274, 'C'); // invalid degree in
calsius
Temperature t5(10, 'T'); // invalid scale;
// display temprature to the screen
cout << "\nTemperature t1 is: ";
t1.Show();
cout << "\nTemperature t2 is: ";
t2.Show();
cout << "\nTemperature t3 is: ";
t3.Show();
cout << "\nTemperature t4 is: ";
t4.Show();
cout << "\nTemperature t5 is: ";
t5.Show();
t1.Input();
// Allow user to enter a temperature for t1
cout << "\nTemperature t1 is: ";
t1.Show();
t1.SetFormat('L');
// change format of t1 to "Long" format
cout << "\nTemperature t1 is: ";
t1.Show();
t2.SetFormat('P');
// change format of t2 to "presicion-1" format
cout << "\nTemperature t2 is: ";
t2.Show();
bool t3_set = t3.Set(45, 'c'); // set
temperatur of t3
if (t3_set)cout << "\nSuccessfully set
temperature of t3";
else cout << "\nUnsuccessfully to set
temperature of t3";
t3.Show();
cout << "\nTemperature t4 is: ";
bool t4_set = t4.Set(45, 't'); // failed to set
temperature
if (t4_set)cout << "\nSuccessfully set
temperature of t4";
else cout << "\nUnsuccessfully to set
temperature of t4";
t4.Show();
cout << "\nTemperature t5 is: ";
bool t5_convert = t5.Convert('K'); // convert t5 to
kelvin
if (t5_convert)cout << "\nSuccessfully convert
temperature of t5";
else cout << "\nUnsuccessfully to convert
temperature of t5";
cout << "\nTemperature t5 is: ";
t5.Show();
bool t4_convert = t4.Convert('p'); // failed
convert of t4
if (t4_convert)cout << "\nSuccessfully convert
temperature of t4";
else cout << "\nUnsuccessfully to convert
temperature of t4";
cout << "\nTemperature t4 is: ";
t4.Show();
int t1_t2 = t1.Compare(t2);
if (t1_t2 == 1) cout << "\nTemperature t1 is
greater than temperature t2";
else if (t1_t2 == -1 )cout << "\nTemperature t2
is greater than temperature t1";
else cout << "\nBoth temperature t1 and t2 are
equal";
// and so on. Add your own tests to fully test the
class'
// functionality.
cout << "\n";
system("pause");
return 0;
}
//=====================================================
output 1:case t1<t2
output 2: case t1 == t2
output 3: case t1> t2
Note: I have tried my best to test all member function.