Ch10 OOP SolarSystem
Ch10 OOP SolarSystem
Ch10 OOP SolarSystem
Planet Objects
Objectives
• To explore classes and objects further
• To understand how to construct a class
• To write constructors, accessor methods,
mutator methods, and special methods
• To understand the concept of self
• To explore instance data
• To implement a graphical simulation using
objects
Object Oriented Programming
• Objects are instances of Classes
• Objects can perform methods
• Instance Data
– What an object knows about itself
• Methods
– What an object can do
Turtle objects
• Instance Data
– Color
– Heading
– Tail position
• Methods
– forward
– backward
– up
Astronomy
• Design classes to represent planets, sun,
moons, etc.
• Use these classes to write programs that
simulate the solar system
Planet Class
• Instance Data
– Name
– Mass
– Distance from sun
– Radius
Types of Methods
• Constructor
– Used to make a new instance of the class
– Named _ _init_ _
• Accessor = “Getters”
– Used to get information from an object
– Get instance data
• Mutator = “Setters”
– Used to change something about an object
– Change instance data
Syntax
class Classname:
def method1():
...
def method2():
...
...
self
• Special reference to the object on which the
method is called
• Always the first parameter to a class method
• Also used as object name to call other methods
in the class
Hiding Instance Variables
• The class should manage the instance
variables (encapsulation).
• So instance variables should not be accessed
by code outside the class.
• Prefacing an instance variable name with __(2
underscores) hides the name (Python
“mangles” the name).
A Constructor (Listing 10.2)
class Planet:
def _ _init_ _(self, iName, iRad, iM, iDist):
self._ _name = iName
self._ _radius = iRad
self._ _mass = iM
self._ _distance = iDist
Calling the Constructor
>>> myPlanet = Planet("X25", 45, 198, 1000)
Accessors (Listing 10.3)
def getName(self):
return self._ _name
def getRadius(self):
return self._ _radius
def getMass(self):
return self._ _mass
def getDistance(self):
return self._ _distance
More Accessors (Listing 10.3)
def getVolume(self):
import math
v = 4/3 * math.pi * self._ _radius**3
return v
def getSurfaceArea(self):
import math
sa = 4 * math.pi * self._ _radius**2
return sa
def getDensity(self):
d = self._ _mass / self.getVolume()
return d
Mutators (Listing 10.4)
def setName(self, newName):
self._ _name = newname
#more methods
Solar System Class
• A sun
• Many planets
– Use a list to keep the collection of planets
The SolarSystem Class (Listing 10.8)
class SolarSystem:
def _ _init_ _(self, aSun):
self._ _theSun = aSun
self._ _planets = []
def addPlanet(self, aPlanet):
self._ _planets.append(aPlanet)
def showPlanets(self):
for aPlanet in self._ _planets:
print(aPlanet)
SolarSystem with Four Planets
(Figure 10.5)
Visualize and Animate
• Use a turtle to draw the sun and the planets
• Animate the solar system by moving the turtle
• Need to develop a simple understanding of
planetary movement
– Velocity
– Acceleration
– Mass
New SolarSystem (Listing 10.9)
class SolarSystem:
def _ _init_ _(self, width, height):
self.__theSun = None
self.__planets = []
self.__ssTurtle = turtle.Turtle()
self.__ssTurtle.hideturtle()
self.__ssScreen = turtle.Screen()
self.__ssScreen.setworldcoordinates(-width/2.0, -height/2.0,
width/2.0, height/2.0)
def addPlanet(self, aPlanet):
self.__planets.append(aPlanet)
def addSun(self, aSun):
self.__theSun = aSun
def showPlanets(self):
for aPlanet in self.__planets:
print(aPlanet)
def freeze(self):
self.__ssScreen.exitonclick()
New Sun (Listing 10.10)
class Sun:
def __init__(self, iname, irad, im, itemp):
self.name = iname
self.radius = irad
self.mass = im
self.temp = itemp
self.x = 0
self.y = 0
self.sturtle = turtle.Turtle()
self.sturtle.shape("circle")
self.sturtle.color("yellow")
def getXPos(self):
return self.x
def getYPos(self):
return self.y
New Planet (Listing 10.11)
class Planet:
def _ _init_ _(self, iName, iRad, iM, iDist, iC):
self.__name = iName
self.__radius = iRad
self.__mass = iM
self.__distance = iDist
self.__x = self.__distance
self.__y = 0
self.__color = iC
self.__pTurtle = turtle.Turtle()
self.__pTurtle.color(self.__color)
self.__pTurtle.shape("circle")
self.__pTurtle.up()
self.__pTurtle.goto(self.__x, self.__y)
self.__pTurtle.down()
#other methods as before
def getXPos(self):
return self.__x
def getYPos(self):
return self.__y
Visualizing Sun and Planets
(Figure 10.6)