Python Programming: An Introduction to Computer Science
9.6.EXERCISES 153 Writea programthatsimulatesmultiplegamesofblackjackandestimatestheprobabilitythatthedealer willbust. Ablackja ...
154 CHAPTER9. SIMULATIONANDDESIGN ...
Chapter 10 Defining Classes Inthelastthreechapters,wehave developedtechniquesforstructuringthecomputationsofa program.In thenext ...
156 CHAPTER10. DEFININGCLASSES 10.2 ExampleProgram:Cannonball. Beforelaunchingintoa detaileddiscussionofhow towriteyourownclasse ...
10.2. EXAMPLEPROGRAM:CANNONBALL 157 h0 = input("Enterthe initial height (in meters): ") time = input("Enterthe time interval bet ...
158 CHAPTER10. DEFININGCLASSES Thesituationfortheverticalcomponentis slightlymorecomplicated,sincegravitycausesthey-velocity toc ...
10.3. DEFININGNEWCLASSES 159 10.2.3 ModularizingtheProgram Youmayhave noticedduringthedesigndiscussionthatI employedstepwiserefi ...
160 CHAPTER10. DEFININGCLASSES 10.3.1 Example:Multi-SidedDice Youknow thata normaldie(thesingularofdice)is a cubeandeachfaceshow ...
10.3. DEFININGNEWCLASSES 161 def roll(self): self.value= randrange(1,self.sides+1) def getValue(self): return self.value def set ...
162 CHAPTER10. DEFININGCLASSES OK,soselfis a parameterthatrepresentsanobject.Butwhatexactlycanwedowithit?Themain thingtoremember ...
10.3. DEFININGNEWCLASSES 163 theta = math.pi* angle / 180.0 self.xvel = velocity* math.cos(theta) self.yvel = velocity* math.sin ...
164 CHAPTER10. DEFININGCLASSES a = input("Enterthe launch angle (in degrees): ") v = input("Enterthe initial velocity (in meters ...
10.4. OBJECTSANDENCAPSULATION 165 inotherprograms.Indoingso,it wouldbea goodideatoadddocumentationthatdescribeshowtheclass canbe ...
166 CHAPTER10. DEFININGCLASSES "Returns the x position(distance) of this projectile." return self.xpos Youmightnoticethatmany of ...
10.5. WIDGETOBJECTS 167 Figure10.3:Snapshotofdicerollerinaction. themousewasclickedaftertheclickhasalreadycompleted. Nevertheles ...
168 CHAPTER10. DEFININGCLASSES AsI mentionedabove, inorderforthiscodetowork,ourconstructorwillhave toinitializeself.label asanap ...
10.5. WIDGETOBJECTS 169 """A button is a labeledrectangle in a window. It is activatedor deactivated with the activate() and dea ...
170 CHAPTER10. DEFININGCLASSES thesizeofthedieasparameters. setValueChangetheview toshow a givenvalue.Thevaluetodisplaywillbepas ...
10.5. WIDGETOBJECTS 171 pip.setOutline(self.background) pip.draw(self.win) return pip def setValue(self,value): "Set this die to ...
172 CHAPTER10. DEFININGCLASSES Pythontoindicatethata methodis “private”totheclassandnotintendedforusebyoutsideprograms. 10.5.4 T ...
«
4
5
6
7
8
9
10
11
12
13
»
Free download pdf