11.9 Summary

In this chapter, we designed and implemented a large, multiclass graphical simulation. First, we considered a description of the problem. We picked out the important subjects (nouns) and began to implement classes, one for each. Then, to design a class, we described those things that instances of a class would need to know about themselves (the instance variables) and those things that instances of a class would need to be able to do (the methods). After creating each class, we implemented a function that established many objects, and we let each object perform methods as defined by the initial rules of the simulation. By using random numbers, we were able to provide a degree of uncertainty so that no two runs of our simulation would look the same.

Key Terms

  • computer simulation

  • inheritance

  • noun

  • objects

  • predator–prey relationships

Python Keywords and Functions

  • isinstance

Programming Exercises

  1. 11.1   Modify the simulation so that a World consists of two kinds of locations, water and land. Fish and plants can live only in the water, and bears can go in the water but only to eat fish.

  2. 11.2   Modify the simulation to give bears an energy instance variable. Each time a bear eats, its energy level increases. Bears should lose energy as they move and breed. If the energy level drops to zero, the bear dies.

  3. 11.3   Add a Berry class so that the bears have another food source. This class should behave much like the Plant class.

  4. 11.4   Choose another real-world predator–prey ecosystem and implement a simulation.

Design Credits: Calculator Icon made by Smashicons from www.flaticon.com

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.31.24