Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Object.php Create a PHP script that will perform the following tasks. Define a c

ID: 3675638 • Letter: O

Question

Object.php

Create a PHP script that will perform the following tasks.

Define a class Vehicle which has protected properties: make, model, year, price. Create a constructor method that takes in make, model, year, and price. Implement a public method displayObject() to display the properties of each object instance.

Define a derived class LandVehicle that inherits from the Vehicle class and contains a private property: maxSpeed. You may need to override the constructor and displayObject() method for this derived class.

Define another derived class WaterVehicle that also inherits from the Vehicle class and contains private property: boatCapacity. You may need to override the constructor and displayObject() method for this derived class.

Instantiate (Create) at least three objects of LandVehicle and display the properties of each object instance. Sample output is as follows:

Make:Toyota,

Model:Camry,

Year:1992,

Price:2000,

Speed Limit:180

Make:Honda,

Model:Accord,

Year:2002,

Price:6000,

Speed Limit:200

Instantiate (Create) at least three objects of WaterVehicle and display the properties of each object instance. Sample output is as follows:

Make:Mitsubishi,

Model:Turbo,

Year:1999,

Price:20000,

Boat Capacity:18

Make:Hyundai,

Model:XT,

Year:2012,

Price:26000,

Boat Capacity:8

Make:Toyota,

Model:Camry,

Year:1992,

Price:2000,

Speed Limit:180

Make:Honda,

Model:Accord,

Year:2002,

Price:6000,

Speed Limit:200

Explanation / Answer

<html
<head>
<title>Untitled Page</title>
</head>
<body>

<?php
echo "Remember PHP works best when put inside a HTML page!! "
echo "test php"

class Vehicle   {
   protected: /* create the properties */
       var $make;
       var $model;
       var $year;
/* the constructor function - same name as the class name - similar to java and c++ */
       function Vehicle($make, $model, $year)   {
           $this->add_item($make, $model, $year);
       } /* end constructor function */

       function displayObject()   {
           echo $make;
           echo $model;
           echo $year;

       } /* end function disp obj */
} /* end class Vehicle */

class LandVehicle extends Vehicle   {
/* inherit from the Vehicle class */
   var $maxSpeed;
/* time to override */
       function Vehicle($make, $model, $year, $maxSpeed)   {
           $this->add_item($make, $model, $year, $maxSpeed);          
       } /* end constructor function */

       function displayObject()   {
           echo $make;
           echo $model;
           echo $year;
           echo $maxSpeed;

       } /* end function disp obj */
  
} /* end class LandVehicle */

class WaterVehicle extends Vehicle   {
/* inherited from the Vehicle class */
   var $boatCapacity;
/* time to override */
       function Vehicle($make, $model, $year, $boatCapacity)   {
           $this->add_item($make, $model, $year, $boatCapacity);          
       } /* end constructor function */

       function displayObject()   {
           echo $make;
           echo $model;
           echo $year;
           echo $boatCapacity;

       } /* end function disp obj */
  
  
} /* end class WaterVehicle */

/* time to instantiate the objects of the classes */
$car1 = new LandVehicle("Toyata", "Camry", 1992, 180);
$car2 = new LandVehicle("Honda", "Accord", 2002, 200);

$boat1 = new WaterVehicle("Mitsubishi", "Turbo", 1999, 18);
$boat2 = new WaterVehicle("Hyundai", "XT", 2012, 8);
$boat3WellAShip = new WaterVehicle("GeneralMotors", "Titanic", 1979, 5000);
/* a ship can hold up to 5000 people easily !! hence capacity = 5000 */


/* a pit fall in PHP to watch for: PHP does not call the constructors from the base class when they are included in the derived or imherited classes - it is up to us to include the coding tp pass the call on through from derived to base classes as and when needed */
/* in our case the constructor in WaterVehicle and LandVehicle will not call the constructor of the base Vehicle class unless we do so explicitly *

?>


</body>
</html>