Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. # Created by: Anna Devlin
  2. # Based on bicycle program by Mr. Coxall
  3. # Created on: Nov 2016
  4. # Created for: ICS3U
  5. # This class is used to define a vehicle object
  6.  
  7. class Vehicle:
  8. # this class defines a vehicle
  9.  
  10. # class variable shared by all instances
  11.  
  12. def __init__(self, gear = 0, license_plate_number = '123ABC', colour = 'blue'):
  13. # private fields
  14.  
  15. self.__rpm = 10
  16. self.__speed = 0
  17. self.__gear = gear
  18. self.__license_plate_number = '123ABC'
  19. self.__colour = 'blue'
  20. self.__maximum_speed = 200
  21.  
  22. #in order to set fields "legally"
  23. self.set_license_plate_number(license_plate_number.upper())
  24. self.set_colour(colour)
  25.  
  26. #properties
  27. def get_rpm(self):
  28. # get the rpm property
  29. return self.__rpm
  30.  
  31. def get_colour(self):
  32. # get the rpm property
  33. return self.__colour
  34.  
  35. def set_colour(self, new_colour):
  36. #set the colour to one of the colours the dealership has in stock
  37.  
  38. if new_colour == 'blue' or new_colour == 'red' or new_colour == 'green' or new_colour == 'yellow' or new_colour == 'black':
  39. self.colour = new_colour
  40. else:
  41. pass
  42.  
  43. def get_speed(self):
  44. # get the speed property
  45. return self.__speed
  46.  
  47. def get_gear(self):
  48. # get the gear property
  49. return self.__gear
  50.  
  51. def set_gear(self, new_gear):
  52. # set the gear property
  53. if new_gear > 0 and new_gear < 6:
  54. self.__gear_speed_recalculation(new_gear)
  55. self.__gear = new_gear
  56.  
  57. def get_license_plate_number(self):
  58. # get the license plate number property
  59. return self.__license_plate_number
  60.  
  61. def set_license_plate_number(self, new_license_plate_number):
  62. # set the license plate number
  63.  
  64. if len(str(new_license_plate_number)) <= 8 and len(str(new_license_plate_number)) > 0:
  65. self.__license_plate_number = str(new_license_plate_number)
  66. else:
  67. self.__license_plate_number = '123ABC'
  68.  
  69. def get_maximum_speed(self):
  70. # get the maximum speed property
  71. return self.__maximum_speed
  72.  
  73. #private methods
  74. def __gear_speed_recalculation(self, new_gear):
  75. #changes the speed as the gear is changed
  76.  
  77. # old_gear is local
  78. old_gear = self.__gear
  79.  
  80. if self.__rpm != 0:
  81. if old_gear > new_gear:
  82. self.__speed = self.__speed - 2*(new_gear - old_gear)
  83. elif old_gear < new_gear:
  84. self.__speed = self.__speed + 2*(new_gear - old_gear)
  85. else:
  86. self.__speed = 0
  87.  
  88. # public methods
  89. def apply_accelerator(self, speed_increase):
  90. #increase the current speed by value passed in
  91.  
  92. self.__speed = self.__speed + speed_increase
  93. if self.__speed > self.__maximum_speed:
  94. self.__ = self.__maximum_speed
  95.  
  96. def apply_brakes(self, speed_decrease):
  97. # decrease the current speed by value passed in
  98.  
  99. self.__speed = self.__speed - speed_decrease
  100. if self.__speed < 0:
  101. self.__speed = 0
  102.  
  103. def current_state(self):
  104. # returns the current state of the bicycle as a string
  105. # not necessary because of the getters and setter, but convenient to get all the information
  106.  
  107. # this varaible is local to this method
  108. return_string = 'Rpm: ' + str(self.__rpm) + ' Speed: ' + str(self.__speed) + ' Gear: ' + str(self.__gear) + ' License Plate Number: ' + str(self.__license_plate_number) + ' Colour: ' + str(self.__colour) + ' Maximum Speed: ' + str(self.__maximum_speed)
  109. return return_string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement