Describe the basic tenants of object-orientation

Assignment Help PHP Web Programming
Reference no: EM13910053

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') . "<br />");

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) . "<br />");

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 . '<br />');
$car -> drive(1000); print($car . '<br />');
$car -> addGas(5);
$car -> drive(10); print($car . '<br />');

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

Reference no: EM13910053

Questions Cloud

Binding energy of the electron : An xray photon of wavelength 0.989 nm strikes a surface. The emitted electron has a kinetic energy of 969eV. What is the binding energy of the electron in kJ/mol?
Explain and critique the pecking-order theory : What signals are provided to investors when a company obtains debt financing? What signals are provided to investors when a company obtains equity financing?
What is the probability of observing a sample proportion : Assuming that p equals .60 and the sample size is 1,000, what is the probability of observing a sample proportion that is at least .64?
Normalize the following table : Normalize the following table upto and including the 3NF. Submit a 1 page printout of only the final set of normalized tables in Data Architect. Just use Data Architect to do the tables. No need for E-R, mapping, relationships etc. Just tables. State..
Describe the basic tenants of object-orientation : 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 - Write a function modeMaker() tha..
What is the probability that the average fill for the drums : If we draw a random sample of 100 drums from the shipment, what is the probability that the average fill for the 100 drums is between 49.88 gallons and 50.12 gallons?
Inventory management : Inventory Management, What additional cost is the shop incurring by using this current order size rather than the economic order quantity?
Calculate weighted average cost of capital using book value : Promo Pak has compiled the following financial data: Calculate the weighted average cost of capital using book value weights.
Explain why it is important to calculate confidence interval : Explain why it is important to calculate a confidence interval. Explain the meaning of the term "95 percent confidence." Under what conditions is the confidence interval [x + za/2(s/ 1n)] for m valid?

Reviews

Write a Review

PHP Web Programming Questions & Answers

  Make a project on developing an online gift store

Make a project on developing an online gift store for gift item selling by using PHP({[P]ersonaL [H]ome [P]age} Hypertext Preprocessor). The system should be  an online application that can be accessed through the web.

  On 1 january 2007 megabucks ltd cleared a large area of

on 1 january 2007 megabucks ltd cleared a large area of land looking over a bay in which commercial prawn trawling was

  How can the ideas from ties that bind can help to compare

how can the ideas from ties that bind can help to compare business ethical decision making between usa and brazil. for

  Discuss the moral and economic implications involved in the

discuss the moral and economic implications involved in the movement.analyze each of the implications identified above

  What php feature automatically adds a backslash

What PHP feature automatically adds a backslash to any single quote, double quote, or NULL character contained in data that a user submits to a PHP script

  How technology fits porters five forces model

Project on the topic of web design: How technology fits porters five forces model

  Web application development

Create a simple Web Application that collects gradings under a number of criteria, and then calculates an aggregate mark

  How to connect php with mysql

How to connect PHP with MySQL

  Prepare an ajax enabled web form

Prepare an AJAX enabled web form utilising a ListView control that will allow logged-in staff to list, edit, delete, insert magazine details for magazines from a selected magazine category.

  Php script and then saves the form

Create a simple survey form that posts to a PHP script and then saves the form inputs to a file. The program should then respond with a display of the information contained in the file including the latest post

  Xhtml form that collects a name

Create an XHTML form that collects a name, address, phone number, and zip code. The phone number must be input in the format of ddd-ddd-dddd and the zip code must be in the format of ddddd-dddd.

  Qestion one describe the various ways in which a

question one describe the various ways in which a contractual relationship can come to an end including

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd