Fiyero1988 Posted December 13, 2009 Share Posted December 13, 2009 Ok so im being asked to create the following in PHP: Create a class Rectangle. The class has private member variables length and width. It has set and get methods for both length and width. The set methods should verify that the length and width are numbers larger than 0 and less than 20. It has a method to calculate the area. This method returns the result of multiplying length time width. Create an instance of the Rectangle class test each method. Here is an example of testing the get and set methods for length: $myRect‐>setLength(4); echo $myRect‐>getLength(); I can't for the life of me write this script by far the hardest thing for me to figure out. Link to comment https://forums.phpfreaks.com/topic/184946-im-in-a-bit-of-a-jam-need-a-php-superhero-to-help-lol/ Share on other sites More sharing options...
ngreenwood6 Posted December 13, 2009 Share Posted December 13, 2009 here you go. i have left a little implementation for you but here is the basics. <?php //create the rectangle object $rectangle = new Rectangle(); //set the length and width $rectangle->setLength(3); $rectangle->setWidth(20); //get the area $area = $rectangle->calculateArea(); //display the area echo $area; class Rectangle{ private $length; private $width; public function setLength($length){ if(is_numeric($length) && $length > 0 && $length < 20){ $this->length = $length; } else { //do something here if its not a number, less than 0 or greater than 20 } } public function setWidth($width){ if(is_numeric($width) && $width > 0 && $width < 20) { $this->width = $width; } else { //do something here if its not a number, less than 0 or greater than 20 } } public function getLength(){ return $this->length; } public function getWidth(){ return $this->width; } public function calculateArea(){ return $this->length * $this->width; } } ?> Link to comment https://forums.phpfreaks.com/topic/184946-im-in-a-bit-of-a-jam-need-a-php-superhero-to-help-lol/#findComment-976355 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.