This is my first oop php library and wanted to get advice on how I can improve what I wrote. This is what I needed to do:
I'm also not sure on what it meant by "proportionally resize the shape up or down, given a floating-point scale factor" I think what I did might be right but I could be wrong. Also am I supposed to have a new class for each shape or is there a better solution? Any help is much appreciated.
<?PHP
echo "Circle (r=5) <br/>";
$circle = new circle(5);
$circle->getArea();
$circle->getPerimiter();
$circle->scale(up, .5);
echo "Scaled up .5<br/>";
$circle->getArea();
$circle->getPerimiter();
echo "<br/>Right Triangle (a=4, b=5) <br/>";
$rt = new RightTriangle(4, 5);
$rt->getArea();
$rt->getPerimiter();
$rt->scale(down, .5);
echo "Scaled down .5<br/>";
$rt->getArea();
$rt->getPerimiter();
echo "<br/>Equilateral Triangle <br/>";
$et = new EquilateralTriangle(6);
$et->getArea();
$et->getPerimiter();
$et->scale(up, .;
echo "Scaled Up .8<br/>";
$et->getArea();
$et->getPerimiter();
echo "<br/>Rectangle<br/>";
$r = new Rectangle(8, 7);
$r->getArea();
$r->getPerimiter();
$r->scale(down, .;
echo "Scaled Down .8<br/>";
$r->getArea();
$r->getPerimiter();
echo "<br/>Square<br/>";
$s = new Square(25);
$s->getArea();
$s->getPerimiter();
$s->scale(up, 2.5);
echo "Scaled Up 2.5<br/>";
$s->getArea();
$s->getPerimiter();
echo "<br/>Parallelogram<br/>";
$p = new Parallelogram(5.7, 6.;
$p->getArea();
$p->getPerimiter();
$p->scale(up, 1);
echo "Scaled Up 1<br/>";
$p->getArea();
$p->getPerimiter();
class Circle {
public function __construct( $radius )
{
$this->radius = $radius;
}
public function getArea()
{
echo number_format(pow($this->radius, 2) * M_PI, 2)."<br/>";
}
public function getPerimiter()
{
echo number_format(2 * M_PI * $this->radius, 2)."<br/>";
}
public function scale($direction, $scale)
{
if($direction == 'up')
{
$this->radius = $this->radius + ($this->radius * $scale);
}
else
{
$this->radius = $this->radius - ($this->radius * $scale);
}
}
}
class RightTriangle {
public function __construct( $a, $b )
{
$this->a = $a;
$this->b = $b;
}
public function getArea()
{
echo number_format(($this->a*$this->b/2), 2)."<br/>";
}
public function getPerimiter()
{
echo number_format($this->a + $this->b + sqrt(pow($this->a, 2) + pow($this->b, 2)), 2)."<br/>";
}
public function scale($direction, $scale)
{
if($direction == 'up')
{
$this->a = $this->a + ($this->a * $scale);
$this->b = $this->b + ($this->b * $scale);
}
else
{
$this->a = $this->a - ($this->a * $scale);
$this->b = $this->b + ($this->b * $scale);
}
}
}
class EquilateralTriangle {
public function __construct( $a )
{
$this->a = $a;
}
public function getArea()
{
echo number_format((sqrt(3)/4)*pow($this->a,2),2)."<br/>";
}
public function getPerimiter()
{
echo number_format(3 * $this->a, 2)."<br/>";
}
public function scale($direction, $scale)
{
if($direction == 'up')
{
$this->a = $this->a + ($this->a * $scale);
}
else
{
$this->a = $this->a - ($this->a * $scale);
}
}
}
class Rectangle {
public function __construct( $w, $l )
{
$this->w = $w;
$this->l = $l;
}
public function getArea()
{
echo number_format($this->w * $this->l,2)."<br/>";
}
public function getPerimiter()
{
echo number_format(2 * ($this->w + $this->l), 2)."<br/>";
}
public function scale($direction, $scale)
{
if($direction == 'up')
{
$this->w = $this->w + ($this->w * $scale);
$this->l = $this->l + ($this->l * $scale);
}
else
{
$this->w = $this->w - ($this->w * $scale);
$this->l = $this->l - ($this->l * $scale);
}
}
}
class Square {
public function __construct( $a )
{
$this->a = $a;
}
public function getArea()
{
echo number_format(pow($this->a,2),2)."<br/>";
}
public function getPerimiter()
{
echo number_format(4 * $this->a, 2)."<br/>";
}
public function scale($direction, $scale)
{
if($direction == 'up')
{
$this->a = $this->a + ($this->a * $scale);
}
else
{
$this->a = $this->a - ($this->a * $scale);
}
}
}
class Parallelogram {
public function __construct( $a, $b )
{
$this->a = $a;
$this->b = $b;
$this->h = $a/$b;
}
public function getArea()
{
echo number_format($this->b * $this->h,2)."<br/>";
}
public function getPerimiter()
{
echo number_format(2 * ($this->a + $this->b), 2)."<br/>";
}
public function scale($direction, $scale)
{
if($direction == 'up')
{
$this->a = $this->a + ($this->a * $scale);
$this->b = $this->b + ($this->b * $scale);
}
else
{
$this->a = $this->a - ($this->a * $scale);
$this->b = $this->b - ($this->b * $scale);
}
}
}
?>