Menu Close

TYBCS PHP Assignment 4 – Object Oriented Programming

TYBCS PHP Assignment 4 Solutions on this Website. Some of the solutions of Set A, Set B, Set C is available here.

TYBCS PHP Assignment 4 Set A

Q: 1) Define an interface which has methods area( ), volume( ). Define constant PI. Create a class cylinder which implements this interface and calculate area and volume. (Hint: Use define( ))

HTML FILE

<html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <body> <form class="" action="a4a1.php" method="post"> Enter the radius: <input type="text" name="rad" value=""><br><br> Enter height:<input type="text" name="height" value=""><br><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
Code language: HTML, XML (xml)

PHP FILE saved as “a4a1.php”

<?php $r=$_POST['rad']; $h=$_POST['height']; define('PI','3.14'); interface functions { function area($r,$h); function volume($r,$h); } class cylinder implements functions { function area($r,$h) { $area=(2*PI*$r($h+$h)); echo "Area of cylinder is: $area"; } function volume($r,$h) { $volume=(PI*$r*$r*$h); echo "Volume of the cylinder is: $volume"; } } $x=new cylinder(); $x->area($r,$h); $x->volume($r,$h); ?>
Code language: PHP (php)

Q: 2) Write class declarations and member function definitions for an employee(code, name, designation). Derive emp_account(account_no, joining_date) from employee and emp_sal(basic_pay, earnings, deduction) from emp_account. Write a menu driven program a) To build a master table b) To sort all entries c) To search an entry d) Display salary

HTML FILE

<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <body> <form class="" action="a4a2.php" method="post"> <input type="radio" name="rad" value="1">Build a Master Table <br><br> <input type="radio" name="rad" value="2">Sort all entries <br><br> <input type="radio" name="rad" value="3">Search an entry by Employee's Name <input type="text" name="ename"> <br><br> <input type="radio" name="rad" value="4">Display Salary <br><br> <input type="submit" name="sub" value="Submit"> </form> </body> </html>
Code language: HTML, XML (xml)

PHP FILE saved as “a4a2.php”

<?php class employee { public $code,$name,$desig; function __construct($a,$b,$c) { $this->code=$a; $this->name=$b; $this->desig=$c; } public function display_emp() { echo' <td>'.$this->code.'</td> <td>'.$this->name.'</td> <td>'.$this->desig.'</td> '; } public function getname() { return $this->name; } } class emp_acc extends employee { public $ano,$jdate; function __construct($a,$b,$c,$d,$e) { parent::__construct($a,$b,$c); $this->ano=$d; $this->jdate=$e; } public function display_acc() { echo' <td>'.$this->ano.'</td> <td>'.$this->jdate.'</td> '; } } class emp_sal extends emp_acc { public $bpay,$earns,$ded,$total; function __construct($a,$b,$c,$d,$e,$f,$g,$h) { parent::__construct($a,$b,$c,$d,$e); $this->bpay=$f; $this->earns=$g; $this->ded=$h; $this->total=$this->bpay + $this->earns - $this->ded; } public function display_sal() { echo' <td>'.$this->bpay.'</td> <td>'.$this->earns.'</td> <td>'.$this->ded.'</td> <td>'.$this->total.'</td> '; } } $emp[0]=new emp_sal('A923B','Ramesh','Staff','10001','21/09/2011','20000','5000','3000'); $emp[1]=new emp_sal('A823B','Ram','HR','10002','22/09/2011','25000','5000','3000'); $emp[2]=new emp_sal('A723B','Sita','Analyst','10003','23/09/2011','22000','5000','3000'); $emp[3]=new emp_sal('A623B','Gita','Organiser','10004','24/09/2011','21000','5000','3000'); $emp[4]=new emp_sal('A523B','Aman','Manager','10005','25/09/2011','30000','5000','3000'); $emp[5]=new emp_sal('A423B','Amar','Staff','10006','26/09/2011','20000','5000','3000'); $emp[6]=new emp_sal('A323B','Tarun','Analyst','10007','27/09/2011','21000','5000','3000'); $emp[7]=new emp_sal('A223B','Mahi','Organiser','10008','28/09/2011','21000','5000','3000'); $emp[8]=new emp_sal('A123B','Karna','Staff','10009','29/09/2011','20000','5000','3000'); $emp[9]=new emp_sal('A023B','Suraj','Staff','10010','30/09/2011','20000','5000','3000'); $ch=$_POST['rad']; $ename=$_POST['ename']; $flag=0; function mastertable($emp) { echo '<table border="1" width="100%"> <tr> <th>Code</th> <th>Name</th> <th>Designation</th> <th>Ac No</th> <th>Join date</th> <th>Basic Pay</th> <th>Earnings</th> <th>Deduction</th> <th>Total Salary</th> </tr>'; for($i=0;$i<10;$i++) { echo '<tr>'; $emp[$i]->display_emp(); $emp[$i]->display_acc(); $emp[$i]->display_sal(); echo '</tr>'; } echo '</table>'; } switch($ch) { case 1: mastertable($emp); break; case 2: echo 'Sorting w.r.t Employee Code <br><br>'; function srt($a,$b) { return strcmp($a->code,$b->code); } usort($emp,"srt"); mastertable($emp); break; case 3: echo "Searching for employee $ename .....<br><br>"; for($i=0;$i<10;$i++) { $temp=$emp[$i]->getname(); if($temp==$ename) { $flag=1; break; } } if($flag==0) { echo "Not Found<br>"; } else { echo "Found in the records<br>"; } break; case 4: echo "Displaying Salary of all Employees .... <br><br>"; echo '<table border="1" width="100%"> <tr> <th>Employee Name</th> <th>Basic Pay</th> <th>Earnings</th> <th>Deduction</th> <th>Total Salary</th> </tr>'; for($i=0;$i<10;$i++) { echo '<tr> <td>'.$emp[$i]->getname().'</td> '; $emp[$i]->display_sal(); } break; } ?>
Code language: HTML, XML (xml)

TYBCS PHP Assignment 4 Set B

Q:1) Create class rectangle and derive a class square from class Rectangle. Create another class circle. Create an interface with only one method called area(). Implement this interface in all the classes. Include appropriate data members and constructors in all classes. Write a program to accept details of a square, circle and rectangle and display the area.

HTML FILE

<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <body> <form class="" action="a4b1.php" method="post"> <li> Rectangle <br><br> Length: <input type="text" name="len"> <br><br> Width: <input type="text" name="width"> <br><br> </li> <li> Square <br><br> Side: <input type="text" name="side"> <br><br> </li> <li> Circle <br><br> Radius: <input type="text" name="radius"> <br><br> </li> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
Code language: HTML, XML (xml)

PHP FILE saved as “a4b1.php”

<?php define("PI","3.142"); interface a { function area(); } class rectangle implements a { public $l,$b; function __construct($x,$y) { $this->l=$x; $this->b=$y; } function area() { $area=$this->l * $this->b ; echo "Area of the Rectangle is $area <br>"; } } class square extends rectangle implements a { public $s; function __construct($p,$q,$r) { parent::__construct($p,$q); $this->s=$r; } function area() { parent::area(); $area=$this->s * $this->s; echo "Area of the Square is $area <br>"; } } class circle implements a { public $r; function __construct($x) { $this->r=$x; } function area() { $area=PI * $this->r * $this->r; echo "Area of the Circle is $area <br>"; } } $side=$_POST['side']; $length=$_POST['len']; $width=$_POST['width']; $radius=$_POST['radius']; $obj=new square($length,$width,$side); $obj1=new circle($radius); $obj->area(); $obj1->area(); ?>
Code language: HTML, XML (xml)

SET C

Q:1) Define an interface for stack operation. Implement this interface in a class.

PHP FILE (only php file is there)

<?php interface st_op { function push($item); function pop(); function top(); function isEmpty(); } class stack implements st_op { public $stack,$limit; function __construct($limit=10) { $this->stack=array(); $this->limit=$limit; } public function push($item) { if(count($this->stack)<$this->limit) { array_unshift($this->stack,$item); } else { die("Stack Overflow!!"); } } public function pop() { if($this->isEmpty()) { die("Stack is empty!!"); } else { return array_shift($this->stack); } } public function top() { return current($this->stack); } public function isEmpty() { return empty($this->stack); } } $obj=new stack(); $obj->push("ABC"); $obj->push("DEF"); $obj->push("GHI"); $obj->push("KLM"); echo $obj->top(); // prints KLM echo "<br>"; $obj->push("NOP"); echo $obj->pop(); //prints NOP ?>
Code language: HTML, XML (xml)

I know answers to some of the questions are missing. I will try to put them here when i manage to finish them.

If you have any questions in the codes or any suggestions to improve it, feel free to comment below.

Please note: You should use these codes just to refer and not just blatantly copy paste. You will not learn how to program in the later way.

If you are using these codes on your websites please see that you give source link as this website. Its not necessary but still I would be glad if you choose to do so.

My website : https://wintrysphere.me/
Learn PHP at tutorialspoint : https://www.tutorialspoint.com/php/index.htm

3 Comments

  1. Pingback:TYBCS PHP Assignment 5 - PHP Database (PostgreSQL) - wintrysphere

Leave a Reply

Your email address will not be published. Required fields are marked *