0% found this document useful (0 votes)
3 views5 pages

SDL Practical 5

The document contains a PHP code that defines an abstract class 'Shape' and three derived classes: 'Triangle', 'Square', and 'Circle', each implementing a method to calculate their area. It also includes an HTML form allowing users to select a shape and input dimensions to calculate the area. Upon form submission, the corresponding shape object is created, and the area is calculated and displayed based on the user input.

Uploaded by

sushantasole21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

SDL Practical 5

The document contains a PHP code that defines an abstract class 'Shape' and three derived classes: 'Triangle', 'Square', and 'Circle', each implementing a method to calculate their area. It also includes an HTML form allowing users to select a shape and input dimensions to calculate the area. Upon form submission, the corresponding shape object is created, and the area is calculated and displayed based on the user input.

Uploaded by

sushantasole21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Code :

<?php
abstract class Shape {
abstract public function getArea();
}

class Triangle extends Shape {


private $base, $height;

public function __construct($base, $height) {


$this->base = $base;
$this->height = $height;
}

public function getArea() {


return 0.5 * $this->base * $this->height;
}
}

class Square extends Shape {


private $side;

public function __construct($side) {


$this->side = $side;
}

public function getArea() {


return $this->side * $this->side;
}
}

class Circle extends Shape {


private $radius;

public function __construct($radius) {


$this->radius = $radius;
}

public function getArea() {


return pi() * pow($this->radius, 2);
}
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Area Calci</title>
</head>
<body>
<h2>Shape Selection</h2>
<form method="POST">
<input type="radio" name="shape" value="Triangle" required> Triangle
<input type="radio" name="shape" value="Square"> Square
<input type="radio" name="shape" value="Circle"> Circle</br><br>
Enter Dimensions:<br></br>
<input type="number" name="val1" placeholder="Enter first value" required>
<input type="number" name="val2" id="val2" placeholder="Enter second value (if needed)">
<input type="submit" name="submit" value="Calculate Area">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$shapeType = $_POST['shape'];
$val1 = $_POST['val1'];
$val2 = isset($_POST['val2']) ? $_POST['val2'] : null;
$shape = null;

switch ($shapeType) {
case "Triangle":
if ($val2) {
$shape = new Triangle($val1, $val2);
} else {
echo "Please enter both base and height for the triangle.";
}
break;
case "Square":
$shape = new Square($val1);
break;
case "Circle":
$shape = new Circle($val1);
break;
}

if ($shape) {
echo "<h3>Area of $shapeType: " . $shape->getArea() . "</h3>";
}
}
?>
</body>
</html>

You might also like