Can anyone help me with this C++ program? Farm Simulator Overview In this assign
ID: 3692354 • Letter: C
Question
Can anyone help me with this C++ program?
Farm Simulator
Overview
In this assignment you will simulate growing crops on a farm over a period of time. You will define a base class: Crop that is inherited by several different Crop derived classes: Cauliflower, Kale, Parsnip, and Strawberry. The base class defines attributes that are common to all Crop classes: the time it takes for a crop to grow, the name of the crop, the base value for the crop, and whether or not the crop will continue to grow after it has been harvested. Each derived class has a special attribute that contributes to the final sale cost for that type of crop which is defined in the Crop values section below.
The base Crop class will define a virtual function called harvest, takes in the current day in the simulation as a parameter and returns the value for the crop if it is ready to be harvested on that day. If the crop isn’t ready to be harvested, it returns 0.
Derived Crop classes
Each derived class from the base class Crop has a special attribute that is unique to that type of crop. The special attribute is used as a multiplier on the base value for the crop when the crop is ready to be harvested. The Parsnip class is defined below as an example:
class Parsnip : public Crop {
public:
Parsnip() : Crop("Parsnip", 2, 35, false) {
srand(time(NULL));
int randomNum = rand() % 100;
// create a random length multiplier between 1 and 2 (e.g., 1.75)
lengthMultiplier = static_cast<double>(randomNum) / 100 + 1;
}
virtual int harvest(int numDays) {
int value = Crop::harvest(numDays);
return value * lengthMultiplier;
}
private:
double lengthMultiplier;
};
class Kale : public Crop {
public:
private:
double crunchinessMultiplier;
};
class Strawberry : public Crop {
public:
private:
double juicinessMultiplier;
}
class Cauliflower : public Crop {
public:
private:
double sizeMultiplier;
};
Sample input
Your program will read in data from a text file. The file contains information about the crops your farm will be simulating. The user of the program will enter the filename from a prompt from your program.
The first line of the file will contain the number of crops in the file. Each subsequent line that follows will contain a character that represents a crop that your farm will be growing. The character in the file maps to the first letter of each crop that your farm grows (e.g., c maps to Cauliflower). Here is an example:
8
c
c
k
p
k
s
s
k
Crop values
Name: Cauliflower
Grow time: 6 days
Base value: 175
Regrows: Yes
Special attribute: Size
Name: Kale
Grow time: 4 days
Base value: 110
Regrows: No
Special attribute: Crunchiness
Name: Parsnip
Grow time: 2 days
Base value: 35
Regrows: No
Special attribute: Length
Name: Strawberry
Grow time: 3 days
Base value: 120
Regrows: Yes
Special attribute: Juiciness
Growing crops
After reading in the text file into a dynamic array of type Crop* (notice that it is an array of base class pointers—this will allow you to use polymorphism later), your program will begin the simulation for the farming period. Your program will prompt the user to enter the number of days, n, they would like to simulate.
After n days have been simulated, your program needs to calculate the yield of your farm. For each Crop class, the crop is ready to “harvest” when n equals the time it takes for that crop to grow. When a crop is harvested, it is also sold and your farm receives payment equal to the sale of that crop. If the crop is a type of crop that continues to grow after it has been harvested, it can be harvested multiple times if the simulation continues (i.e., Cauliflower takes 6 days to harvest so if our simulation runs for 24 days it can be harvested four times).
Sample output
Your output will need to contain detailed results of your farm simulation. For each day, you need to print out how many crops were harvested that day and how much they were sold for. If no crops were harvested, just print “No crops were harvested today.”
After the day-by-day breakdown, your program must also print the total yield for your farm over that period of time.
Here is output for a 6-day simulation using the input file from above:
Day 1: No crops harvested today.
Day 2: 1 crop(s) harvested for 35g.
Day 3: 2 crop(s) harvested for 240g.
Day 4: 3 crop(s) harvested for 220g.
Day 5: No crops harvested today.
Day 6: 4 crop(s) harvested for 600g.
Total yield: 1095g
NOTE: The actual values will change from run-to-run unless you seed your random number generate with the same number each time.
Coding requirements and assumptions
Your program must read in a text file—the name of the file will be determined by the user
Your program must use inheritance: implement a base class Crop, and 4 derived classes (the Parsnip implementation has been provided for you)
Your program must use polymorphism: create a dynamic array of the base class pointers, and call the harvest function from that array so the version from the derived classes get used
Explanation / Answer
#include <iostream>
using namespace std;
class crops
{
public:
void name(string n)
{
name=n;
}
void GrowTime(int g)
{
Growtime=g;
}
void BaseValue(int b)
{
Basevalue=b;
}
void ReGrow(int r)
{
ReGrow=r;
}
protected:
string name;
int GrowTime;
int BaseValue;
bool ReGrow;
public int harvest(string har)
{
if(crop is not grown)
{
return 1;
}
else
{
return 0;
}
}
};
class califlower:public crop
{
special attribute()
{
return size;
}
};
class Parsnip : public Crop {
public:
Parsnip() : Crop("Parsnip", 2, 35, false) {
srand(time(NULL));
int randomNum = rand() % 100;
// create a random length multiplier between 1 and 2 (e.g., 1.75)
lengthMultiplier = static_cast<double>(randomNum) / 100 + 1;
}
virtual int harvest(int numDays) {
int value = Crop::harvest(numDays);
return value * lengthMultiplier;
}
private:
double lengthMultiplier;
};
class Kale : public Crop {
public:
private:
double crunchinessMultiplier;
};
class Strawberry : public Crop {
public:
private:
double juicinessMultiplier;
};
int main(void)
{
Califlower cali;
string size;
cali.Name(califlower);
cali.GrowTime(6);
cali.BaseValue(175);
cail.ReGrows(yes);
size=cali.size();
cout<<"size"<<cali.size()<<end1;
Parsnip pars;
string size;
pars.Name(parship);
pars.GrowTime(2);
pars.BaseValue(35);
pars.ReGrows(no);
size=pars.size(length);
cout<<"size"<<cali.size()<<end1;
Kale kal;
string size;
kal.Name(kale);
kal.GrowTime(4);
kal.BaseValue(110);
kal.ReGrows(n0);
size=kal.size(chrunchess);
cout<<"size"<<kal.size()<<end1;
Strwberry strw;
string size;
strw.Name(Strawberry);
strw.GrowTime(3);
strw.BaseValue(120);
strw.ReGrows(yes);
size=strw.size();
cout<<"size"<<strw.size()<<end1;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.