TUGAS
PEMROGRAMAN WEB 2
Disusun oleh :
Nama : Findi Hardianty
NIM :
20160810036
Kelas : Teknik
Informatika B
FAKULTAS
ILMU KOMPUTER
UNIVERSITAS
KUNINGAN
2018
Program Encapsulation
|
<?php
class arithmetic
{
var $first=1000;
var $second=500;
function add()
{
$res=$this->first+$this->second;
echo "Gaji Harian = ".$res."<br/>";
}
function sub()
{
$res=$this->first-$this->second;
echo "Gaji Lembur = ".$res."<br/>";
}
function mult()
{
$res=$this->first*$this->second;
echo "Gaji Pokok = ".$res."<br/>";
}
}
$obj= new arithmetic( );
$obj->add( );
$obj->sub( );
$obj->mult( );
?>
|
Program Polymorphism
|
<?php
/**
* Interface Kain class.
**/
interface Kain {
public function calcArea();
}
/**
* Jika KainUtils class.
**/
class KainUtils {
static function
getKainInfo(Kain $KainObj) {
$Kain_terdapat =
get_class($KainObj);
return "Jika Kain
terdapat type {$Kain_terdapat} Maka Berukuran:
{$KainObj->calcArea()}";
}
}
/**
* Katun class.
**/
class Katun implements Kain {
private $height;
private $width;
public function
__construct($height, $width) {
$this->height = $height;
$this->width = $width;
}
public function calcArea() {
return $this->height * $this->width;
}
}
/**
* Sutra class.
**/
class Sutra implements Kain {
private $radius;
public function
__construct($radius) {
$this->radius = $radius;
}
public function calcArea() {
return
pow($this->radius, 2) * pi();
}
}
// Display Jika "Katun" info.
echo KainUtils::getKainInfo(new Katun(4, 5));
echo "</br>";
// Display Jika "Sutra" info.
echo KainUtils::getKainInfo(new Sutra(4));
?>
|
Comments
Post a Comment