Assignment 10: Inheritance

Due by: Friday, December 1, 2023 at 11:59 p.m.

Didn't you think a lot of the code we had to write in the bear and fish simulation was redundant? This project gives you the opportunity to simplify the simulation through the power of recursion.

You can download an updated version of this simulation here:

A few changes have been made to this version of this simulation. These are:

  • A Plant class has been added, with the color green and a shape of a circle.
  • The Fish class has been changed to have the color blue so that it is more distinct from Plant objects.
  • Most importantly, a Creature class has been added as the parent class of Bear, Fish, and Plant.
  • Some changes have already been made to refactor code that was the same (or nearly the same) in Bear and Fish, moving that code to Creature.

Specification

Your job is to make the following improvements to the simulation. Unlike previous assignments, you don't have to write a lot of code, but you do have to read a lot of code. This kind of work is similar to the real world, where you will encounter large bodies of existing code and will need to make small changes to add functionality or to remove bugs.

Improving tryToEat()

Examine the tryToBreed() method in the Creature class. Note that the class is passed in as a parameter so that it knows what kind of new creature to create when breeding.

Make a similar modification by abstracting tryToEat() out of the Bear and Fish classes and into the Creature class. Test your modification to make sure it still works as before.

Enhancing World

Add three additional instance variables World class: bearCount, fishCount, and plantCount. (Note that these are separate from the variables with the same names that are used to start the simulation running.) Add get and set methods for each instance variable. Make sure that the addThing() and deleteThing() methods in the World class modify these variables appropriately.

Simplifying Offsets

The offsets variable appears, and is redundantly defined, in several methods of the three creature classes. Move offsets so that it is an instance variable of the Creature class and is initialized in the constructor.

Improved Thresholds

Remove all of the hardcoded thresholds for starvation, breeding, and overcrowding and make them instance variables of their respective classes. Have these set in the constructor of each class.

Random Breeding and Starving

Modify the constructors so that each creature you instantiate has a random time to breed and a random time to starve. Experiment with the range of random values until the distribution makes sense when you run the simulation.

Random Living

Modify the simulation so that live() is called for each creature every single time unit. However, each creature's live() method should also include a parameter that represents a probability. The actions in the live() method should only be performed with the given probability.

Hint: You will use random.random() to decide whether or not the actions will be performed. Think about how you can use a random number in the range [0, 1) so that this probability is adhered to.

Turn In

Upload assignment10.py to Blackboard.

All work must be submitted before Friday, December 1, 2023 at 11:59 p.m. unless you are going to use a grace day.

All work must be done individually. You may discuss general concepts with your classmates, but it is never acceptable for you to look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.

Grading

Your grade will be determined by the following weights:

Category Weight
Improving tryToEat() 10%
Enhancing World 15%
Simplifying Offsets 15%
Improved Thresholds 20%
Random Breeding and Starving 20%
Random Living 20%

Under no circumstances should any student look at the code written by another student. Tools will be used to detect code similarity automatically.