11.5 Design

We begin by creating a basic design for our simulation based on the previous description. The first step is to identify those parts of the problem that correspond to objects. One of the easiest ways to start the process of object identification is to consider the prominent nouns that appear in the description of the problem. In this case, words such as bear, fish, and world seem like good choices. The nouns that we identify now will likely become Python classes when we implement the design.

Next, we need to analyze each noun and come up with a list of things that it should know and a list of actions that it can perform. The list of things that an object should know will become instance variables. The list of things that an object can do will become methods. This process helps us decide how objects will interact with one another. You may not identify all the instance variables and methods immediately, but you can always go back and add more later as you discover additional information about the problem.

The following lists provide a first attempt to identify the characteristic properties for the nouns previously identified:

  1. World

    • A world should know:

      • The maximum X and Y dimensions.

      • A collection of all life-forms present.

      • A grid with the locations of each specific life-form.

    • A world should be able to:

      • Return the dimensions of the world.

      • Add a new life-form at a specific location.

      • Delete a life-form wherever it is.

      • Move a life-form from one world location to another.

      • Check a world location to see if it is empty.

      • Return the life-form at a specific location.

      • Allow a life-form to live for one time unit.

      • Draw itself.

  2. Bear

    • A bear should know:

      • The world to which it belongs.

      • Its specific world location given as (x, y).

      • How long it has gone without food.

      • How long it has gone without breeding.

    • A bear should be able to:

      • Return its location, both the x and y values.

      • Set its location, both the x and y values.

      • Set the world to which it belongs.

      • Appear if it is newly born.

      • Hide if it dies.

      • Move from one location to another.

      • Live for one time unit (includes breeding, eating, moving, and dying).

  3. Fish

    • A fish should know:

      • The world to which it belongs.

      • Its specific location given as (x, y).

      • How long it has gone without breeding.

    • A fish should be able to:

      • Return its location, both the x and y values.

      • Set its location, both the x and y values.

      • Set the world that it belongs to.

      • Appear if it is newly born.

      • Hide if it dies.

      • Move from one location to another.

      • Live for one time unit (includes breeding, moving, and dying).

FIGURE 11.1 summarizes these results in a tabular form. Each table consists of the name of the class, a list of instance variables, and a list of methods. Names have been chosen that are consistent with the previous descriptions. Note that we have included an extra instance variable, a turtle, in each class. As we want this to be a graphical simulation, it will be necessary to include instances of the Turtle in any class that will have some type of drawing capability.

“A class diagram is shown for three classes World, Fish, and Bear.

FIGURE 11.1 Listing instance variables and methods for each class.

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

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