PDA

View Full Version : Move method in php



hale88
12-13-2009, 07:57 PM
what is move method in CLASS?

I tried to search in google but no luck:


"Create a move method in the Shape class that accepts two values. These values are added to x and y attributes to simulate moving the shape."

this is the code I got so far (please fix me if something is wrong with this code):

class shape {
private $name;
private $x;
private $y;

function __construct($x , $y) {
$this->X = $x;
$this->y = $y;
}
public function setName($name) {
$this-> name = $name ;
}
public function getName() {
return $this-> name ;
}
public function setX($x) {
$this-> x = $x ;
}

public function getX(){
return $this-> x ;
}
public function setY($y){
$this-> Y = $y;
}
public function getY(){
return $this-> Y ;
}

thanks
hale

hale88
12-13-2009, 10:14 PM
here are some update. I have to create a Shape.php, Circle.php, and Square.php and yes I did, here is the code: However, I cannot get them run for some reason ( I still need MOVE METHOD)

Testing code:

require_once('Shape.php');
require_once('Circle.php');
require_once('Square.php');

$myCircle = new Circle("aCircle", 2, 3, 5);
$mySquare = new Square("aSquare", 0, 0, 4);

echo $mySquare->getName();
echo "<br />";
echo "Area of square is ";
echo $mySquare->area();
echo "<br />";

$mySquare-> move(23, 45);
echo "Position of square is x = ";
echo $mySquare->getX();
echo " y = ";
echo $mySquare->getY();
$mySquare->setSide(6);
echo "<br />";
echo "Area of square is ";
echo $mySquare->area();
echo "<br />";
echo $myCircle->getName();
echo "<br />";
echo "Area of circle is ";
echo $myCircle->area();
echo "<br />";
$myCircle->move(15, 20);
echo "Position of circle is x = ";
echo $myCircle->getX();
echo " y = ";
echo $myCircle->getY();
$myCircle->setRadius(-2);
echo "<br />";
echo "Area of circle is ";
echo $myCircle->area();

Shape:
class Shape {
private $name;
private $X;
private $Y;

function move ($X,$Y) {
return $this->X;
return $this->Y;
}

function __construct($x,$y) {
$this->X = $x;
$this->Y = $y;
}
public function setName($name) {
$this-> name = $name ;
}
public function getName() {
return $this-> name ;
}

public function setX($X) {
$this-> name = $X ;
}
public function getX() {
return $this-> X ;
}

public function setY($Y) {
$this-> name = $Y;
}
public function getY() {
return $this-> Y ;
}

}

Circle:
require_once ('Shape.php');
class Circle extends Shape {

private $radius;


function __construct($name,$x,$y,$radius) {
Shape::__construct($name,$x,$y);
$this->radius = $radius;


}
public function setradius($radius) {
if(is_numeric($radius) && $radius > 0 ) {
$this->radius = $radius;
}
if(is_numeric($radius) && $radius < 0 ) {
$this->radius = 0;
}
}
public function getradius($radius) {
return $this-> radius ;
}

function getarea() {
return 3.14159 * $this-> radius * $this-> radius;
}


}
$myCircle = new Circle("aCircle", 2, 3, 5);
echo $myCircle->getradius();
echo "<br />";
echo $myCircle->getName();
?>

Square:

require_once ('Shape.php');
class Square extends Shape {

private $side;


function __construct($name,$x,$y,$side) {
Shape::__construct($name,$x,$y);
$this->side = $side;


}
public function setSide($side) {
if(is_numeric($side) && $side > 0 ) {
$this->side = $side;
}
if(is_numeric($side) && $side < 0 ) {
$this->side = 0;
}
}
public function getSide($side) {
return $this-> side ;
}

function getarea() {
return $this-> side * $this-> side;
}

}
$mySquare = new Square("aSquare", 0, 0, 4);
echo $mySquare->getSide();
echo "<br />";
echo $mySquare->getName();

s1nykuL
12-14-2009, 03:03 AM
The braces in your code are mismatched, getradius and getarea are outside your circle class, getside and getarea are outside your square class.

You do not have the php delimiters set up properly there is no opening "<? php" in the code only the closing "?>"


As for a move function, what you need to do is change the x,y co-ordinates for the shapes. You can write functions that either set new x,y co-ordinates and discard the old co-ordinates or add values to the existing x,y co-ordinates. Setting new x,y co-ordinates would be the easiest approach, however if the calling code can only provide changes in co-ordinates and not absolute co-ordinates simply setting new co-ordinates will not work.

I would write a function that did both;replace x,y with new values or change x,y via addition. You could pass a flag(1 or zero, true or false) to this function to control which approach is used within the function.

something like this

function move(newX, newY, flag) {

if flag { //replace old with new co-ordinates
x= newX;
y=newY;
}
else //change existing co-ordinates;negative values of newX or newY would be the equivalent of moving the co-ordinates left or down)
x+=newX;
y+=newY;
}

Did you get your temp conversion code working?

hale88
12-15-2009, 08:46 PM
I left the Function Temperature assign since I need to figure out how it works. However, this project is more interesting:

here the three separate files and A index.php for testing these three files:

1. Shape:

<?php

class Shape {

protected $name;
protected $x;
protected $y;


function __construct($x,$y) {
$this->x = $x;
$this->y = $y;
}
public function setName($name) {
$this-> name = $name ;
}
public function getName() {
return $this-> name ;
}

public function setX($x) {
$this-> name = $x ;
}
public function getX() {
return $this-> x ;
}

public function setY($y) {
$this-> name = $y;
}
public function getY() {
return $this-> y ;
}

public function move($x, $y) {
$this->x += $x;
$this->y += $y;
}

}

2. Circle:

<?php

require_once ('Shape.php');

class Circle extends Shape {

private $radius;


function __construct($name,$x,$y,$radius) {
Shape::__construct($name,$x,$y);
$this->radius = $radius;


}
public function setradius($radius) {
if(is_numeric($radius) && $radius > 0 ) {
$this->radius = $radius;
}
else {
$this->radius = 0;
}
}
public function getradius($radius) {
return $this-> radius ;
}

function area($radius) {
return 3.14159 * $this->radius * $this->radius;
}


}

?>

3. Square:

<?php

require_once ('Shape.php');

class Square extends Shape {

private $side;

public function __construct($name, $x, $y, $side) {
$this->name = $name;
$this->x = $x;
$this->y = $y;
$this->side = $side;

}
public function setSide($side) {
if(is_numeric($side) && $side > 0 ) {
$this->side = $side;
}
else {
$this->side = 0;
}
}
public function getSide($side) {
return $this-> side ;
}

function area($side) {
return $this-> side * $this-> side;
}

}

?>

4. Testing index.php Code:

<?php

require_once('Shape.php');
require_once('Circle.php');
require_once('Square.php');

$myCircle = new Circle("aCircle", 2, 3, 5);
$mySquare = new Square("aSquare", 0, 0, 4);

echo $mySquare->getName();
echo "<br />";
echo "Area of square is ";
echo $mySquare->area();
echo "<br />";

$mySquare-> move(23, 45);
echo "Position of square is x = ";
echo $mySquare->getX();
echo " y = ";
echo $mySquare->getY();
$mySquare->setSide(6);
echo "<br />";
echo "Area of square is ";
echo $mySquare->area();
echo "<br />";
echo $myCircle->getName();
echo "<br />";
echo "Area of circle is ";
echo $myCircle->area();
echo "<br />";
$myCircle->move(15, 20);
echo "Position of circle is x = ";
echo $myCircle->getX();
echo " y = ";
echo $myCircle->getY();
$myCircle->setRadius(3);
echo "<br />";
echo "Area of circle is ";
echo $myCircle->area();
?>

s1nykuL
12-30-2009, 05:43 AM
I have been busy and without Internet access, so I appologise for the delay. I expect you have worked out by now that that your area functions within each shape class require an argument "$side" and when you are calling the area functions from index.php you are not sending that argument and you really don't need to as area can be calculated from the parameters you used to create the shape.

Come on I aint gonna fix anything for you ;) I will help you to fix it yourself though.

I put your code on my LAMP server and this was displayed in my browser when I opened index.php

aSquare
Area of square is 16
Position of square is x = 23 y = 45
Area of square is 36

Area of circle is 78.53975
Position of circle is x = 15 y = 22
Area of circle is 28.27431

see it in action: http://www.propergander.org.uk/shapetest/index.php

Running from the command line this was the output:

user@server:/var/www/shapetest$ php index.php

aSquare<br />Area of square is
Warning: Missing argument 1 for Square::area(), called in /var/www/shapetest/index.php on line 13 and defined in /var/www/shapetest/Square.php on line 28
16<br />Position of square is x = 23 y = 45<br />Area of square is
Warning: Missing argument 1 for Square::area(), called in /var/www/shapetest/index.php on line 24 and defined in /var/www/shapetest/Square.php on line 28
36<br /><br />Area of circle is
Warning: Missing argument 1 for Circle::area(), called in /var/www/shapetest/index.php on line 29 and defined in /var/www/shapetest/Circle.php on line 28
78.53975<br />Position of circle is x = 15 y = 22<br />Area of circle is
Warning: Missing argument 1 for Circle::area(), called in /var/www/shapetest/index.php on line 39 and defined in /var/www/shapetest/Circle.php on line 28
28.27431

Sorry I took so long to reply. Out of curiosity what OS are you doing your coding on?

ps your shape class does not have the closing "?>" either :)

hale88
12-30-2009, 12:01 PM
thanks I just got my problem solved from my instructor ;). I was missing the input for the Area->created error

s1nykuL
12-31-2009, 08:55 AM
np, good luck :)