PHP Object-Oriented Programming exercise: Calculate the perimeter and area of a rectangle, triangle, circle

Source: Internet
Author: User
Tags php tutorial


Just finished learning PHP object-oriented programming, referring to the Gaulo Teacher's PHP tutorial to learn this example.

Chip



Here is the implementation code:

index.php


Form.class.php_ This is the form class _

<?php//returns different form class form{private $action according to the value of the different action submitted in index;        Private $shape;            Constructor method function __construct ($action = "") {$this->action = $action;        $this->shape=isset ($_request["action"])? $_request["Action"]: "rect";            } function __tostring () {//Todo:implement __tostring () method.  $form = ' <form action= '. $this->action. ' "            Method= "POST" > ";                    Switch ($this->shape) {case "rect": $form. = $this->getrect ();                Break                    Case "triangle": $form. = $this->gettriangle ();                Break                    Case "Circle": $form. = $this->getcircle ();                Break            Default: $form. = ' Please select a shape <br> ';            } $form. = ' <input type= "Submit" Name= "sub" value= "Calculation" > ";   $form. = ' </form> ';         return $form;            } Private Function GetRect () {$input = ' <b> Please enter | rectangle | Width and height:</b><p> '; $input. = ' width: <input type= "text" name= "width" value= "'. $_post[' width ']. '"            ><br> '; $input. = ' Height: <input type= "text" name= "height" value= "'. $_post[' height ']. '"            ><br> ';            $input. = ' <input type= ' hidden ' name= ' action ' value= ' rect ' > ';        return $input;            } Private Function Gettriangle () {$input = ' <b> Please enter | triangle | of three-side:</b><p> ';            $input. = ' first side: <input type= ' text "name=" side1 "value=" '. $_post[' Side1 ']. ' ><br> ';            $input. = ' second side: <input type= "text" name= "Side2" value= "'. $_post[' Side2 ']. ' ><br> ';            $input. = ' third side: <input type= "text" name= "side3" value= "'. $_post[' Side3 ']. ' ><br> ';            $input. = ' <input type= ' hidden ' name= ' action ' value= ' triangle ' > ';        return $input; } private function Getcircle () {$input = ' <b> Please enter | circle | radius:</b><p> ';            $input. = ' Radius: <input type= "text" name= "radius" value= "'. $_post[' radius ']. ' ><br> ';            $input. = ' <input type= ' hidden ' name= ' action ' value= ' Circle ' > ';        return $input; }}/** * Created by Phpstorm. * User:user * DATE:2018/4/15 * time:16:26 * */

Shape.class.php This is an abstract class used to define the canonical

<?phpabstract class Shape {public    $shapeName;    The area (), perimeter () method must be in the canonical circle, Triangle, and rect to    abstract function area ();    Abstract function perimeter ();    Public Function Setshapename ($shapeName)    {        $this->shapename = $shapeName;        return $this;    }    Determines whether the number entered is a valid number greater than 0    protected function validate ($value, $message = "shape") {        if ($value = = "" "| |! Is_numeric ($value) | | $value < 0) {            echo ' <font color= ' red ' > '. $message. ' must be a non-negative number and cannot be empty </font><br> ';            return false;        } else {            return true;}}    } /** * Created by Phpstorm. * User:user * DATE:2018/4/15 * time:16:42 * *

CIRCLE.CLASS.PHP_ is the calculation of the circumference and area of the formula _

<?phpclass Circle extends Shape {    private $radius =0;    function __construct () {        $this->shapename= "Circle";        if ($this->validate ($_post[' radius '), ' radius of circle ') {            $this->radius=$_post["radius"];        } else{            exit;        }    }    function area () {        return pi () * $this->radius* $this->radius;    }    function perimeter () {        return 2*pi () * $this->radius;    }} /** * Created by Phpstorm. * User:user * DATE:2018/4/15 * time:17:06 * *

rect.class.php

<?phpclass Rect extends shape{    private $width =0;    Private $height =0;    function __construct ()    {        $this->shapename= "Rectangle";        if ($this->validate ($_post["width"], ' rectangle widths ') & $this->validate ($_post["height"], ' rectangle '))        {            $ this->width=$_post["width"];            $this->height=$_post["height"];        }        else{            exit;        }    }    function area () {        return $this->width* $this->height;    }    function perimeter ()    {        return 2 * ($this->width + $this->height);}    } /** * Created by Phpstorm. * User:user * DATE:2018/4/15 * time:17:02 * *

triangle.class.php

<?phpclass Triangle extends shape{private $side 1=0;    Private $side 2=0;    Private $side 3=0;        function __construct () {$this->shapename= "triangle";        if ($this->validate ($_post[' side1 '), ' first edge of triangle ') {$this->side1=$_post["side1"];        } if ($this->validate ($_post[' Side2 '), ' second side of the triangle ') {$this->side2=$_post["side2"];        } if ($this->validate ($_post[' side3 '), ' third edge of triangle ') {$this->side3=$_post["side3"];            } if (! $this->validatesum ()) {echo ' <font color= ' red ' > triangle must be greater than the third side </font> ';        Exit        }} function area () {$s = ($this->side1+ $this->side2+ $this->side3)/2;    return sqrt ($s * ($s-$this->side1) * ($s-$this->side2) * ($s-$this->side3));    } function perimeter () {return $this->side1+ $this->side2+ $this->side3; } Private Function Validatesum () {$condition 1 = ($this->side1 + $this->side2) > $this->side3;        $condition 2 = ($this->side1 + $this->side3) > $this->side2;        $condition 3 = ($this->side2 + $this->side3) > $this->side1;        if ($condition 1 && $condition 2 && $condition 3) {return true;        } else {return false; }}}/** * Created by Phpstorm. * User:user * DATE:2018/4/15 * time:17:04 * *

Result.class.php_ Here is the return calculation result class _

<?php class result{Private $shape; function __construct () {switch ($_post[' action ') {case ' rect ': $this-                    >shape=new Rect ();                Break                    Case ' triangle ': $this->shape=new triangle ();                Break                    Case ' Circle ': $this->shape=new circle ();                Break            Default: $this->shape=false; }}/** * @return String */function __tostring () {//Todo:implem            Ent __tostring () method. if ($this->shape) {$result = $this->shape->shapename. ' Perimeter: '. $this->shape->perimeter (). '                <br> '; $result. = $this->shape->shapename. ' Area: '. $this->shape->area (). '                <br> ';            return $result;            }else{return ' without this shape ';   }     }}/** * Created by Phpstorm. * User:user * DATE:2018/4/15 * time:16:47 * *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.