Ejemplo n.º 1
0
 function testGetArea()
 {
     // Need a Rectangle:
     $r = new Rectangle(8, 9);
     // The assertion tests the math:
     $this->assertEquals(72, $r->getArea());
 }
Ejemplo n.º 2
0
    {
        return $width * $height;
    }
    public function getPerimeter($width, $height)
    {
        return $width + $height;
    }
    public function isSquare()
    {
        return $this->width == $this->height;
    }
}
$width = 160;
$height = 75;
echo "<h2>With a width of {$width} and a height of {$height}...</h2>";
$r = new Rectangle($width, $height);
echo '<p>The area of the rectangle is ' . $r->getArea($width, $height) . '</p>';
echo '<p>The perimeter of the rectangle is ' . $r->getPerimeter($width, $height) . '</p>';
echo '<p>This rectangle is ';
if ($r->isSquare()) {
    echo 'also';
} else {
    echo 'not';
}
echo ' a square.</p>';
?>

</p>

</body>
</html>
Ejemplo n.º 3
0
<?php

require_once __DIR__ . "/../vendor/autoload.php";
require_once __DIR__ . "/../src/Rectangle.php";
$app = new Silex\Application();
$app->get("/", function () {
    return "Home";
});
$app->get("/new_rectangle", function () {
    return "<!DOCTYPE html>\n        <html>\n        <head>\n            <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css'>\n            <title>Make a rectangle!</title>\n        </head>\n        <body>\n            <div class='container'>\n                <h1>Geometry Checker</h1>\n                <p>Enter the dimensions of your rectangle to see if it's a square.</p>\n                <form action='/view_rectangle'>\n                    <div class='form-group'>\n                      <label for='length'>Enter the length:</label>\n                      <input id='length' name='length' class='form-control' type='number'>\n                    </div>\n                    <div class='form-group'>\n                      <label for='width'>Enter the width:</label>\n                      <input id='width' name='width' class='form-control' type='number'>\n                    </div>\n                    <button type='submit' class='btn-success'>Create</button>\n                </form>\n            </div>\n        </body>\n        </html>";
});
$app->get("/view_rectangle", function () {
    $my_rectangle = new Rectangle($_GET['length'], $_GET['width']);
    $area = $my_rectangle->getArea();
    if ($my_rectangle->isSquare()) {
        return "<h1>Congratulations! You made a square! Its area is {$area}.</h1>\n                    <a href='/new_rectangle'>Back!</a>";
    } else {
        return "<h1>Sorry! This isn't a square. Its area is {$area}.</h1>\n            <a href='/new_rectangle'>Back!</a>";
    }
});
return $app;
<?php

require_once "rectangle.php";
require_once "square.php";
$rect1 = new Rectangle(9, 8);
echo "The area of rectangle 1 is: " . $rect1->getArea() . PHP_EOL;
$rect2 = new Rectangle(4, 5);
echo "The area of rectangle 2 is: " . $rect2->getArea() . PHP_EOL;
$square1 = new Square(5);
echo "The perim of square 1 is: " . $square1->getPerimeter() . PHP_EOL;
$square2 = new Square(4);
echo "The perim of square 2 is: " . $square2->getPerimeter() . PHP_EOL;
<?php

// PHP exercise about extending classes from a parent class. Rectangle is the parent class.
// Square is the child class extending off of Rectangle.
// This file runs in the command line.
// Because Square.php requires Rectangle.php, this file has access to both.
require_once 'Square.php';
$rectangle = new Rectangle(5, 10);
echo 'Rectangle area: ' . $rectangle->getArea() . PHP_EOL;
echo 'Rectangle perimeter: ' . $rectangle->getPerimeter() . PHP_EOL;
$square = new Square(9);
echo 'Square area: ' . $square->getArea() . PHP_EOL;
echo 'Square perimeter: ' . $square->getPerimeter() . PHP_EOL;
Ejemplo n.º 6
0
    }
    public function getPerimeter()
    {
        return $this->width * 2 + $this->height * 2;
    }
    public function isSquare()
    {
        if ($this->width == $this->height) {
            return true;
        }
    }
}
$width = 160;
$height = 75;
echo "<h2>With a width of {$width} and a height of {$height}...</h2>";
$r = new Rectangle($width, $height);
echo '<p>The area of the rectangle is ' . $r->getArea() . '</p>';
echo '<p>The perimeter of the rectangle is ' . $r->getPerimeter() . '</p>';
echo '<p>This rectangle is ';
if ($r->isSquare()) {
    echo 'also';
} else {
    echo 'not';
}
echo ' a square.</p>';
?>

</p>

</body>
</html>
Ejemplo n.º 7
0
        }
    }
    function getArea()
    {
        return $this->length * $this->width;
    }
}
$my_rectangle = new Rectangle();
$my_rectangle->length = $_GET["length"];
$my_rectangle->width = $_GET["width"];
?>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <title>Make a Rectangle</title>
</head>
<body>
    <div class="container">
    <?php 
if ($my_rectangle->isSquare()) {
    echo "<h1>Congratulations! You made a square!</h1>";
} else {
    echo "<h1>Sorry! This isn't a square</h1>";
}
echo "<p>The area is: " . $my_rectangle->getArea() . "</p>";
?>
    </div>
</body>
</html>
Ejemplo n.º 8
0
<?php

require_once "rectangle.php";
require_once "square.php";
$rectangle = new Rectangle(15, 5);
$square = new Square(10);
echo $rectangle->getArea() . PHP_EOL;
echo $square->getArea() . PHP_EOL;
echo $square->getPerimeter() . PHP_EOL;
Ejemplo n.º 9
0
 */
// Include the class definition:
// require('Rectangle.php');
function __autoload($class)
{
    require $class . '.php';
}
// Define the necessary variables:
$width = 160;
$height = 75;
// Print a little introduction:
echo "<h2>With a width of {$width} and a height of {$height}...</h2>\n";
// Create a new object:
$r = new Rectangle($width, $height);
// Print the area.
echo '<p>The area of the rectangle is ' . $r->getArea() . "</p>\n";
// Print the perimeter.
echo '<p>The perimeter of the rectangle is ' . $r->getPerimeter() . '</p>' . PHP_EOL;
// Is this a square?
echo '<p>This rectangle is ';
if ($r->isSquare()) {
    echo 'also';
} else {
    echo 'not';
}
echo ' a square.</p>' . PHP_EOL;
// Delete the object:
unset($r);
?>
</body>
</html>
Ejemplo n.º 10
0
	宽度:<input type="text" name="width" > <br/>
	高度:<input type="text" name="height" > <br/>
	<input type="submit" name="btn" value="计算">
	</form>
</body>
</html>

<?php 
include 'file_7_extraTraining.php';
class Rectangle extends Shape
{
    private $width, $height;
    function __construct($width, $height)
    {
        $this->width = $width;
        $this->height = $height;
    }
    function getArea()
    {
        return $this->width * $this->height;
    }
    function getPerimeter()
    {
        return 2 * $this->width + 2 * $this->height;
    }
}
if (isset($_POST["btn"])) {
    $rect1 = new Rectangle($_POST["width"], $_POST["height"]);
    echo "矩形的周长:" . $rect1->getPerimeter() . "<br/>";
    echo "矩形的面积:" . $rect1->getArea() . "<br/>";
}
Ejemplo n.º 11
0
<?php

require_once 'square.php';
$rectangle = new Rectangle(4, 5);
echo 'The area is: ' . $rectangle->getArea() . PHP_EOL;
$square = new Square(4);
echo 'The area is: ' . $square->getArea() . PHP_EOL;
echo 'The perimeter is: ' . $square->perimeter() . PHP_EOL;