Python Programming in Context Chapter 10 Objectives To
Python Programming in Context Chapter 10
Objectives • To explore classes and objects further • To understand how to construct a class • To write constructors, accessor methods, and mutator 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 manipulate the solar system
Planet Class • Instance Data – Name – Mass – Distance from sun – radius
Listing 10. 1 class Planet: def __init__(self, iname, irad, im, idist): self. name = iname self. radius = irad self. mass = im self. distance = idist
Figure 10. 1
Types of Methods • Constructor – Used to make a new instance of the class • Accessor – Used to get information from an object – Get instance data • Mutator – Used to change something about an object – Change instance data
Listing 10. 2 def get. Name(self): return self. name def get. Radius(self): return self. radius def get. Mass(self): return self. mass def get. Distance(self): return self. distance
Listing 10. 3 def get. Volume(self): v = 4. 0/3 * math. pi * self. radius**3 return v def get. Surface. Area(self): sa = 4. 0 * math. pi * self. radius**2 return sa def get. Density(self): d = self. mass / self. get. Volume() return d
Listing 10. 4 def set. Name(self, newname): self. name = newname
Listing 10. 5 def show(self): print(self. name)
Listing 10. 6 def __str__(self): return self. name
Figure 10. 2
Namespaces • Namespaces and reference work as they did before • self is the name of the implicit parameter that always refers to the object itself • Never explicitly pass a value to the implicit parameter
Figure 10. 3
Figure 10. 4
The Sun Class • Instance Data – Name – Mass – Radius – Temperature
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 get. Mass(self): return self. mass def __str__(self): return self. name
Solar System Class • A sun • Many planets – Use a list to keep the collection of planets
Listing 10. 8 class Solar. System: def __init__(self, asun): self. thesun = asun self. planets = [] def add. Planet(self, aplanet): self. planets. append(aplanet) def show. Planets(self): for aplanet in self. planets: print(aplanet)
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
Listing 10. 9 class Solar. System: 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 add. Planet(self, aplanet): self. planets. append(aplanet) def add. Sun(self, asun): self. thesun = asun def show. Planets(self): for aplanet in self. planets: print(aplanet) def freeze(self): self. ssscreen. exitonclick()
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 get. XPos(self): return self. x def get. YPos(self): return self. y
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 = idist 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 get. XPos(self): return self. x def get. YPos(self): return self. y
Figure 10. 6
Figure 10. 7
Figure 10. 8
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
Listing 10. 13 def move. To(self, newx, newy): self. x = newx self. y = newy self. pturtle. goto(newx, newy) def get. XVel(self): return self. velx def get. YVel(self): return self. vely def set. XVel(self, newvx): self. velx = newvx def set. YVel(self, newvy): self. vely = newvy
Listing 10. 14 def move. Planets(self): G =. 1 dt =. 001 for p in self. planets: p. move. To(p. get. XPos() + dt * p. get. XVel(), p. get. YPos() + dt * p. get. YVel()) rx = self. thesun. get. XPos() - p. get. XPos() ry = self. thesun. get. YPos() - p. get. YPos() r = math. sqrt(rx**2 + ry**2) accx = G * self. thesun. get. Mass()*rx/r**3 accy = G * self. thesun. get. Mass()*ry/r**3 p. set. XVel(p. get. XVel() + dt * accx) p. set. YVel(p. get. YVel() + dt * accy)
Listing 10. 15 def create. SSand. Animate(): ss = Solar. System(2, 2) sun = Sun("SUN", 5000, 10, 5800) ss. add. Sun(sun) m = Planet("MERCURY", 19. 5, 1000, . 25, 0, 2, "blue") ss. add. Planet(m) m = Planet("EARTH", 47. 5, 5000, 0. 3, 0, 2. 0, "green") ss. add. Planet(m) m = Planet("MARS", 50, 9000, 0. 5, 0, 1. 63, "red") ss. add. Planet(m) m = Planet("JUPITER", 100, 49000, 0. 7, 0, 1, "black") ss. add. Planet(m) num. Time. Periods = 2000 for amove in range(num. Time. Periods): ss. move. Planets() ss. freeze() create. SSand. Animate()
Figure 10. 9
- Slides: 35