Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface Geometry {
- public function getValue ();
- public function getMass ();
- }
- class Paral implements Geometry {
- protected $a;
- protected $b;
- protected $c;
- protected $density;
- public function __construct($a, $b, $c, $density){
- $this->a = $a;
- $this->b = $b;
- $this->c = $c;
- $this->density = $density;
- }
- public function getValue (){
- return $this->a*$this->b*$this->c;
- }
- public function getMass (){
- return $this->getValue() * $this->density;
- }
- public function __toString(){
- return (string)($this->getMass());
- }
- }
- class Cube extends Paral implements Geometry {
- public function __construct($a, $density){
- $this->a = $a;
- $this->b = $a;
- $this->c = $a;
- $this->density = $density;
- }
- public function __toString(){
- return (string) parent::__toString();
- }
- }
- class Sfera implements Geometry {
- private $r;
- private $density;
- public function __construct($r, $density){
- $this->r = $r;
- $this->density = $density;
- }
- public function getValue (){
- return 4/3*(3.14*$this->r*$this->r*$this->r);
- }
- public function getMass (){
- return $this->getValue() * $this->density;
- }
- public function __toString(){
- return (string)($this->getMass());
- }
- }
- class Pyramid implements Geometry {
- private $a;
- private $b;
- private $c;
- private $h;
- private $density;
- public function __construct($a, $b, $c, $h, $density){
- $this->a = $a;
- $this->b = $b;
- $this->c = $c;
- $this->h = $h;
- $this->density = $density;
- }
- public function getValue (){
- $a = $this->a;
- $b = $this->b;
- $c = $this->c;
- $h = $this->h;
- $p = ($a+$b+$c)/2;
- $s = sqrt($p * ($p - $a) * ($p - $b)*($p - $c));
- return ($s*$h)/3;
- }
- public function getMass (){
- return $this->getValue() * $this->density;
- }
- public function __toString(){
- return (string)($this->getMass());
- }
- }
- $paral = new Paral(5,6,7,0.1);
- print "Обем и масса паралелепипеда:" . "\n";
- print $paral->getValue() . "\n";
- print $paral->getMass() . "\n";
- //print $paral;
- $cube = new Cube(5,0.1);
- print "Обем и масса куба:" . "\n";
- print $cube->getValue() . "\n";
- print $cube->getMass() . "\n";
- //print $cube;
- $sfera = new Sfera(3,0.1);
- print "Обем и масса сферы:" . "\n";
- print $sfera->getValue() . "\n";
- print $sfera->getMass() . "\n";
- $pyramid = new Pyramid(2, 3, 4, 7, 0.1);
- print "Обем и масса пирамиды:" . "\n";
- print $pyramid->getValue() . "\n";
- print $pyramid->getMass() . "\n";
- print "_______________________" . "\n" ;
- print "Рандомный выбор фигуры: \n";
- $figures = array(); //Рандомный выбор фигуры
- $figuresMass = array();
- for ($i = 0; $i < 4; $i++){
- $figures[$i]=rand(0, 3);
- switch ($figures[$i]) {
- case 0:
- $figures[$i]=new Paral(5,6,7,0.1);
- $figuresMass[$i] = $figures[$i]->getMass();
- break;
- case 1:
- $figures[$i]=new Cube(5,0.1);
- $figuresMass[$i] = $figures[$i]->getMass();
- break;
- case 2:
- $figures[$i]=new Sfera(3,0.1);
- $figuresMass[$i] = $figures[$i]->getMass();
- break;
- case 3:
- $figures[$i]=new Pyramid(2, 3, 4, 7, 0.1);
- $figuresMass[$i] = $figures[$i]->getMass();
- break;
- };
- print $i . " => ";
- print ($figuresMass[$i]);
- print "\n" ;
- }
- print "_______________________" . "\n" ;
- print "Сортированный массив: \n";
- sort($figuresMass);
- foreach ($figuresMass as $key => $value) {
- echo $key." => ".$value ."\n";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement