Python Fan functions
(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).
main()
SOLUTION:
■ 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).
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.
class Fan():
slow = 1
medium = 2
fast = 3
def __init__(self,speed = slow, radius = 5, color = 'blue', on = False):
self.__speed = speed
self.__radius = radius
self.__color = color
self.__on = on
def getSpeed(self):
return self.__speed
def getRadius(self):
return self.__radius
def getColor(self):
return self.__color
def getOn(self):
return self.__on
def setFanOn(self):
self.__on = True
def setFanOff(self):
self.__on = False
def setSpeedSlow(self):
self.__speed = self.slow
return self.__speed
def setSpeedMedium(self):
self.__speed = self.medium
return self.__speed
def setSpeedFast(self):
self.__speed = self.fast
return self.__speed
def setColor(self, color):
self.__color = color
def setRadius(self, radius):
self.__radius = radius
def main():
t = Fan()
t.setFanOn()
t.setSpeedFast()
t.setRadius(10)
t.setColor('Yellow')
print('Speed: ', t.getSpeed(),' Color: ', t.getColor(),' Radius: ', t.getRadius(),' On/Off: ', t.getOn())
p = Fan()
p.setSpeedMedium()
p.setRadius(5)
p.setColor('Blue')
p.setFanOff()
print('Speed: ', p.getSpeed(),' Color: ', p.getColor(),' Radius: ', p.getRadius(),' On/Off: ', p.getOn())
slow = 1
medium = 2
fast = 3
def __init__(self,speed = slow, radius = 5, color = 'blue', on = False):
self.__speed = speed
self.__radius = radius
self.__color = color
self.__on = on
def getSpeed(self):
return self.__speed
def getRadius(self):
return self.__radius
def getColor(self):
return self.__color
def getOn(self):
return self.__on
def setFanOn(self):
self.__on = True
def setFanOff(self):
self.__on = False
def setSpeedSlow(self):
self.__speed = self.slow
return self.__speed
def setSpeedMedium(self):
self.__speed = self.medium
return self.__speed
def setSpeedFast(self):
self.__speed = self.fast
return self.__speed
def setColor(self, color):
self.__color = color
def setRadius(self, radius):
self.__radius = radius
def main():
t = Fan()
t.setFanOn()
t.setSpeedFast()
t.setRadius(10)
t.setColor('Yellow')
print('Speed: ', t.getSpeed(),' Color: ', t.getColor(),' Radius: ', t.getRadius(),' On/Off: ', t.getOn())
p = Fan()
p.setSpeedMedium()
p.setRadius(5)
p.setColor('Blue')
p.setFanOff()
print('Speed: ', p.getSpeed(),' Color: ', p.getColor(),' Radius: ', p.getRadius(),' On/Off: ', p.getOn())
main()
Speed: 3 Color: Yellow Radius: 10 On/Off: True
Speed: 2 Color: Blue Radius: 5 On/Off: False
Коментари
Постави коментар