Write a function reducearr func that takes an array and a


1. Describe, in your own words, the 5 basic tenants of object-orientation.

Programming

2. Write a function reduce($arr, $func) that takes an array and a function as a parameter. The reduce function should apply the parameter function to each element of the array in succession to produce a single result. Note: the current result should always be the first parameter to the function and the next element of the array should always be the second parameter. You may not use the PHP function array_reduce in your solution. For example, the result of the following should be 10.

function myMax($current, $new) {
return $current < $new ? $new : $current;
}

$arr = array(10, 5, 3, 5, 1, 2, 5, 7, 4);
print("Max: " . reduce($arr, 'myMax') . "
");

3. Write a function modeMaker() that forms a closure such that the function it returns can be used with your reduce() function above to find the mode of an array. The mode of an array is the value that appears the most frequently (so in the $arr above, the mode is 5). To do this, you will need an array called $seen that keeps the count of times an element has been examined At the end of the reduce function, the $seen array should look like the following for the $arr above:

Array (
[10] => 1
[5] => 3
[3] => 1
[1] => 1
[2] => 1
[7] => 1
)

Note that the closure you generate should always be returning the current mode for what it has seen so far (so it will either be the current mode, or the new element passed in). The following is a start for modeMaker:
function modeMaker() {
$seen = array();
return function($current, $new) use (&$seen) {
// your code that uses $seen goes here
};
}

$mode = modeMaker();
$arr = array(10, 5, 3, 5, 1, 2, 5, 7, 4);
print("Mode: " . reduce($arr, $mode) . "
");

4. 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:

a. construct($initialGas, $mpg): which takes the initial gas amount and miles per gallon as parameters.
b. drive($miles): which will drive the specified miles (deducting gas from the tank and incrementing miles on the odometer)
c. addGas($gallons): which will add more gas to the tank (the tank is of unlimited capacity).
d. readFuelGauge(): which will return the number of gallons of gas remaining in the tank.
e. 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 toString() 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)

Request for Solution File

Ask an Expert for Answer!!
PHP Web Programming: Write a function reducearr func that takes an array and a
Reference No:- TGS01208345

Expected delivery within 24 Hours