Recent Question/Assignment

Week 5 Hands-On Exercises – Object-Oriented Design
Source: Source: Connolly, R. and Hoar, R., (2018), Fundamentals of Web Development
Before you do the exercises
If you haven’t already done so, you should create a folder in your personal drive for all the exercises. This set of exercises requires a functioning webserver to interpret the PHP code.
Exercise 5-1 Data Encapsulation
1. Our exercises in this chapter to date have used a class where all the members are public. To demonstrate the definition, usage and purpose of data encapsulation, open, examine, and test lab05-exercise01.php, outputs a forecast, but unlike the previous exercise, uses dot notation to update values before outputting.
2. Open the file Forecastv2.class.php, then save it into a new file called Forecastv3.class.php. This new file is to be used in this exercise.
3. To make this code work, you will have to add default values to every parameter in the constructor for Forecast.
function __construct($d=0, $h=0, $l=0, $desc=0) {
4. As you can see your current output no longer captures the correct high and low temperatures. This is because in Exercise 4, the check to update the static variables happened in the constructor. Now, because we set the values outside the constructor, the updated highs and low don't update the static allTimeHigh and allTimeLow variables. This will be solved using encapsulation.
Change all the public (non static) variables to private.
private $date; private $high; private $low; private $description; public static $allTimeHigh=0; public static $allTimeLow=100;
Reload the page and you will get a fatal error output:
Fatal error: Cannot access private property
This happens because lab05-exercise01.php uses - notation to access and update the variables in the class.
5. To fix this problem we will write public getter and setter methods for each private variable as follows:
public function getDate(){ return $this- date;
}
public function setDate($d){
$this- date = $d;
}
public function getHigh(){ return $this- high;
}
public function setHigh($h){ $this- high = $h;
}
public function getLow(){ return $this- low;
}
public function setLow($l){ $this- low = $l;
}
public function getDescription(){ return $this- description;
}
public function setDescription($d){ $this- description = $d;
}
6. Now in lab05-exercise01.php, replace the former accessing of variables directly with calls to the appropriate setter as follows:
for($i=0;$i 7;$i++){
$dayOne = new Forecast();
$dayOne- setHigh($i*5);
$dayOne- setLow($i*-5);
$dayOne- setDate(date(-d M, Y-, $today+$i*$oneday));
$dayOne- setDescription(-Sunny-);
$forecast[]=$dayOne;
}
7. Run and test lab05-exercise01.php. Your final page should almost look correct now, but the allTimeHgh and allTimeLow variables are still not being updated. So, move on!
By adding the check for the lowest and highest values (as we did in our constructor back in Exercise 4-3) into our setter functions, we will ensure data is correct. Now we can rest assured that however our class is used, the allTimeHigh and allTimeLow will be updated correctly, since they can only be modified by the constructor or the setter. Correcting this oversight will serve as a proof of concept about why encapsulation matters. The code below shows how to add to our setters for high and low with changes in red
public function setHigh($h){ if($h self::$allTimeHigh){
self::$allTimeHigh = $h;
}
$this- high = $h;
}
public function setLow($l){
if($l self::$allTimeLow) {
self::$allTimeLow = $l;
}
$this- low = $l;
}
Now your page will contain not look like Figure 5.1, but will manage and validate the all time high and low, including every time any instance of a Forecast is updated!
Figure 5.1 Screenshot of the completed Exercise 5-1 with encapsulated getters and setters.
8. Run the program, check the results, and take screenshots. Your output will look similar to Figure 5.1.
Exercise 5-2 Inheritance
1. Open and run lab05-exercise02.php. Notice that it prints out a list of vehicles.
2. Open Vehicle.class.php, to see the given definition for a Vehicle. Add one more class below the definition of Vehicle: LandVehicle as follows:
class LandVehicle extends Vehicle { private $wheels;
function __construct($mk, $md, $f, $spd, $whlCount) { parent::__construct($mk, $md, $f, $spd);
$this- wheels = $whlCount; }
}
3. Now modify the instantiation of the second vehicle lab05-exercise02.php, to be a LandVehicle, and pass 4, to say it has 4 wheels.
$hybridCar = new LandVehicle(-Ford-, -Prius-, -Hybrid-,-160-,4);
When you run the page, you will get error messages about the properties defined in the parent class not being accessible. To fix this, modify the declaration of the properties in the vehicle from private to protected:
protected $make; protected $model; protected $fuel;
protected $topSpeed;
4. To make the LandVehicle output a different output than the generic vehicle we must override the __toString Method. For our LandVehicle add the following:
function __toString(){ return
' div class=-panel panel-default col-lg-3 col-md-4 col-sm-6-
div class=-panel-heading-
h3 class=-panel-title- '.$this- make.' /h3
/div
div class=-panel-body-
table class=-table table-hover-
tr td Model: /td td '.$this- model.' /td /tr
tr td Fuel: /td td '.$this- fuel.' /td /tr tr td Top Speed: /td td '. $this- topSpeed .
' Mph /td /tr
tr td Wheels: /td td '.$this- wheels.' /td /tr
/table
/div
/div ';
}
Notice we are accessing the $wheels element of this class, and the elements in the parent class. Now out page will output a different box for a Land Vehicle than it does a regular base Vehicle (namely it will tell us how many wheels it has).
5. Finally, we will create a second subclass to represent Water Vehicles. Much like LandVehicle we will add some properties for water vehicles and override the __toString() method. (In this case we are adding the boat's capacity, lifeboat capacity, and changing the speed to be in Knots rather than Miles per hour).
class WaterVehicle extends Vehicle { private $capacity; private $lifeBoatCapacity;
function __construct($mk, $md, $f, $spd,$cap, $lifeboat) { parent::__construct($mk, $md, $f, $spd);
$this- capacity = $cap; $this- lifeBoatCapacity = $lifeboat;
}
function __toString(){ return
‘ div class=-panel panel-default col-lg-3 col-md-4 col-sm-6-
div class=-panel-heading-
h3 class=-panel-title- '.$this- make.' /h3
/div
div class=-panel-body-
table class=-table table-hover-
tr td Model: /td td '.$this- model.' /td /tr
tr td Fuel: /td td '.$this- fuel.' /td /tr tr td Top Speed: /td td ' . $this- topSpeed .
' Knots /td /tr
tr td Capacity: /td td '.$this- capacity.' /td /tr
tr td Life Boat Capacity: /td td ' .
$this- lifeBoatCapacity . ' /td /tr
/table
/div
/div ';
}
}
6. In lab05-exercise02.php, instantiate a water vehicle and echo it as follows:
$boat = new WaterVehicle(-White Star Line-, -Titanic-,
-Steam-,-24-,3327,1178);
echo $boat;
7. Run the program, check the results, and take screenshots. Your output will look similar to Figure 5.2.
8. Save the file.
Figure 5.2 A list of vehicles; the output of Exercise 5-2.
To Submit:
Create a folder and name it “Week5handson” - no spaces in the name. Put on all of the program files that you have created for all exercises and a file containing screenshots of running the programs into this folder. Compress the folder, using a zip or rar utility, and then submit this zipped folder to the correct submission link on Moodle.

Looking for answers ?