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

  Random integral numbers based on normal distribution

Prepare a system to generate random integral numbers based on normal distribution. Study Data Generator's structure and extend number generation type to activate normal distribution.

  Shopping cart program for web applications class

Shopping Cart program for web applications class. Allows user to browse while keeping track of the items in which they will purchase at the end on the order page link and this will give a final price for all items.

  Create a web site for an apple farm

Create a web site for an apple farm. Create an HTML5 form allowing visitors to create an account with the site. Account details are to be stored in your MySQL database. Information should be stored in a secure way.

  Develop a dynamic website open university

Develop a dynamic website Open University

  Sample website project

This website consists of three sections: a narrative, a storyboard, and a business Website.

  Online banking application

Designing and developing a web applications The company you are working has secured a contract with a local banking group to develop an ONLINE BANKING APPLICATION using PHP and MySQL.

  Design a dynamic database

Design a dynamic database using Mangodb, html , and php.

  Show the accessibility and usability of website

The webpage must have several menus about country Azerbaijan such as "About Azerbaijan" "History" "Geography" "Landscape" and "Accessibility" menu.

  Implement a web application

Implement a web application called CS320Starter, which is similar to the crowd funding platform Kickstarter where people raise funds for their projects.

  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.

  Create a very simple order system as a web application

Create a very simple Order System as a web application.

  Which of the following jsp expressions is valid

A JSP expression can contain any Java expression that evaluates to a String object, Java object, primitive type, primitive type or Java object

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