Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask PHP Expert

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)

PHP , Programming

  • Category:- PHP
  • Reference No.:- M91582987
  • Price:- $165

Guranteed 48 Hours Delivery, In Price:- $165

Have any Question?


Related Questions in PHP

Final project assignment -requirements specifications -1

FINAL PROJECT ASSIGNMENT - REQUIREMENTS / SPECIFICATIONS - 1. For the final project, you will have to make good use of your CMS using PHP and MySQL for the content delivery. You will have to be able to deliver the conten ...

In php write a simple addtion calculator that reads in two

In PHP write a simple addtion calculator that reads in two values and then add them together and display the output. Be sure to validate both inputs and ensure that it can add 0+0 = Sum: 0. The user should be able to inp ...

Question continue to build on the skills of providing web

Question: Continue to build on the skills of providing Web page content and structure with HTML and Web page style and layout using CSS. Demonstrate the skill of creating a dynamic Web page that includes JavaScript clien ...

Question using the course software and week one template as

Question: Using the course software and Week One Template as a starting point, create a new HTML webpage and include the following: • Write the code to display your name in the largest-size heading element. • Write the m ...

Question develop a 5-6 page word document that describes

Question: Develop a 5-6 page Word document that describes the design for a small Web site that meets the following specifications. 1. The Web site includes four or more Web pages designed according to current usability g ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As