Slip 3 - A) Write a Calculator class that can accept two values, then add, subtract, multiply them or divide them on request. For example: $calc = new Calculator( 3, 4 ); echo $cale- >add(); // Displays “7” echo $calce- >multiply(); // Displays “12”

 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