Answer to Question #110707 in C++ for mohsmef

Question #110707
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. Incidentally, this kind of main is called a driver, since it is used soley to test something (new). In that driver, you will create enough objects of your new type(s) in order to adequately test their functions. We leave it up to you to do this properly.
1
Expert's answer
2020-04-20T15:11:52-0400
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <time.h>


using namespace std;


class Platypus {
    public :
        /*
         * default constructor that creates a dead platypus
         * */
        Platypus(){
            srand(time(0));
            weight = 0.0;
            age = 0;
            name = "-";
            gender = '-';
            alive = false;
            mutant = false;
        }
        /*
         * constructor that you can pass values to so as to establish
         * its gender, weight, age, and name; it will default to alive and not mutant
         * */
        Platypus(char gender, float weight, short age, string name){
            srand(time(0));
            this->weight = weight;
            this->age = age;
            this->name = name;
            this->gender = gender;
            alive = true;
            mutant = false;
        }
        /*
         *  output to the screen the attributes of that platypus
         * */
        void print(){
             string gen = (gender == 'm') ? "male" : "female";
             printf("\nPlatypus %s is %s %d years old,\n",name.c_str(),gen.c_str(),age);
             string life = (alive) ? "steel alive" : "already dead";
             string mutantS = (mutant) ? "with mutation" : "without mutation";
             printf("has %f pounds and %s %s \n",weight,life.c_str(),mutantS.c_str());
        }
        /*
         *   increments the object's age.
         *   It will also include a 2% chance become a mutant.
         *   Further, the platypus has a chance of becoming dead each time it ages.
         *   This chance is ten times the platypus' weight.
         * */
        void age_me(){
            if (alive == false)
                return ;
            age++;
            _generateMutation();
            _generateDeath();
        }
        /*
         *  function that accepts another platypus object as a parameter.
         *  It will have the calling platypus attack the other (passed in) platypus.
         *  (calling_platypus_weight/other_platypus_weight) * 50 < random value from 1 to 100
         *  if it is true, the calling platypus survives; otherwise the other platypus survives.
         * */
        void fight(Platypus * target){
            if (alive == false)
                return ;
           float fightRatio = (weight / target->getWeight()) * 50.0f;


           if (fightRatio > static_cast<float>(_grade()))
               target->die();
           else
               die();
        }


        /*
         *  function that increases the weight of the platypus by a random amount
         *  from 0.1% to 5.0% of the platypus' current weight
         * */
        void eat(){
            if (alive == false)
                return ;
            float weightAmount = (5.0f * _grade()) / 100.0f;
            weightAmount = (weightAmount < 0.1f) ? 0.1f : weightAmount;
            weight += weight * weightAmount;
        }
        /*
         *  A hatch function that will randomly set up a newborn platypus
            with alive=true, mutant=false, and age=0.
            Gender will randomly be 'm' or 'f' with equal probability.
            Weight will randomly be between 0.1 and 1.0 pounds.
         * */
        Platypus * hatch(){printf("**\n");
            if (alive == false)
                return NULL;
            char gender = ' ';
            string name= "";
            if (_grade() > 50){
                gender = 'f';
                name = nameF[_grade() % nameSize];
            } else {
                gender = 'm';
                name = nameM[_grade() % nameSize];
            }
            float weight= static_cast<float>(_grade()) / 100.0f;
            weight = (weight < 0.1f) ? 0.1f : weight;
            short age= 0;


            return (new Platypus(gender, weight, age, name));
        }


        void die(){
            alive = false;
        }
        float getWeight() const{
            return weight;
        }
        ~Platypus(){}
    private:
        float weight;
        short age;
        string name;
        char gender;
        bool alive;
        bool mutant;
        const static int nameSize = 5;
        const string nameM[nameSize]={"Bob","Jack","Clif","Rob","Bill"};
        const string nameF[nameSize]={"Anna","Nanna","Ganna","Hanna","Sveta"};
        /*
         *  weight * 10 = x% chance to die
         * */
        void _generateDeath(){
            if (_grade() <= (weight * 10))
                alive = false;
        }
        /*
         *  2% chance to became a mutant
         * */
        void _generateMutation(){
            if (_grade() <= 2)
                mutant = true;
        }
        /*
         *  create random grade from 1 to 100
         * */
        int _grade(){
            return ( rand() % 100 + 1 );
        }


};


void eatTest(Platypus * p){
    printf("-----------EAT TEST-------------\n");
    p->print();
    p->eat();
    printf("- - - - - - - - - - - - - - - - \n");
    p->print();
    printf("--------------------------------\n");
}
void ageTest(Platypus * p){
    printf("------------AGE TEST------------\n");
    p->print();
    p->age_me();
    printf("- - - - - - - - - - - - - - - - \n");
    p->print();
    printf("--------------------------------\n");
}


void fightTest(Platypus * p1, Platypus * p2){
    printf("-----------FIGHT TEST-----------\n");
    p1->print();
    p2->print();
    p1->fight(p2);
    printf("- - - - - - - - - - - - - - - - \n");
    p1->print();
    p2->print();
    printf("--------------------------------\n");
}
int main(int argc, char *argv[])
{


    Platypus * root = new Platypus('f', 0.3f, 1, "Eva");
    ageTest(root);
    Platypus * childA = root->hatch();
    eatTest(childA);
    Platypus * childB = root->hatch();
    eatTest(childB);


    fightTest(childA, childB);




    delete root;
    delete childA;
    delete childB;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS