LAB 9

Exercise 1 The Dice object was described in class in the following way: # # dice.py # Class definition for a multi-sided dice. # from random import randrange class Dice: def __init__(self, sides): self.sides = sides self.value = 1 def roll(self): self.value = randrange(1, self.sides+1) def getValue(self): return self.value def setValue(self, value): self.value = value def Main(): d = Dice(6) d.roll() print("Value: ", d.getValue()) if __name__ == '__main__': Main() Save the Dice program in cs177/lab09/dice.py Then write a dice game program that rolls the dice with you. Here is an example of the input/output: Press <Enter to roll the dice ... You got 6 and 6 Press <Enter for my turn to roll the dice ... I got 2 and 5 You win!!! Do you want to play again (yes/no)? yes Press <Enter to roll the dice ... You got 2 and 1 Press <Enter for my turn to roll the dice ... I got 1 and 6 I win!!! Do you want to play again (yes/no)? yes Press <Enter to roll the dice ... You got 5 and 4 Press <Enter for my turn to roll the dice ... I got 6 and 3 We are even!! Do you want to play again (yes/no)? no Save the dice game program in cs177/lab09/dicegame.py Exercise 2 Write a class to represent the geometric solid sphere. Your class should implement the following methods: __init__(self,radius) Creates a sphere with the given radius. getRadius(self) Returns the radius of this sphere. surfaceArea(self) Returns the surface area of the sphere. volume(self) Returns the volume of the sphere. Save the sphere class in cs177/lab09/sphere.py Then write the following program sphereMain.py to test your class. sphereMain.py from sphere import * def Main(): s = Sphere(3) print("sphere s radius=", s.getRadius()) print("surface area=", s.surfaceArea()) print("volume=", s.volume()) Main() Then run sphereMain.py to test your implementation. The output should be the following: sphere s radius= 3 surface area= 113.097335529 volume= 339.292006588 Save the sphere tester in cs177/lab09/sphereMain.py
Powered by