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

Class Car

source code

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. 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.
source code
 
drive(self, destination)
Drives in a straight line towards the destination.
source code
 
fuel(self, fuel_amount)
Adds a given amount of fuel to the tank, if possible.
source code
 
fill_up(self)
Fills up the tank of this car.
source code
 
get_fuel_ratio(self)
Returns a number between 0 and 100 that represents the percentage of fuel in the tank.
source code
 
get_location(self)
Tells where the car is located at the moment
source code
 
get_kilometers_driven(self)
Returns the total number of kilometers driven with this car.
source code

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)

source code 

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

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

drive(self, destination)

source code 

Drives in a straight line towards the destination. If there isn't enough fuel, the car stops somewhere along the way.

Parameters:
  • destination - the intended destination of the car
Returns:
a boolean value indicating if the car reached its intended destination

fuel(self, fuel_amount)

source code 

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 - the number of liters of fuel to be placed in the tank
Returns:
the number of liters of fuel successfully placed in the tank

fill_up(self)

source code 

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

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

get_fuel_ratio(self)

source code 

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:
a percentage value indicating how full the car's tank is

get_location(self)

source code 

Tells where the car is located at the moment

Returns:
the car's location

get_kilometers_driven(self)

source code 

Returns the total number of kilometers driven with this car.

Returns:
the number of kilometers driven with the car