Question
Hello, I am currently working on a program and I would like to know if it is done correctly. Thank you. project 8 Change the
Hello, I am currently working on a program and I would like to know if it is done correctly. Thank you.
project 8
Change the program to use classes for temperature, wind, and weather measurement.
C++ code:
// wind.h
#ifndef WIND_H_
#define WIND_H_
# include
using namespace std;
// wind_t class to hold data about wind and perform operations on wind
class wind_t{
private:
double speed;
string direction;
public:
wind_t();
wind_t(double,string);
void setWindSpeed(double);
void setWindDirection(string);
double getWindSpeed();
string getWindDirection();
void displayWind();
};
#endif /* WIND_H_ */
// end of wind.h
// wind.cpp - Implementation file of wind.h
# include "wind.h"
// default constructor
wind_t::wind_t()
{}
// parameterized constructor
wind_t::wind_t(double speed, string direction)
{
wind_t::speed = speed;
wind_t::direction = direction;
}
// setters
void wind_t::setWindDirection(string direction)
{
wind_t::direction = direction;
}
void wind_t::setWindSpeed(double speed)
{
wind_t::speed = speed;
}
// getters
double wind_t::getWindSpeed()
{
return speed;
}
string wind_t::getWindDirection()
{
return direction;
}
// display wind
void wind_t::displayWind()
{
cout << "Wind Direction " << direction << " ";
cout << "Wind Speed " << speed << " ";
}
// end of wind.cpp
// temperature.h
#ifndef TEMPERATURE_H_
#define TEMPERATURE_H_
# include
using namespace std;
// class to store details of temperature and provide operations on temperature object
class temperature_t{
private:
int temperature;
public:
temperature_t();
temperature_t(int);
void setTemperature(int);
int getTemperature();
void displayTemperature();
};
#endif /* TEMPERATURE_H_ */
// end of temperature.h
// temperature.cpp - Implementation file of temperature.h
# include "temperature.h"
// default constructor
temperature_t::temperature_t()
{}
//parameterized constructor
temperature_t::temperature_t(int temperature)
{
temperature_t::temperature = temperature;
}
// setters
void temperature_t::setTemperature(int temperature)
{
temperature_t::temperature = temperature;
}
// getters
int temperature_t::getTemperature()
{
return temperature;
}
// display temperature object
void temperature_t::displayTemperature()
{
cout << "Temperature " << temperature << " ";
}
// end of temperature.cpp
// weather.h
#ifndef WEATHER_H_
#define WEATHER_H_
# include
# include "wind.h"
# include "temperature.h"
using namespace std;
// class to store details of weather
class weather_t{
private:
wind_t wind;
temperature_t temperature;
public :
weather_t();
weather_t(double speed, string direction , int temperature);
void setWeatherWind(double speed, string direction);
void setWeatherTemperature(int temperature);
wind_t getWeatherWind();
temperature_t getWeatherTemperature();
void printWeather();
};
#endif /* WEATHER_H_ */
// end of weather.h
// weather.cpp - Implementation file of weather.h
#include "weather.h"
// default constructor
weather_t::weather_t()
{}
// parameterized constructor
weather_t::weather_t(double speed, string direction, int temp)
{
wind.setWindSpeed(speed);
wind.setWindDirection(direction);
temperature.setTemperature(temp);
}
// setters
void weather_t::setWeatherTemperature(int temperature)
{
weather_t::temperature.setTemperature(temperature);
}
void weather_t ::setWeatherWind(double speed, string direction)
{
wind.setWindDirection(direction);
wind.setWindSpeed(speed);
}
// getters
wind_t weather_t::getWeatherWind()
{
return wind;
}
temperature_t weather_t ::getWeatherTemperature()
{
return temperature;
}
// display weather object
void weather_t ::printWeather()
{
temperature.displayTemperature();
wind.displayWind();
cout<<" ";
}
// end of weather.cpp
// EdwardMagruderWeatherStation.cpp file to implement weather.h
# include "weather.h"
#include
# include
using namespace std;
string DisplayMenu(string station_name)
{
string str, temp;
do
{
cout << "*******************WEATHER STATION: " << station_name\
<< " *******************" << endl << endl;
cout << "I. Input a complete weather reading." << endl;
cout << "P. Print the current weather." << " ";
cout << "H. Print the weather history (from most recent to oldest)." << endl;
cout << "E. Exit the program." << " ";
cout << "Enter your choice: " << endl;
cin >> str;
temp = str;
for (std::string::size_type i = 0; i < str.length(); ++i)
temp[i] = toupper(str[i]);
str = temp;
} while (!(str == "I" || str == "P" || str == "H" || str == "E"));
return str;
}
double getWindSpeed()
{
double speed = -1;
string temp_string;
stringstream converter;
//this loop will be iterated continuously untill user enters windspeed which is greater than zero
do{
cout << "Enter Wind speed(>=0): ";
cin >> temp_string;
stringstream(temp_string) >> speed;
if (speed < 0)
cout << "Wind speed should be always greater or equal to 0(zero)";
}while (speed < 0);
return speed;
}
string getWindDirection()
{
string direction;
do{
cout << "Enter the Wind Direction (North,South,East,West): ";
cin >> direction;
for (std::string::size_type i = 0; i < direction.length(); ++i)
direction.at(i) = toupper(direction.at(i));
}while (!(direction == "NORTH" || direction == "SOUTH" || direction == "EAST" || direction =="WEST"));
return direction;
}
int getTemperature()
{
int temperature;
string temp_string;
stringstream converter;
cout << "Enter the temperature: ";
cin >> temp_string;
converter << temp_string;
converter >> temperature;
return temperature;
}
int main()
{
//Have the user provide a name for the weather station upon entry.
weather_t *history;
int historySize;
string station_name, input_choice;
int histCount = 0;
cout << "Enter the name of Weather Station: ";
getline(cin, station_name);
do {
cout << "Please enter how many records you want" << " ";
cin >> historySize;
if (historySize <= 0)
cout << "Input record count should always be greater than 0(zero)" << " ";
} while (historySize <= 0);
history = new weather_t[historySize];
while (1)
{
//Control loop to perform various actions
input_choice = DisplayMenu(station_name);
if (input_choice == "I")
{
if(histCount double speed = getWindSpeed(); string direction = getWindDirection(); int temp = getTemperature(); history[histCount].setWeatherTemperature(temp); history[histCount].setWeatherWind(speed,direction); histCount++; }else cout<<" Maximum records entered"; }else if(input_choice == "P") { cout << "*************Printing Current Weather*************" << endl; if (histCount > 0) { history[histCount-1].printWeather(); } else { cout << "no data has been entered" << " "; } }else if(input_choice == "H") { cout << "*************Printing Weather*************" << endl; if (histCount == 0) cout << "no data to output" << " "; int max = histCount > historySize ? historySize : histCount; for (int i = max-1; i >-1; i--) { history[i].printWeather(); } }else if (input_choice == "E") { break; } } return 0; } //end of main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started