Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculate area of a rectangle?

    the idea is that the function to calculate the area to be used inside the function perimeter, so that the perimeter function handles all printing.

    This is what I've done but it's not the same as what I have mentioned above.
    What Should I do?


    This is what I want to do

    Create a method called "Area ()" that calculates the area of a rectangle.
    Call the method "Area ()" method from within "perimeter ()", submit
    length and width as an argument to the method "Area ()" and use these to
    calculate the area. Modify Approach "perimeter () "so that it prints the perimeter
    and area when the user presses the button "CALCULATE". No printing will be done in
    Approach "Area () "


    PHP Code:
    <body>

        

        

    <h1>PHP-sida 4</h1>

        

        

    <form action="php4.php" method="post">

        

        

        

    <p>Längd: <input type="text" name="length" size="20" /></p>

        

        

        

    <p>Bredd: <input type="text" name="width" size="20" /></p>

        

        

        

    <p><input type="submit" value="CALCULATE" name="calc" /></p>

        

        

    </form>




    <?php



    if (isset($_POST['calc']))
    {

        

    $length$_POST['length'];

        

    $width$_POST['width'];

        

    echo 
    "length:" $length"<br><br>";

        

    echo 
    "width:" $width"<br><br>";

        



    function 
    perimeter($length$width)
    {

    return  
    2* ($length$width);

    }
    $perimeterperimeter($length$width);

    echo 
    "perimeter:" $perimeter"<br>""<br>";

        


    function 
    area($length$width)
    {

        

    return 
    $length$width;
    }
    $area area($length$width);
    echo 
    "area:" $area ;

    }
     
    ?>


    </body>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    This looks like school homework to me.

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    DAMN THESE SCHOOLKIDS!

    Not quite sure what you're trying to do but maybe a little snippet can give you an idea.

    Is this what you're trying to do? Call two functions from one function:

    PHP Code:
    class rectangle{
    function 
    calculateParameters($length,$width){
        
    $perim $this->calculatePerimeter($length,$width);
        
    $area $this->calculateArea($length,width);
        echo(
    'Perimeter: ' $perim '<br>');
        echo(
    'Area: ' $area);
    }
    function 
    calculatePerimeter($length,$width){
        return 
    * ($length $width);
    }
    function 
    calculateArea($length,width){
        return 
    $length $width
    }
    }
    $rectangle = new rectangle;
    $rectangle->calculateParameters(50,20); 
    OR are you trying to do everything in one function:
    PHP Code:
    function rectangleParameters($length,$width){
        
    $area $length $width;
        
    $perim =  * ($length $width);
        echo(
    'Perimeter: ' $perim '<br>');
        echo(
    'Area: ' $area);
    }
    rectangleParameters(50,20); 
    Personally I would suggest you use a method similar to the first option I gave you, that way its easy to calculate individual values without having to call the calculateParameters method.

  • #4
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you but I am not allowed to give the numbers myself

    rectangleParameters(50,20);

    It's like this anyone that going to see this page can give what ever length and what ever width that they want in the text-box and than enters the calculate button and gets the length, the width that he/she gave and the area and the perimeter that was calculated.


    Create a method called "perimeter ()" that calculates the perimeter of a rectangle. The function to receive the two parameters "long" and "width" of a form. perimeter shall then be printed on the page when the user presses a button titled "CALCULATE".

    Create a method called "Area ()" that calculates the area of a rectangle.
    Call the method "Area ()" method from within "perimeter ()", submit
    length and width as an argument to the method "Area ()" and use these to
    calculate the area. Modify Approach "perimeter () "so that it prints the perimeter
    and area when the user presses the button "CALCULATE". No printing will be done in
    Approach "Area () "


    the idea is that the function to calculate the area to be used inside the function perimeter, so that the perimeter function handles all printing.
    Last edited by newbe; 12-09-2010 at 12:50 PM.

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    Go do your own homework.


  •  

    Posting Permissions

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