## This is a combination of Listings 10.9 (with a bug fix), 10.10, and ## 10.11 from Python Programming in Context by Miller and Ranum. ## Individual methods from Listings 10.2-10.7 have also been inserted. ## Finally, a createSS() function is based on Session 10.12. import cTurtle import math ## Listing 10.9 class SolarSystem: def __init__(self, width, height): self.thesun = None self.planets = [] self.ssturtle = cTurtle.Turtle() self.ssturtle.hideturtle() self.ssturtle.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) # bug fixed: Listing 10.9 missed the parentheses def freeze(self): self.ssturtle.exitOnClick() def getMassOfSun(self): return self.thesun.getMass() ## 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 = cTurtle.Turtle() self.sturtle.shape("circle") self.sturtle.color("yellow") def getMass(self): return self.mass def __str__(self): return self.name def getXPos(self): return self.x def getYPos(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 = cTurtle.Turtle() self.pturtle.color(self.color) self.pturtle.shape("circle") self.pturtle.up() self.pturtle.goto(self.x,self.y) self.pturtle.down() def getName(self): return self.name def getRadius(self): return self.radius def getMass(self): return self.mass def getDistance(self): return self.distance def getVolume(self): v = 4.0/3 * math.pi * self.radius**3 return v def getSurfaceArea(self): sa = 4.0 * math.pi * self.radius**2 return sa def getDensity(self): d = self.mass / self.getVolume() return d def setName(self, newname): self.name = newname def show(self): print(self.name) def __str__(self): return self.name def getXPos(self): return self.x def getYPos(self): return self.y def createSS(): ss = SolarSystem(2,2) ss.addSun(Sun("SUN",5000,10,5800)) ss.addPlanet(Planet("MERCURY",19.5,1000,.25,'blue')) ss.addPlanet(Planet("EARTH",47.5,5000,.3,'green')) ss.addPlanet(Planet("MARS",50,9000,.5,'red')) ss.addPlanet(Planet("JUPITER",100,49000,.7,'black')) return ss