""" unit06b_car_classes.py holds a few class definitions relating to automobiles that we will use when creating (instantiating) a particular car with specific properties. George Gollin, March 8, 2017 """ class Tire: """ Tire is the class definition for an automobile tire. """ ########################################################################### # here is the "class constructor." It is a "class function" or "class method" # used to initialize various parameters. # It is called automatically whenever you instantiate (create an instance of) # an object of this class. In Python it is always named __init__. Note the # pair of underscores at the beginning and end of the class constructor # name. def __init__(self): """ class constructor for Tire. """ # tire pressure (psi) is initialized to zero since we are instantiating # a brand new, fresh from the factory tire. Note the use of "self." to # indicate that this class variable is available to the outside world. self.pressure = 0 # maximum allowable pressure, psi (kaboom!) self.maximum_pressure = 45. # nominal pressure as a front tire (this will vary with the model of car) self.nominal_as_front_tire = 35. # nominal pressure as a rear tire (this will vary with the model of car) self.nominal_as_rear_tire = 32. # nominal pressure as a spare tire (this will vary with the model of car) self.nominal_as_spare_tire = 32. # low tire pressure warning threshold self.low_pressure_warning_threshold = 25. # end of class constructor. ########################################################################### # here is a class function to return the status of the tire. Status codes: # 0 everything is fine # -1 pressure below low pressure warning threshold # -2 pressure above maximum allowable pressure def status(self): """ class function to return status information about the tire. 0: everything is fine -1: underpressure -2: overpressure """ # initialize status code status_code = 0 # is there a problem? if self.pressure < self.low_pressure_warning_threshold: status_code = -1 elif self.pressure > self.maximum_pressure: status_code = -2 # we are finished. return status_code # end of class function. ########################################################################### # end of class Tire definition ########################################################################### class Engine: """ Engine is the class definition for an automobile engine. """ def __init__(self): """ class constructor for Engine """ # engine displacement (volume of cylinders in which air-fuel mixture # burns) in litres self.displacement = 1.6 # maximum rpm for engine self.RPM_limit = 8000 # transmission's miles-per-hour per RPM conversion factor self.MPH_per_RPM = 0.02 # current engine speed self.RPM = 0. # end of class constructor. ########################################################################### # here is a class function to return the status of the engine. Status codes: # 0 everything is fine # -1 RPM exceeds maximum value def status(self): """ class function to return status information about the tire. 0: everything is fine -1: RPM exceeds maximum value """ # initialize status code status_code = 0 # is there a problem? if self.RPM > self.RPM_limit: status_code = -1 # we are finished. return status_code # end of class function. ########################################################################### # here is a class function to translate RPM into MPH def getMPH(self): """ class function to return speed in MPH based on engoine RP """ return self.RPM * self.MPH_per_RPM # end of class function. ########################################################################### # here is a class function to return RPM corresponding to a particular MPH def MPHtoRPM(self, MPH): """ class function to return RPM associated with a particular speed """ # use the MPH per RPM figure to calculate the desired engine RPM. desired_RPM = MPH / self.MPH_per_RPM return desired_RPM # end of class function. ########################################################################### # end of class Engine definition ########################################################################### class Car: """ Car is the class definition for an automobile. We build the car from objects belonging to other classes: tires and an engine. """ def __init__(self): """ class constructor for Car """ # define a few of the car's properties. self.color = "#FF0000" self.has_fuzzy_dice = False self.bumper_sticker = "My other car is a fish" self.model = "Prestidigitator" # instantiate an engine. Note the use of parentheses on Engine so that # Python knows we are referring to a class. self.my_engine = Engine() # Instantiate the tires. self.left_front_tire = Tire() self.right_front_tire = Tire() self.left_rear_tire = Tire() self.right_rear_tire = Tire() # Now inflate the tires. Note the use of the class name in the constructor. # After the object is instantiated we can refer to the specific objects # by name (e.g. left_front_tire.nominal_as_front_tire), but we can't do # that yet. self.left_front_tire.pressure = Tire().nominal_as_front_tire self.right_front_tire.pressure = Tire().nominal_as_front_tire self.left_rear_tire.pressure = Tire().nominal_as_rear_tire self.right_rear_tire.pressure = Tire().nominal_as_rear_tire # end of class constructor. ########################################################################### def drive(self, MPH): """ class function to set the car parameters to drive at a certain speed. first check that tires and engine are ok, though. return a 0 if everything is fine, and a -1 if not. """ # first check tire status and print a warning if necessary. tires_are_OK = self.left_front_tire.status() == 0 and \ self.right_front_tire.status() == 0 and \ self.left_rear_tire.status() == 0 and \ self.right_rear_tire.status() == 0 # if tires are not OK exit with a warning message. if not tires_are_OK: print("tires are not OK so we are not going anywhere.") return -1 # now check that we can run the engine at the RPM necessary to go MPH necessary_RPM = self.my_engine.MPHtoRPM(MPH) if necessary_RPM >= self.my_engine.RPM_limit: print("uh oh, RPM exceeds engine maximum RPM") return -1 # if we get to here everything is fine. Set the engine speed. self.my_engine.RPM = necessary_RPM # print a message and return a 0 since all is fine. print("All is copacetic. \nWe are driving along at ", MPH, \ "miles per hour.") return 0 # end of class function ########################################################################### # end of class Car definition ###########################################################################