Slip 5 - A) Create an abstract class Shape with methods area( ) and volume( ). Derive three classes rectangle (length, breath), Circle(radius) and Cylinder(radius, height), Calculate area and volume of all. (Use Method overriding).

 Solution:

slip5-p2-q1.php

<?php
define('pi',3.14);
abstract class shape
{
 abstract function calc_area($r,$h);
 abstract function calc_vol($r,$h);
}
class circle extends shape
{
  function calc_area($r,$a)
  {
              return pi*$r*$a;       
  }

}

class cylinder extends shape
{
  function calc_area($r,$h)
  {
              return 2*pi*$r*($r+$h);          
  }

  function calc_vol($r,$h)
  {
              return pi*$r*$r*$h;
  }
}

class rectangle extends shape
{
  function calc_area($r,$h)
  {
              return $r*$h;   
  }

}
$r=$_GET['r'];
$h=$_GET['h'];
$a=$r;
$ob=new rectangle();
echo "Area of rectangle ".$ob->calc_area($r,$h);
echo "</br>";
$ob=new cylinder();
echo "Area of cylinder ".$ob->calc_area($r,$h);
echo "</br>";
echo "Volume of cylinder".$ob->calc_vol($r,$h);
echo "</br>";
$ob=new circle();
echo "Area of circle ".$ob->calc_area($r,$a);
echo "</br>";
?>

slip5-p1-q1.html

<html>
<body>
<form action="slip5-p2-q1.php" method=get>
<center><h2>Enter values for Cone & Cylinder</h2>
<p>Enter Radius </td><td><input type="text" name="r"><br>
<p>Enter Height</td><td> <input type="text" name="h"><br>
<p><input type="submit" value="calculate">
</form>
</body>
</html>

Post a Comment

5 Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. x = $x;
    $this->y = $y;
    }

    function area()
    {
    return $this->x * $this->y;
    }

    function volume()
    {
    return 0;
    }
    }

    class Cylinder extends Shape
    {
    function __construct($x, $y)
    {
    $this->x = $x;
    $this->y = $y;
    }

    function area()
    {
    return PI * $this->x * $this->x;
    }

    function volume()
    {
    return FOUR_THIRDS * PI * ($this->x * $this->x);
    }
    }

    class Circle extends Shape
    {
    private $radius;

    function __construct($radius)
    {
    $this->radius = $radius;
    }

    function area()
    {
    return PI * $this->radius * $this->radius;
    }

    function volume()
    {
    return 0; //
    }
    }

    ReplyDelete