Advertisement
Guest User

P2PU Python course: Objects

a guest
Apr 7th, 2013
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. #Tasks
  2. #1. Two objects: Car and Gas Station
  3. #2. List several attributes for each object
  4. #3.  List at least one method that the object employs
  5. #4. Create Python pseudocode for each object, don't forget the __init__() function.
  6. #5. Post your pseudo code to this task discussion.
  7. # Extra Credit: Have one of the objects return information that is utilized by the other object.
  8.  
  9. class Car:
  10.     """Typical personal transport vehicle in use since the beginning of the
  11.    20th century"""
  12.     def __init__(self, make, seats, fuel_type, power):
  13.         "initialisation of an instance of the car class"
  14.         self.make = make
  15.         self.seats = seats
  16.         self.fuel_type = fuel_type
  17.         self.power = power      #in kWh
  18.         self.fuel_level = 0.0   #initially the fuel tank is empty (in liters)
  19.  
  20.     def calculate_fuel(self, new_fuel):
  21.         "Calculate the fuel in the car's tank"
  22.         self.fuel_level += float(new_fuel)
  23.         print ("It has now %d liters of fuel.") % (self.fuel_level)
  24.  
  25.  
  26. class GasStation:
  27.     "A place where one can fill up the fuel tank of one's car."
  28.     def __init__(self, hydrogen_price):
  29.         "Creates an instance of the GasStation class"
  30.         self.h2_price = float(hydrogen_price)
  31.  
  32.     def get_fuel(self, Car, fuel):
  33.         """Gets fuel for the Car and calculates price and new fuel level,
  34.        by passing the added fuel to the Car.calculate_fuel method """
  35.         price = float(fuel) * self.h2_price
  36.         print "The %s has taken %d liters. Total cost is: %d." % (Car.make, fuel, price)
  37.         Car.calculate_fuel(fuel)
  38.  
  39. mycar = Car('Riversimple', 4, 'hydrogen', 60)
  40. hydrogenstation = GasStation(2.45)
  41. hydrogenstation.get_fuel(mycar, 8.5)
  42. hydrogenstation.get_fuel(mycar, 4)
  43. dirtycar = Car('Ford', 7, 'gasoline', 150)
  44. gasstation = GasStation(1.8)
  45. gasstation.get_fuel(dirtycar, 50)
  46. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement