Ch10 OOP SolarSystem

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

Chapter 10

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

#other mutators follow the same pattern


Special Methods
• Called magic methods or dunder methods
__str__ (Listing 10.5)
def __str__(self):
return self.__name
• print function calls automatically when object is
printed

>>> myHome = Planet("Earth", 6371, 5.97e24,


152097701)
>>> print(myHome)
Earth
Logical View of the Planet Object (Figure
10.2)
Methods and self
• Namespaces and references work as they did
before.
• self is the name of the implicit parameter that
refers to the object itself.
• Never explicitly pass a value to the implicit
parameter.
Local Namespace for a Method
(Figure 10.3)
The Sun Class
• Instance Data
– Name
– Mass
– Radius
– Temperature
The Sun Class (Listing 10.7)
import math
class Sun:
def _ _init_ _(self, iName, iRad, iM, iTemp):
self.__name = iName
self.__radius = iRad
self.__mass = iM
self.__temp = iTemp
 
def getMass(self):
return self.__mass
 
def _ _str_ _(self):
return self.__name

#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")

#other methods as before

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)

© 2001–2019 Python Software Foundation. All Rights Reserved


Velocity in 2 Dimensions (Figure 10.7)
Using Similar Triangles to Computer
Acceleration Components (Figure 10.8)
Planets get 2 more instance variables (Listing
10.12)
class Planet:
def __init__(self, iName, iRad, iM, iDist, iVx,
iVy, iC):
 
#other instance variables as before
 
self.__velX = iVx
self.__velY = iVy
And more methods (Listing 10.13)
def moveTo(self, newX, newY):
self.__x = newX
self.__y = newY
self.__pTurtle.goto(self.__x, self.__y)
 
def getXVel(self):
return self.__velX
 
def getYVel(self):
return self.__velY
 
def setXVel(self, newVx):
self.__velX = newVx
 
def setYVel(self, newVy):
self.__velY = newVy
New SolarSystem Method
(Listing 10.14)
def movePlanets(self):
G = .1
dt = .001
 
for p in self.__planets:
p.moveTo(p.getXPos() + dt * p.getXVel(),
p.getYPos() + dt * p.getYVel())
 
rX = self.__theSun.getXPos() - p.getXPos()
rY = self.__theSun.getYPos() - p.getYPos()
r = math.sqrt(rX**2 + rY**2)
 
accX = G * self.__theSun.getMass() * rX/r**3
accY = G * self.__theSun.getMass() * rY/r**3
 
p.setXVel(p.getXVel() + dt * accX)
p.setYVel(p.getYVel() + dt * accY)
Animation (Listing 10.15)
def createSSandAnimate():
ss = SolarSystem(2, 2)
 
sun = Sun("Sun", 5000, 10, 5800)
ss.addSun(sun)
 
m = Planet("Mercury", 19.5, 1000, .25, 0, 2, "blue")
ss.addPlanet(m)
 
m = Planet("Earth", 47.5, 5000, 0.3, 0, 2.0, "green")
ss.addPlanet(m)
 
m = Planet("Mars", 50, 9000, 0.5, 0, 1.63, "red")
ss.addPlanet(m)
 
m = Planet("Jupiter", 100, 49000, 0.7, 0, 1, "black")
ss.addPlanet(m)
 
numTimePeriods = 2000
for aMove in range(numTimePeriods):
ss.movePlanets()
 
ss.freeze()
Running the Animation (Figure 10.9)

© 2001–2019 Python Software Foundation. All Rights Reserved.

You might also like