Jump to content

PHP shape library


googlexx

Recommended Posts

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);
}
}
}
?>

 

Link to comment
Share on other sites

1. All of these classes are shapes, right? And they all have the same set of methods, like getArea and getPerimeter (notice the spelling) and scale, right? Making each class completely distinct does not fit into the idea of "all of these are shapes", and simply using methods with matching names does not work for "the same set of methods". Look into inheritance and polymorphism.

abstract class Shape {

	public abstract function getArea();

	public abstract function getPerimeter();

	public abstract function scale($scale);

}
2a. The scaling can (should) be done merely with a number. 1.0 is the same size, >1.0 is larger,

2b. Writing up or down tells PHP to look for a constant with that name. If you want a string then use quotes.

3. "a" and "b" and "w" and "l" are not appropriate variable names for things that matter, like the length of a side of a square or of the width of a rectangle. Use good names like "width".

4. A method called "get*" should get a value. As in return something. It should never output anything. And they shouldn't try to format numbers either.

5. Put your classes in separate files. You should learn about autoloading classes at this point, but requireing each file is okay for such a small assignment.

Link to comment
Share on other sites

Hopefully, this makes requinix' comments a little more clear.

 

Shape.php

abstract class Shape {

    public abstract function getArea();

    public abstract function getPerimeter();

    public abstract function scale($scale);

}

Rectangle.php

class Rectangle extends Shape {
    
    private $width;
    private $height;
    
    public function __construct($width, $height)
    {
        $this->width = $width;
        $this->height = $height;
    }
    
    public function getArea()
    {
        return $this->width * $this->height;
    }
    
    public function getPerimeter()
    {
        return $this->width * 2 + $this->height * 2;
    }
    
    public function scale($scale)
    {
        $this->width *= $scale;
        $this->height *= $scale;
    }
}

index.php

error_reporting(E_ALL);
ini_set('display_errors',1);

define('br','<br />');

$rect = new Rectangle(3,4);

echo "Area of 3 x 4 rectangle is: {$rect->getArea()}" . br;
echo "Perimeter of 3 x 4 rectangle is: {$rect->getPerimeter()}" . br;
echo '-------------------' . br;
echo "After scaling a 3 x 4 rectangle by a factor of 2" . br;
echo '-------------------' . br;
$rect->scale(2);
echo "Area of scaled rectangle is: {$rect->getArea()}" . br;
echo "Perimeter of scaled rectangle is: {$rect->getPerimeter()}" . br;
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.