Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. <?php
  2.  
  3. class Box
  4. {
  5.     private $length;
  6.     private $width;
  7.     private $height;
  8.  
  9.     public function __construct
  10.     (
  11.         float $length,
  12.         float $width,
  13.         float $height
  14.     )
  15.     {
  16.         $this->setLength($length);
  17.         $this->setWidth($width);
  18.         $this->setHeight($height);
  19.     }
  20.  
  21.     /**
  22.      * @param float $length
  23.      * @throws Exception
  24.      */
  25.     private function setLength(float $length): void
  26.     {
  27.         if ($length < 1) {
  28.             throw new Exception("Length cannot be zero or negative.");
  29.         } else {
  30.             $this->length = $length;
  31.         }
  32.     }
  33.  
  34.     /**
  35.      * @param float $width
  36.      * @throws Exception
  37.      */
  38.     private function setWidth(float $width): void
  39.     {
  40.         if ($width < 1) {
  41.             throw new Exception("Width cannot be zero or negative.");
  42.         } else {
  43.             $this->width = $width;
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * @param float $height
  49.      * @throws Exception
  50.      */
  51.     private function setHeight(float $height): void
  52.     {
  53.         if ($height < 1) {
  54.             throw new Exception ("Height cannot be zero or negative.");
  55.         } else {
  56.             $this->height = $height;
  57.         }
  58.     }
  59.  
  60.     public function volumeCalculation()
  61.     {
  62.         $volumeCalculation = $this->length * $this->width * $this->height;
  63.         $volumeCalculation = round($volumeCalculation, 1);
  64.         printf("Volume - %0.2f\n", $volumeCalculation);
  65.     }
  66.  
  67.     public function surfaceAreaCalculation()
  68.     {
  69.         $surfaceArea = 2 * ($this->length * $this->width) + 2 * ($this->length * $this->height) + 2 * ($this->width * $this->height);
  70.         $surfaceArea = round($surfaceArea, 2);
  71.         printf("Surface Area - %0.2f\n", $surfaceArea);
  72.     }
  73.  
  74.     public function lateralSurfaceAreaCalculation()
  75.     {
  76.         $lateralSurface = 2 * ($this->length * $this->height) + 2 * ($this->width * $this->height);
  77.         $lateralSurface = round($lateralSurface, 2);
  78.         printf("Lateral Surface Area - %0.2f\n", $lateralSurface);
  79.     }
  80. }
  81.  
  82. $length = readline();
  83. $width = readline();
  84. $height = readline();
  85.  
  86. try {
  87.     $box = new Box($length, $width, $height);
  88. } catch (Exception $e) {
  89.     echo $e->getMessage();
  90. }
  91.  
  92. if (isset($box)) {
  93.     $box->surfaceAreaCalculation();
  94.     $box->lateralSurfaceAreaCalculation();
  95.     $box->volumeCalculation();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement