Question: In PHP, I need help to write a class that models the concept of a Car that has a fuel economy (in miles per gallon),

In PHP, I need help to write a class that models the concept of a Car that has a fuel economy (in miles per gallon), an odometer, and a gas tank. Your car should be able to drive a certain number of miles (specified as a parameter) until it reaches its distance or runs out of gas, whichever comes first. Further, you should write functionality to add gas to the car, and read the fuel gauge and the odometer. You should write the following methods:

__construct($initialGas, $mpg): which takes the initial gas amount and miles per gallon as parameters.

drive($miles): which will drive the specified miles (deducting gas from the tank and incrementing miles on the odometer)

addGas($gallons): which will add more gas to the tank (the tank is of unlimited capacity).

readFuelGauge(): which will return the number of gallons of gas remaining in the tank.

readOdometer(): which will return the number of miles logged on the odometer.

You can use the following method inside the Car class to help you debug:

public function __toString() {

return 'Car (gas: ' . $this->readFuelGauge() .

', miles: ' . $this->readOdometer() . ')';

}

Whenever you print a car object, PHP will automatically call the method to determine what should be output.

If you execute the following simple program, you should get the output that follows:

$car = new Car(20, 25);

$car -> drive(25);

print($car . '
');

$car -> drive(1000);

print($car . '
');

$car -> addGas(5);

$car -> drive(10);

print($car . '
');

Car (gas: 19, miles: 25) Car (gas: 0, miles: 500) Car (gas: 4.6, miles: 510)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!