Environment Inheritance

Class Relation UML

 

class uml diagram

 

NOTE: Make sure to know what is public versus private (habitat and hunger and public attributes so that the sub-classes can access them). plantGrow function is private since we don’t want outside forces adding vegitation; it should only be controlled by the environment itself

 

Class Descriptions

class Plant

– Constructor takes a single positional argument, sustinence, which is the amount of hunger it satiates. It should initialize the associated attribute.

– getSustinence is the accessor method for the attribute

– getName returns the hard-coded value ”Plant”

– timePasses the plant grows so the sustiness level should go up by one

– a String representation should be in the format: (\ <sustinece level>/)

 

class Animal

– Constructor takes two parameters, a name of the animal (required, no default value), and the environment object in which it lives - default to None. size should be set to 1 and hunger to 0 (i.e. small baby and not hungry just yet)

– getSize is the accessor for the size

– getName is the accessor for the name

– eat – should just subtract one from the hunger, (or set to zero - should not go negative)

– timePasses – the animal’s hunger should grow by it’s size (the larger it is the faster it gets hungry). If the animal’s hunger was at 0 when timePassed was called it should grow larger. If the animal’s hunger is at 10, it should print out that the animal died and return False (False meaning it did not survive), otherwise return True.

– a String representation should be in the format: <name>(size:<size>) has a <hunger> hunger level

 

class Carnivore extends Animal

– override the eat function. It should check the animal’s habitat to see if it is not the only animal. If it is it cannot eat so it return’s None. If it is not the only animal in the habitat it should get a random animal that is not itself and eat it. Hunger should go down by the eaten animal’s size (again do not go negative), and return the animal that was chosen to be eaten.

 

class Herbivore extends Animal

– override the eat function. It should check the animal’s habitat to see if there is any plant-life.  If there isn’t it cannot eat so it return’s None.  If there is plants  it should get a random plant and eat it. Hunger should go down by the eaten plant’s sustinence level (again do not go negative), and return the plant that was chosen to be eaten.

 

class Environment

– The constructor should take no parameters, two lists should be initialized to be empty and then 5 plants should be grown to start the environment (use private function to help grow plants)

– plantGrow creates a plant object with a random sustinece level from 1-5 and adds it to the vegitation list.

– addAnimal adds the provided animal to the list

– getVegitationAmount returns the size of the vegitation list

– getPopulation returns the size of the animal list

– getRandomPlant returns a random plant from the vegitation list

– getRandomAnimal returns a random animal from the animals list

 

– timePasses All animals must first eat, keep track of the items that were eaten. (To make things simplier you can allow multiple animals to eat the same thing). After everything has eaten, remove the animals/plants that were eaten, and print a message showing what has died. Then pass time for all remaining animals and plants, if something has starved make sure to remove them as well. Then because animals produce fertilizer create a number of new plants equal to the number of remaining animals.

– a String representation should be in the format: <number of animals> animals,<number of plants> plants, followed by the list of each plant and each animal

Need a custom answer at your budget?

This assignment has been answered 6 times in private sessions.

© 2024 Codify Tutor. All rights reserved