Jump to content

I'm in a bit of a JAM need a PHP superhero to help LOL


Fiyero1988

Recommended Posts

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.

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;
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.