In: Computer Science
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that specifies the radius of the fan. ■ A private string data field named color that specifies the color of the fan. ■ The accessor and mutator methods for all four data fields. ■ A constructor that creates a fan with the specified speed (default SLOW), radius (default 5), color (default blue), and on (default False). Draw the UML diagram for the class and then implement the class. Write a test program that creates two Fan objects. For the first object, assign the maximum speed, radius 10, color yellow, and turn it on. Assign medium speed, radius 5, color blue, and turn it off for the second object. Display each object’s speed, radius, color, and on properties.
CODE:
class Fan():
SLOW = 1
MEDIUM = 2
FAST = 3
def __init__( self , on = False , rad = 5 , spd = SLOW , clr = "blue" ):
self.speed = spd
self.radius = rad
self.color = clr
self.on = on
def __str__(self):
if self.on == True:
return "\n\t Fan Speed = " + self.get_Speed() + "\n\t Fan Radius = " + str(self.radius) + "\n\t Fan Color = " + self.color + "\n"
else:
return "\n\t Fan is off" + "\n\t Fan Radius = " + str(self.radius) + "\n\t Fan Color = " + self.color + "\n"
def get_Speed(self):
if self.speed == 1:
return "SLOW"
elif self.speed == 2:
return "MEDIUM"
elif self.speed == 3:
return "FAST"
def set_Speed(self,spd):
self.speed = spd
def get_Radius(self):
return self.radius
def set_Radius(self,rad):
self.radius = rad
def get_Color(self):
return self.color
def set_Color(self,clr):
self.color = clr
def Set_ON(self):
self.on = True
def Set_OFF(self):
self.on = False
def get_ON(self):
return self.on
f = Fan()
f1 = Fan( True , 10 , 3 , "Yellow" )
f2 = Fan(False , 5 , 2 , "Blue")
print("\n Default FAN :")
print(f)
print("\n FAN 1 :")
print(f1)
print("\n FAN 2 :")
print(f2)
OUTPUT:
UML Diagram:
DON'T FORGET TO GIVE A LIKE