Sobes.tech
Junior — Senior

Detection and correction of errors in the calculation of figure areas

livecode

Task condition

In this task, you need to carefully analyze the provided code snippet, identify logical and syntactical errors, and suggest ways to fix them. Special attention should be paid to the absence of the necessary class, incorrect use of variables, and improper return of results. Propose a more reliable implementation that adheres to OOP principles and polymorphism.

class Shape
{
    public $w;
    public $h;

    public function __construct($w, $h)
    {
        $this->w = $w;
        $this->h = $h;
    }
}

class Triangle
{
    public $r;

    public function __construct($r)
    {
        $this->r = $r;
    }
}
class AreaCalculator
{
    public
    function calculate($figures)
    {
        $areas = [];
        foreach ($figures as $figure) {
            if (is_a($figure, 'Square')) {
                $areas[] = $figure->w * $figure->h;
            } else if (is_a($figure, 'Triangle')) {
                $areas[] = $figure->r * $figure->r * pi();
            }
        }
        return array_sum($areas);
    }
}