10.5 Designing and Implementing a Solar System

Now we are ready to build our solar system, which will consist of a sun and a collection of planets, with each planet defined to be some distance away from the sun. We will assume that the sun resides at the center of the solar system. We will implement the SolarSystem class in the same way as we have implemented the other classes seen so far: by providing a constructor that will be responsible for defining the instance variables and by defining appropriate accessor and mutator methods.

The complete SolarSystem class is shown in LISTING 10.8. Our constructor (lines 2–4) assumes that a basic SolarSystem object must have a Sun object at its center. As a consequence, the constructor expects to receive a Sun object as a parameter, but it assumes an empty collection of planets. We will implement the planet collection as a list.

Image

LISTING 10.8 The SolarSystem class

To add a Planet to the SolarSystem, we use a mutator method called addPlanet that can modify the collection of planets. This method (lines 6–7) receives a Planet object as a parameter and adds the object to the collection of planets. Because the collection is a list, we simply use the append method.

Finally, the simple accessor method called showPlanets (lines 9–11) shows all of the planets in the solar system. It iterates through the list of planets and prints each one in turn. Recall that the Planet class implements the _ _str_ _ method that returns the name of the planet for use in the print method.

To use the three classes we have implemented, we can save them as Python files. The classes Sun, Planet, and SolarSystem will be saved in the files sun.py, planetclass.py, and solarsystem.py, respectively. When we store a class in a file in this way, we have created a module that can be used by other programs—a module that is accessed and used in much the same way as the turtle, math, and cImage modules. To use our Planet, Sun, or SolarSystem classes, we simply import the module that contains the class definition.

SESSION 10.11 shows the statements that import the modules, and it creates a solar system with four planets. After importing the modules, we create a Sun object called sun and a SolarSystem object called ss using sun as its center. Next, we create a new Planet object p with a planet name of Mercury and add it as the first planet in the solar system. We then create and add three other planets in the same way. For simplicity, we reuse the object name p. Finally, we call the showPlanets method, which prints the names of the planets in our solar system. FIGURE 10.5 shows all of the objects and references present at this point.

Image

SESSION 10.11 Creating and showing a solar system with four planets

A figure shows a solar system with four planets. The figure shows the planet object ss whose state contains sun and planets. The references for the sun is marked with sun. The references for the planets are marked further with four planets Mercury, Jupiter, Earth, and Mars.

FIGURE 10.5 A solar system with four planets.

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

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