Rectangle Class
PHP
Easy
7 views
Problem Description
Create a Rectangle class with width and height. Print area and perimeter.
Input Format
One line: w h.
Output Format
Two integers: area perimeter.
Official Solution
<?php
class Rectangle{
public $w; public $h;
function __construct($w,$h){ $this->w=$w; $this->h=$h; }
function area(){ return $this->w*$this->h; }
function perim(){ return 2*($this->w+$this->h); }
}
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$w,$h]=array_map('intval',preg_split('/\\s+/', $inputText));
$r=new Rectangle($w,$h);
echo $r->area().' '.$r->perim();
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!