Write a C++ Snowman object. You should create the following 3 separate files: Sn
ID: 3693825 • Letter: W
Question
Write a C++ Snowman object. You should create the following 3 separate files:
Snowman.h: the header file for the Snowman object and function prototypes;
Snowman.cpp: the implementation of the project functions; and
SnowmanDriver.cpp: a program to test your Snowman object.
The Snowman object will contain the following attributes:
height (double, in feet)
weight (double, in pounds)
temperature (double, in degrees Fahrenheit)
hasHat (bool)
scarfColor (string)
The Snowman object will contain the following data and member functions:
constructor with no arguments
assume
height = 5ft,
weight = 300lbs,
temperature = 29.80F,
hasHat = true,
scarfColor = “RED”
constructor with five arguments: height, weight, temperature, hasHat, scarfColor
separate mutator functions for setting the height, weight, temperature, hasHat, & scarfColor
separate accessor functions for getting the height, weight, temperature, hasHat, & scarfColor
function to calculate the volume of the Snowman.
Assume: PI = 3.14159
H = height
[edit]Here is the Snowman class declaration:
[edit]Driver
The SnowmanDriver.cpp program should:
declare an array of 100 Snowmen
Ask the user for the name of the data file
Open the data file
check to make sure the file opened successfully
if the file FAILED to open return to step 2
Skip the first 2 lines of the data file.
read the height, weight, temperature, hasHat and scarfColor for an unknown number of Snowmen, assume that there is one record per line.
you may assume there are fewer than 100 Snowmen
store each Snowman in the array of Snowmen
don’t forget to close the data file
sort the array of Snowmen by scarf color and temperature
Report the following:
Print a list of all Snowmen sorted by scarf color and temperature
Total number of Snowmen with hats
Total number of Snowmen with a RED scarf
Average volume of all Snowman
All amounts should be rounded to two decimal points.
[edit]Sample Input Data
[edit]Sample Output
Explanation / Answer
Snowman.h:
#include <iostream>
using namespace std;
class Snowman
{
double height;
double weight;
double temperature;
bool hasHat;
string scarfColor;
public:
Snowman();
Snowman(double, double, double, bool, string);
void setHeight(double);
void setWeight(double);
void setTemperature(double);
void setHasHat(bool);
void setScarfColor(string);
double getHeight();
double getWeight();
double getTemperature();
bool getHasHat();
string getScarfColor();
double calcVolume();
};
Snowman.cpp:
#include "Snowman.h"
Snowman::Snowman()
{
height = 5;
weight = 300;
temperature = 29.80;
hasHat = true;
scarfColor = "RED";
}
Snowman::Snowman(double height, double weight, double temp, bool hasHat, string scarfColor)
{
this->height = height;
this->weight = weight;
temperature = temp;
this->hasHat = hasHat;
this->scarfColor = scarfColor;
}
void Snowman::setHeight(double height)
{
this->height = height;
}
void Snowman::setWeight(double weight)
{
this->weight = weight;
}
void Snowman::setTemperature(double temp)
{
temperature = temp;
}
void Snowman::setHasHat(bool hasHat)
{
this->hasHat = hasHat;
}
void Snowman::setScarfColor(string scarfColor)
{
this->scarfColor = scarfColor;
}
double Snowman::getHeight()
{
return height;
}
double Snowman::getWeight()
{
return weight;
}
double Snowman::getTemperature()
{
return temperature;
}
bool Snowman::getHasHat()
{
return hasHat;
}
string Snowman::getScarfColor()
{
return scarfColor;
}
double Snowman::calcVolume()
{
return 3.14159 * height * height * height / 36;
}
SnowmanDriver.cpp:
#include "Snowman.cpp"
#include <fstream>
void sort(Snowman snowmen[], int count)
{
for(int i = 0; i < count - 1; i++) //For each element.
{
int min = i; //Assumes the first element in the remaining array as min.
for(int j = i; j < count; j++) //Find the position of least element in remaining array.
if(snowmen[j].getScarfColor() < snowmen[min].getScarfColor())
min = j;
Snowman temp = snowmen[i]; //Exchange the first element in the array, with minimum element.
snowmen[i] = snowmen[min];
snowmen[min] = temp;
}
}
int main()
{
//declare an array of 100 Snowmen
Snowman snowmen[100];
//Ask the user for the name of the data file
string fileName;
cout<<"Enter the name of the data file: ";
cin>>fileName;
//Open the data file
ifstream fin;
fin.open(fileName);
//check to make sure the file opened successfully
//if the file FAILED to open return to step 2
while(fin.fail())
{
cout<<"Unable to open the file. Try again."<<endl;
cout<<"Enter the name of the data file: ";
cin>>fileName;
fin.open(fileName);
}
//Skip the first 2 lines of the data file.
string temp;
getline(fin, temp);
getline(fin, temp);
int count = 0;
//read the height, weight, temperature, hasHat and scarfColor for an unknown number of Snowmen, assume that there is one record per line.
//you may assume there are fewer than 100 Snowmen
//store each Snowman in the array of Snowmen
while(!fin.eof())
{
double height, weight, temp;
bool hasHat;
string scarfColor;
fin>>height>>weight>>temp>>hasHat>>scarfColor;
snowmen[count++] = Snowman(height, weight, temp, hasHat, scarfColor);
}
//don’t forget to close the data file
fin.close();
//sort the array of Snowmen by scarf color and temperature
sort(snowmen, count);
//Report the following:
//Print a list of all Snowmen sorted by scarf color and temperature
int hatsCount = 0;
int redScarfCount = 0;
double sum = 0;
for(int i = 0; i < count; i++)
{
cout<<snowmen[i].getHeight()<<" ";
cout<<snowmen[i].getWeight()<<" ";
cout<<snowmen[i].getTemperature()<<" ";
cout<<snowmen[i].getHasHat()<<" ";
cout<<snowmen[i].getScarfColor()<<endl;
if(snowmen[i].getHasHat())
hatsCount++;
if(snowmen[i].getScarfColor() == "RED")
redScarfCount++;
sum += snowmen[i].calcVolume();
}
//Total number of Snowmen with hats
cout<<hatsCount<<" Snowmen have a hat."<<endl;
//Total number of Snowmen with a RED scarf
cout<<redScarfCount<<" Snowmen have a RED scarf."<<endl;
//Average volume of all Snowman
cout<<"The average volume of snowman is "<<sum / count<<endl;
//All amounts should be rounded to two decimal points.
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.