Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

lab 10 code.(using this code we need to complete lab 13)(if needed) #include #include #include using namespace std; void plant(char, int, int,char[][10]); char harvest(int, int,char[][10])

lab 10 code.(using this code we need to complete lab 13)(if needed)

#include

#include

#include

using namespace std;

void plant(char, int, int,char[][10]);

char harvest(int, int,char[][10]) ;

void wait(char[][10]) ;

void countVectors(vector, vector);

string report(char[][10]) ;

const char Rows=5;

const char Columns=10;

int main() {

vectorharvest_pro;

vectorwaste;

char land[Rows][Columns]={

{'0','0','0','0','0','0','0','0','0','0'},

{'0','0','0','0','0','0','0','0','0','0'},

{'0','0','0','0','0','0','0','0','0','0'},

{'0','0','0','0','0','0','0','0','0','0'},

{'0','0','0','0','0','0','0','0','0','0'}};

plant('P',3,5,land);

plant('p',4,5,land);

plant('B',1,1,land); //planted Bean at 1,1

plant('R',0,6,land);

plant('p',2,0,land);

plant('r',4,3,land);

plant('R',4,2,land);

for(int i=0;i<5;i++){

for(int j=0;j<10;j++){

cout<

}

cout<<" ";

}

cout<

char har= harvest(1,1,land); //harvested Bean

harvest_pro.push_back(har); //adds harvested Bean to vector

cout<

for(int i=0;i<5;i++){

for(int j=0;j<10;j++){

cout<

}

cout<<" ";

}

cout<

wait(land);

for(int i=0;i<5;i++){

for(int j=0;j<10;j++){

cout<

}

cout<<" ";

}

cout<

string corp=report(land);

cout<

for(int i=0;i

for(int j=0;j

char result = harvest(i,j,land);

if(result=='X'){

waste.push_back(result);

}

else if(result!='0'){

harvest_pro.push_back(result);

}

}

}

countVectors(harvest_pro, waste);

return 0;

}

void plant(char A,int row,int column,char farm [][10] ){

if(farm[row][column]=='0'){

farm[row][column]=A;

}

else{

cout<<"Something is already planted there"<

}

cout<

}

void countVectors(vector harvest_pro, vector waste){

int potatoCount = 0; int beanCount = 0; int riceCount = 0;

cout << waste.size() << " crops were wasted!" << endl;

for(int i=0;i

if(harvest_pro.at(i) == 'P'){

potatoCount++;

}

if(harvest_pro.at(i) =='B'){

beanCount++;

}

if(harvest_pro.at(i)=='R'){

riceCount++;

}

}

cout << potatoCount << " potatoes were harvested!" << endl;

cout << beanCount << " beans were harvested!" << endl;

cout << riceCount << " rice crops were harvested!" << endl;

}

void wait(char farm[][10]){

for(int i=0;i<5;i++){

for(int j=0;j<10;j++){

if(farm[i][j]!='0'){

if(islower(farm[i][j])){

farm[i][j]=(toupper (farm[i][j]));

}

else if(isupper(farm[i][j])){

farm[i][j]='X';

}

}

}

}

}

char harvest(int row, int column,char farm[][10]){

char plant='0';

plant= farm[row][column];

farm[row][column]='0';

return plant;

}

string report(char farm[][10]){

string result=" ";

for(int i=0;i<5;i++){

for(int j=0;j<10;j++){

result=result+farm[i][j]+",";

}

result+=' ';

}

return result;

}

QUESTION:

a class that represents the farm object from labs 10.a class for a robot that manages the tending of a farm.

Farmclass should have four private fields:

int rows int columns int age char **plot//will represent the farm crops and have dimension rows X columns

Farmclass should also have eight public methods (functions) that match each of the functions from lab 10:

bool plant(char, int, int) - plants a crop at a particular location, but only if there is no plant in that location. Returns true if a plant was planted and false if not. (note that this is an adjustment to this function)

char harvest(int, int) - picks a plant at a particular location. The character of the plant is returned and at that location on the farm a '0' is placed. If there is no plant at that location a '0' is returned. A spoiled crop ('X') counts as a plant.

void wait() - this method lets time pass. Every crop in the array that is lowercase becomes uppercase. Every crop in the array that is uppercase becomes an 'X'. The private field age should be incremented.

string report() - the farm generates a string that represents itself. Each plant should be represented by its character separated by a comma and each new row should be separated by a newline character (' ')

int getAge() - return the value of the age of the farm (int age)

int getRows() - returns the number of rows

int getColumns() - returns the number of columns

char at(int, int) const - returns the value of the farm at a particular location in the plot without altering what is there.

Notice that each function has almost the same signature as those from lab 10, with the notable difference being that the array of characters representing the farm no longer needs to be passed in as a parameter because it is now a private member variable with each instance of the class possessing its own copy.

Because Farm is a class, it will need a constructor. In fact, we will give it two constructors. The default constructor will form a Farm object that is 10 X 10. The second constructor form a Farm object of any size, determined by the number of rows and columns passed to it as parameters. Both constructors should initialize the age of the farm to 0 and form a 2-dimensional array of the appropriate size.

Because the Farm dynamically allocates a two-dimensional array of variable size using a double pointer, you will also need to form a destructor that deallocates the memory used for that member variable.

Robotclass should have the following private static fields:

static int riceCount; static int beanCount; static int potatoCount; static int wasteCount; static int robotCount;

The variables are shared by all objects of the class (because of the static keyword).

TheRobotclass should have the following private member fields:

vector produce; int seedVault;

Each instance (object) of the robot class will have its own copy of the private member variables.

The Robot class should also have the following public methods:

void plant(Farm &f, char c) - plants a crop at all spaces in the farm where there is an empty plot using the character passed in as parameter as the plant of choice. So if the farm is something similar to:

0, 0, X, X,

p, R, b, 0,

0, 0, 0, 0

And you call my_bot.plant(my_farm, 'r'); The new plot should look like this:

r, r, X, X,

p, R, b, r,

r,r,r,r,

You should use the farm's plant() function to plant a single plant (from Lab 10) inside this new plant function to save yourself some work. Each time the robot successfully plants a plant, you should increment the static variable (riceCount, beanCount, or potatoCount) that matches what you have successfully planted.You should also decrement the private member variable seedVault. You should only call the farm's plant function and the related incrementing and decrementing if the robot's seedVault attribute is > 0.

void harvest(Farm &f) - picks all plants that are ready to be eaten (those that are capital but not 0 or X). The character of the plants should be added to the robot's private member variable vector produce. Note that this is an adjustment from lab 11. At any location on the farm where a plant is harvested a '0' is placed. If there is no plant ready to be harvested at a location nothing needs to be done for that cell. You can use your original harvest function to harvest each individual plant inside this harvest function to save yourself some work. For example, the farm:

0, 0, X, X,

p, R, b, B,

0, 0, P, 0

should become-->

0, 0, X, X,

p, 0, b, 0,

0, 0, 0, 0

void clean(Farm &f) - removes all X's from the farm and replaces the X's with a zero. The function should increment the static wasteCount variable each time a plant is successfully removed. So for instance, the farm:

0, 0, X, X,

p, R, b, 0,

0, 0, 0, 0

Should become-->

0, 0, 0, 0,

p, R, b, 0,

0, 0, 0, 0

and increment wasteCount twice for having removed two dead plants. You can use your harvest function from the previous lab to make the code simpler to write.

static int getRiceCount() - returns the riceCount

static int getBeanCount() - returns the beanCount

static int getPotatoCount() - returns the potatoCount

static int getWasteCount() - returns the wasteCount

static int getRobotCount() - returns the robotCount

int getSeedVaultCount() const - returns seedVault

int getProduceCount() const - returns how many plants have been harvested by a particular robot. This is not a sum of the static rice/bean/potato count but rather the size of the produce member variable for a particular robot.

void addSeeds(int) - adds the parameter passed in to the seedVault member variable.

Your Robot class should have two private methods:

void bubble_sort() - takes any collection of plants and sorts them into alphabetical order using the bubble sort algorithm. Please consult your textbook for guidance on writing this.

void insertion_sort() - takes any collection of plants and sorts them into alphabetical order using the insertion sort algorithm. Please consult your textbook for guidance on writing this.

Your Robot class should have a constructor. Inside the constructor, the private static robotCount variable should be incremented.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions