Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Box
- {
- private $length;
- private $width;
- private $height;
- public function __construct
- (
- float $length,
- float $width,
- float $height
- )
- {
- $this->setLength($length);
- $this->setWidth($width);
- $this->setHeight($height);
- }
- /**
- * @param float $length
- * @throws Exception
- */
- private function setLength(float $length): void
- {
- if ($length < 1) {
- throw new Exception("Length cannot be zero or negative.");
- } else {
- $this->length = $length;
- }
- }
- /**
- * @param float $width
- * @throws Exception
- */
- private function setWidth(float $width): void
- {
- if ($width < 1) {
- throw new Exception("Width cannot be zero or negative.");
- } else {
- $this->width = $width;
- }
- }
- /**
- * @param float $height
- * @throws Exception
- */
- private function setHeight(float $height): void
- {
- if ($height < 1) {
- throw new Exception ("Height cannot be zero or negative.");
- } else {
- $this->height = $height;
- }
- }
- public function volumeCalculation()
- {
- $volumeCalculation = $this->length * $this->width * $this->height;
- $volumeCalculation = round($volumeCalculation, 1);
- printf("Volume - %0.2f\n", $volumeCalculation);
- }
- public function surfaceAreaCalculation()
- {
- $surfaceArea = 2 * ($this->length * $this->width) + 2 * ($this->length * $this->height) + 2 * ($this->width * $this->height);
- $surfaceArea = round($surfaceArea, 2);
- printf("Surface Area - %0.2f\n", $surfaceArea);
- }
- public function lateralSurfaceAreaCalculation()
- {
- $lateralSurface = 2 * ($this->length * $this->height) + 2 * ($this->width * $this->height);
- $lateralSurface = round($lateralSurface, 2);
- printf("Lateral Surface Area - %0.2f\n", $lateralSurface);
- }
- }
- $length = readline();
- $width = readline();
- $height = readline();
- try {
- $box = new Box($length, $width, $height);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- if (isset($box)) {
- $box->surfaceAreaCalculation();
- $box->lateralSurfaceAreaCalculation();
- $box->volumeCalculation();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement