In: Computer Science
Mr. Kent doesn't care about almost anything ... but himself and his money. So, when his power plant leaked radioactive goo that caused several species of wildlife to go extinct, he was only concerned with the public perception as it might affect his income and possible jail time.
Many rumors surfaced around the Springfield Nuclear Power Plant. One of them is high concern over the mutation rate of the rare Springfield molted platypus. With barely more than 500 left in the wild, the word "extinction" has been tossed around. So, to quell the media, Mr. Kent had 30 of them captured, dissected, and analyzed to check for signs of mutation. He found that the mutation rate is 2% each month, but when they do mutate, they become sterile and cannot reproduce. With this information, he wants to create one of those newfangled computer simulations that the press loves so much. That's where you come in!
Specifications:
In this assignment, you will create a class called Animal_Category.
Your Animal_Category class is to contain the following data members:
Your Animal_Category class is to contain the following member functions:
You will also create a class called Platypus. Below, we will describe what will define a platypus. You will also create a main function in which you will create objects of type platypus to test the functionality of your new user-defined type.
Your Platypus class is to contain the following data members:
Member functions:
Further, the platypus has a chance of becoming dead each time it ages. This chance is ten times the platypus' weight. A 5 pound platypus has a 50% chance of death. A 10 pound platypus (or heavier) has a 100% chance of death. Again here update the value of the corresponding data member when needed.
Think very carefully about writing the above functions and how they should be used. There are indeed circumstances when some functions should not execute. For example, a dead platypus shouldn't eat anything.
Your program should fully test your platypus class. It must call every member function in the platypus class. It must print to the screen what it is doing and show the changes that appear when the member functions are called. The fight function will require two platypuses: one to call the fight function and one to be a parameter in the fight function.
c++ language
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
static int platypus_count=0;
class Animal_Category {
private:
string appearance;
bool nurse;
bool cold_blooded;
bool live_on_land;
bool live_in_water;
public:
Animal_Category(string tappearance, bool tnurse, bool
tcold_blooded, bool tlive_on_land, bool tlive_in_water)
{
this->appearance =
tappearance;
this->nurse = tnurse;
this->cold_blooded =
tcold_blooded;
this->live_on_land =
tlive_on_land;
this->live_in_water =
tlive_in_water;
}
void print() {
cout << left <<
setw(15) << "Appearance :" << right << setw(15)
<< this->appearance << endl;
if(this->nurse)
cout << left <<
setw(15) << "Nurse :" << right << setw(15)
<< "YES" << endl;
else
cout << left <<
setw(15) << "Nurse :" << right << setw(15)
<< "NO" << endl;
if(this->cold_blooded)
cout << left <<
setw(15) << "Cold Blooded :" << right << setw(15)
<< "YES" << endl;
else
cout << left <<
setw(15) << "Cold Blooded :" << right << setw(15)
<< "NO" << endl;
if(this->live_on_land)
cout << left <<
setw(15) << "Live on Land :" << right << setw(15)
<< "YES"<< endl;
else
cout << left <<
setw(15) << "Live on Land :" << right << setw(15)
<< "NO" << endl;
if(this->live_in_water)
cout << left <<
setw(15) << "Live in Water :" << right <<
setw(15) << "YES" << endl;
else
cout << left << setw(15) << "Live in
Water :" << right << setw(15) << "NO" <<
endl;
}
};
class Platypus {
private:
float weight;
int age;
string name;
char gender;
bool alive;
bool mutant;
Animal_Category *animal_type;
const int mutation_rate = 2;
public:
Platypus() {
this->weight = 0.0;
this->age = 0;
this->name = "";
this->gender =' ';
this->alive = false;
this->mutant = false;
this->animal_type = NULL;
platypus_count++;
}
Platypus(float tweight,int tage,string tname,char
tgender)
{
this->weight = tweight;
this->age = tage;
this->name = tname;
this->gender = tgender;
this->alive = true;
this-> mutant= false;
this->animal_type = new
Animal_Category("fur", true, false, true, true);
platypus_count++;
}
void print()
{
cout << left <<
setw(15) << "Weight :" << right << setw(15)
<< this->weight << endl;
cout << left <<
setw(15) << "Age :" << right << setw(15) <<
this->age << endl;
cout << left <<
setw(15) << "Name :" << right << setw(15)
<< this->name << endl;
if(this->gender=='f')
cout << left <<
setw(15) << "Gender :" << right << setw(15)
<< "FEMALE" << endl;
else if (this->gender ==
'm')
cout << left << setw(15) << "Gender
:" << right << setw(15) << "MALE" <<
endl;
if(this->alive)
cout << left <<
setw(15) << "Alive :" << right << setw(15)
<< "YES" << endl;
else
cout << left << setw(15) << "Alive
:" << right << setw(15) << "NO" <<
endl;
this->animal_type->print();
cout << endl;
}
void age_me() {
if (alive)
{
this->age =
age + 1;
if
(age*mutation_rate == 100)
this->mutant = true;
if (10 *
this->weight >= 100)
alive = false;
}
}
Platypus fight(Platypus other)
{
if ((this->alive) &&
(other.alive))
{
int fight_val =
rand() % (101);
if
(((this->weight / other.weight) * 50) >= fight_val)
return (*this);
else
return (other);
}
cout << "Not a valid fight"
<< endl;
return (*this);
}
void eat()
{
if (this->alive)
{
float random =
((float)rand() / (float)RAND_MAX);
float r = random
* 4.9;
this->weight
+= ((r / 100)*this->weight);
}
else
{
cout <<
"Platypus can't eat" << endl;
}
}
friend Platypus hatch(Platypus parent);
};
Platypus hatch(Platypus parent)
{
if (parent.mutant)
{
char gen;
int m_f = rand() % 2;
if (m_f == 0)gen = 'f';
else gen = 'm';
float random = ((float)rand() /
(float)RAND_MAX);
float r = random * 0.9;
float weight = 0.1 + r;
return(Platypus(weight, 0, "plipo",
gen));
}
return (Platypus());
}
int main()
{
Platypus p1(1.5, 48, "Arin", 'f');
p1.print();
p1.age_me();
p1.age_me();
p1.print();
Platypus p2 = hatch(p1);
p2.print();
p2.fight(p1).print();
Platypus p3(10, 49, "Eric", 'm');
p3.print();
p3.age_me();
p3.print();
p3.fight(p1).print();
p1.eat();
p1.print();
p3.eat();
p3.print();
return 1;
}
OUTPUT:
Weight : 1.5
Age : 48
Name : Arin
Gender : FEMALE
Alive : YES
Appearance : fur
Nurse : YES
Cold Blooded : NO
Live on Land : YES
Live in Water : YES
Weight : 1.5
Age : 50
Name : Arin
Gender : FEMALE
Alive : YES
Appearance : fur
Nurse : YES
Cold Blooded : NO
Live on Land : YES
Live in Water : YES
Weight : 0.607227
Age : 0
Name : plipo
Gender : MALE
Alive : YES
Appearance : fur
Nurse : YES
Cold Blooded : NO
Live on Land : YES
Live in Water : YES
Weight : 1.5
Age : 50
Name : Arin
Gender : FEMALE
Alive : YES
Appearance : fur
Nurse : YES
Cold Blooded : NO
Live on Land : YES
Live in Water : YES
Weight : 10
Age : 49
Name : Eric
Gender : MALE
Alive : YES
Appearance : fur
Nurse : YES
Cold Blooded : NO
Live on Land : YES
Live in Water : YES
Weight : 10
Age : 50
Name : Eric
Gender : MALE
Alive : NO
Appearance : fur
Nurse : YES
Cold Blooded : NO
Live on Land : YES
Live in Water : YES
Not a valid fight
Weight : 10
Age : 50
Name : Eric
Gender : MALE
Alive : NO
Appearance : fur
Nurse : YES
Cold Blooded : NO
Live on Land : YES
Live in Water : YES
Weight : 1.55944
Age : 50
Name : Arin
Gender : FEMALE
Alive : YES
Appearance : fur
Nurse : YES
Cold Blooded : NO
Live on Land : YES
Live in Water : YES
Platypus can't eat
Weight : 10
Age : 50
Name : Eric
Gender : MALE
Alive : NO
Appearance : fur
Nurse : YES
Cold Blooded : NO
Live on Land : YES
Live in Water : YES