TY_BCA_AWT_SLIP 4_1




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



html file :
<html>
<body>
<form action="slip_4_Q3.php" method=get>
<center>
<table>
<tr><td>Enter No1</td><td><input type="text" name="a"></td></tr>
<tr><td>Enter No2</td><td><input type="text" name="b"></td></tr>
<tr><td></td><td><input type="submit" value="SUBMIT"></td></tr>
</table>
</center>
</form>
</body>
</html
php file :
<?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($x,$y);
$calc->add();
$calc->subtract();
$calc->multiply();
$calc->div();

?>

3 comments:

Note: only a member of this blog may post a comment.