Results 1 to 7 of 7

Thread: Move method in php

  1. #1
    Xtreme Member
    Join Date
    Jul 2008
    Location
    Somewhere in FL
    Posts
    246

    Move method in php

    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
    Last edited by hale88; 12-13-2009 at 08:00 PM.

    Current system:
    Corsair O8D LiquidAir
    EVGA X58 LE | Intel Core i7 920 | EVGA GTX 285 (waiting for Kepler)| X-Fi Fatal1ty | 1TB (SEAGATE), 2x150GB RAPTOR (raid 0) | TX950w

    RUN ON AIR



    My Flickr

  2. #2
    Xtreme Member
    Join Date
    Jul 2008
    Location
    Somewhere in FL
    Posts
    246
    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();
    Last edited by hale88; 12-13-2009 at 10:45 PM.

    Current system:
    Corsair O8D LiquidAir
    EVGA X58 LE | Intel Core i7 920 | EVGA GTX 285 (waiting for Kepler)| X-Fi Fatal1ty | 1TB (SEAGATE), 2x150GB RAPTOR (raid 0) | TX950w

    RUN ON AIR



    My Flickr

  3. #3
    Xtreme Enthusiast
    Join Date
    Oct 2007
    Location
    Mid UK. Lift a few rocks, eventually you will find me.
    Posts
    665
    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?
    Fun Box: Asus P8Z68-V GEN3++Corsair AX850++i5 2500k@4.5Ghz-1.272v++Corsair A50++2x8Gb Corsair Vengeance++MSI R7970 Lightning++Audigy2 Plat-EX++TBS 6280 DVB-T2 tuner++256Gb OCZ Vertex 4.500Gb Caviar Black.500Gb Seagate Barracuda++Sony AD7240s++Lian-Li PC-60++Linux Mint/Win 7++Asus P238Q

    Work Box: Gigabyte H61MA-DV3++Corsair HX620++i5 3450@stock++2x8Gb Corsair Vengeance++120Gb OCZ Agility 3++Linux Mint

    Quantum theory in a nutshell: It's so small we don't know where it is, it could be here, it could be there.

    Just 'cos it's legal don't make it right.

  4. #4
    Xtreme Member
    Join Date
    Jul 2008
    Location
    Somewhere in FL
    Posts
    246
    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();
    ?>

    Current system:
    Corsair O8D LiquidAir
    EVGA X58 LE | Intel Core i7 920 | EVGA GTX 285 (waiting for Kepler)| X-Fi Fatal1ty | 1TB (SEAGATE), 2x150GB RAPTOR (raid 0) | TX950w

    RUN ON AIR



    My Flickr

  5. #5
    Xtreme Enthusiast
    Join Date
    Oct 2007
    Location
    Mid UK. Lift a few rocks, eventually you will find me.
    Posts
    665
    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
    Fun Box: Asus P8Z68-V GEN3++Corsair AX850++i5 2500k@4.5Ghz-1.272v++Corsair A50++2x8Gb Corsair Vengeance++MSI R7970 Lightning++Audigy2 Plat-EX++TBS 6280 DVB-T2 tuner++256Gb OCZ Vertex 4.500Gb Caviar Black.500Gb Seagate Barracuda++Sony AD7240s++Lian-Li PC-60++Linux Mint/Win 7++Asus P238Q

    Work Box: Gigabyte H61MA-DV3++Corsair HX620++i5 3450@stock++2x8Gb Corsair Vengeance++120Gb OCZ Agility 3++Linux Mint

    Quantum theory in a nutshell: It's so small we don't know where it is, it could be here, it could be there.

    Just 'cos it's legal don't make it right.

  6. #6
    Xtreme Member
    Join Date
    Jul 2008
    Location
    Somewhere in FL
    Posts
    246
    thanks I just got my problem solved from my instructor . I was missing the input for the Area->created error

    Current system:
    Corsair O8D LiquidAir
    EVGA X58 LE | Intel Core i7 920 | EVGA GTX 285 (waiting for Kepler)| X-Fi Fatal1ty | 1TB (SEAGATE), 2x150GB RAPTOR (raid 0) | TX950w

    RUN ON AIR



    My Flickr

  7. #7
    Xtreme Enthusiast
    Join Date
    Oct 2007
    Location
    Mid UK. Lift a few rocks, eventually you will find me.
    Posts
    665
    np, good luck
    Fun Box: Asus P8Z68-V GEN3++Corsair AX850++i5 2500k@4.5Ghz-1.272v++Corsair A50++2x8Gb Corsair Vengeance++MSI R7970 Lightning++Audigy2 Plat-EX++TBS 6280 DVB-T2 tuner++256Gb OCZ Vertex 4.500Gb Caviar Black.500Gb Seagate Barracuda++Sony AD7240s++Lian-Li PC-60++Linux Mint/Win 7++Asus P238Q

    Work Box: Gigabyte H61MA-DV3++Corsair HX620++i5 3450@stock++2x8Gb Corsair Vengeance++120Gb OCZ Agility 3++Linux Mint

    Quantum theory in a nutshell: It's so small we don't know where it is, it could be here, it could be there.

    Just 'cos it's legal don't make it right.

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •