Car

class Car(val fuelConsumption: Double, val tankSize: Double, initialFuel: Double, initialLocation: Pos)

The class Car represents virtual cars which can drive about on a surface. A car has a fuel tank that contains gasoline, which is spent as the car moves but can be added using an appropriate method.

Although some aspects of a car object are immutable (e.g., fuel consumption rate), some others change (e.g., location, fuel).

Value parameters:
fuelConsumption

the fuel consumption of the new car in liters per 100 km

initialFuel

the initial amount of fuel in the car’s tank, in liters (at most equal to tank size)

initialLocation

the starting location of the car

tankSize

the size of the new car’s fuel tank, in liters

class Object
trait Matchable
class Any

Value members

Concrete methods

def drive(destination: Pos, metersToDestination: Double): Unit

Drives the car toward the given destination. This causes the car’s location and its fuel level to change.

Drives the car toward the given destination. This causes the car’s location and its fuel level to change.

Assuming the car has enough fuel to reach the destination, it moves there. (In effect, the car changes location just once and “teleports” to the destination, with no intermediate steps.)

If fuel runs out, the car stops along the way, and its new location is determined by finding the location along the route where the car no longer has the fuel to move further. An example: A car drives from (61.0, 25.0) towards the destination (62.0, 25.5), but only has fuel for 60% of the trip. Consequently, it stops at (61.6, 25.3).

For purposes of determining the car’s stopping point (if fuel runs out) and the amount of fuel consumed by the drive, the car is assumed to move in a straight line “as the crow flies” at a constant speed and to consume fuel evenly throughout.

Note: As the example above illustrates, this method assumes that the car and its destination exist not on a sphere (or geoid) but on a flat, two-dimensional surface whose x and y coordinates map directly to longitude and latitude. The method relies on the assumption that the driven distance is very short compared to the size of Earth, which allows this simplification.

Also note: This method receives, as a parameter, the overall distance from the starting location to the intended destination. The method relies on this information rather than attempting to compute the number of meters between these two points.

Value parameters:
destination

the intended destination of the car

metersToDestination

the distance, in meters, from the car’s current location to destination

def fuel(toBeAdded: Double): Double

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.

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.

Value parameters:
toBeAdded

the number of liters of fuel to be placed in the tank

Returns:

the number of liters of fuel successfully placed in the tank

def fuel(): Double

Fills the tank of this car to capacity. Returns the number of liters of fuel that were successfully placed in the tank.

Fills the tank of this car to capacity. Returns the number of liters of fuel that were successfully placed in the tank.

Returns the distance in that the car can drive with the remaining amount of fuel. Note that this method returns the distance in meters even though fuelConsumption is recorded as liters per 100 km.

Returns the distance in that the car can drive with the remaining amount of fuel. Note that this method returns the distance in meters even though fuelConsumption is recorded as liters per 100 km.

Returns a number between 0.0 and 100.0 that represents the amount of fuel in the tank as a percentage. That is, 100.0 means the tank is full and 0.0 means it is empty.

Returns a number between 0.0 and 100.0 that represents the amount of fuel in the tank as a percentage. That is, 100.0 means the tank is full and 0.0 means it is empty.

Returns the current location of the car.

Returns the current location of the car.

Returns the number of meters the car has been driven, in total, since it was created.

Returns the number of meters the car has been driven, in total, since it was created.

Concrete fields