Slip 9 - A) Write a PHP program to create a Class Calculator which will accept two values from user and pass as an argument through parameterized constructor and do the following task: a) Add b) Subtract c) Multiply them together or divide them on request.

 Solution:

<?php
class Calculate
{
public $a;
public $b;
function __construct($a,$b){
$this->a=$a;
$this->b=$b;
}
public function add()
         {
          $c=$this->a+$this->b;
echo"Addition = $c<br>";
         }
public function subtract()
         {
          $c=$this->a-$this->b;
echo"Subtract = $c<br>";
         }
public function multiply()
         {
          $c=$this->a*$this->b;
echo"Multiplication = $c<br>";
         }
public function div()
         {
          $c=$this->a/$this->b;
echo"Division = $c";
         } }
//$x=$_GET['a'];
//$y=$_GET['b'];
$calc=new Calculate(4,3);
$calc->add();
$calc->subtract();
$calc->multiply();
$calc->div();?>

Post a Comment

0 Comments