Package car_sim :: Module car :: Class Car
[hide private]
[frames] | no frames]

Class Car

object --+
         |
        Car

Class Car represents cars which can be drive about on a two-dimensional coordinate plane. A car has a fuel tank that contains gasoline (the maximum amount is determined by the car's tank size). Each car can have a different fuel consumption rate.

N.B. Wherever the class car makes use of Location, the coordinates of each location are in kilometers (float). For instance, the distance between (-5, 0) and (7, 0) is 12km.

Instance Methods [hide private]
 
__init__(self, fuel_consumption, tank_size, fuel_in_tank, location)
Constructs a new car with the given attributes and which has its kilometer count set to zero.
boolean
drive(self, destination)
Drives in a straight line towards the destination.
float
fuel(self, fuel_amount)
Adds a given amount of fuel to the tank, if possible.
float
fill_up(self)
Fills up the tank of this car.
float
get_fuel_ratio(self)
Returns a number between 0 and 100 that represents the percentage of fuel in the tank.
location object
get_location(self)
Tells where the car is located at the moment
float
get_kilometers_driven(self)
Returns the total number of kilometers driven with this car.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, fuel_consumption, tank_size, fuel_in_tank, location)
(Constructor)

 

Constructs a new car with the given attributes and which has its kilometer count set to zero.

Parameters:
  • fuel_consumption (float) - the fuel consumption of the new car in liters per 100 km
  • tank_size (int) - the size of the new car's fuel tank in liters
  • fuel_in_tank (float) - the initial amount of fuel in the car's tank (at most equal to tank size)
  • location (location object) - the starting location of the car
Overrides: object.__init__

drive(self, destination)

 

Drives in a straight line towards the destination. If there isn't enough fuel, the car stops somewhere along the way. This method calculates how far is the destination from the starting location, figures out how many kilometers can be driven using current fuel amount, consumes fuel and updates kilometer count, determines new location and creates an object of it.

Parameters:
  • destination (location object) - the intended destination of the car
Returns: boolean
True/False indicating if the car reached its intended destination

fuel(self, fuel_amount)

 

Adds a given amount of fuel to the tank, if possible. If the given amount cannot be poured into the tank, the tank is filled up and the rest of the fuel discarded.

Parameters:
  • fuel_amount (float) - the number of liters of fuel to be placed in the tank
Returns: float
the number of liters of fuel successfully placed in the tank

fill_up(self)

 

Fills up the tank of this car. (Hint: you can call the method fuel from this method to obtain an elegant implementation.)

Returns: float
the number of liters of fuel successfully placed in the tank

get_fuel_ratio(self)

 

Returns a number between 0 and 100 that represents the percentage of fuel in the tank. That is, 100 means the tank is full and 0 means it is empty.

Returns: float
a percentage value indicating how full the car's tank is

get_location(self)

 

Tells where the car is located at the moment

Returns: location object
the car's location

get_kilometers_driven(self)

 

Returns the total number of kilometers driven with this car.

Returns: float
the number of kilometers driven with the car