...

View Full Version : Calculate area of a rectangle?



newbe
12-09-2010, 01:58 AM
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 () "



<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);

}
$perimeter= perimeter($length, $width);

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




function area($length, $width)
{



return $length* $width;
}
$area = area($length, $width);
echo "area:" . $area ;

}

?>


</body>

mlseim
12-09-2010, 12:31 PM
This looks like school homework to me.

idalatob
12-09-2010, 12:38 PM
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:



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 2 * ($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:


function rectangleParameters($length,$width){
$area = $length * $width;
$perim = 2 * ($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.

newbe
12-09-2010, 12:46 PM
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.

idalatob
12-10-2010, 07:26 AM
Go do your own homework.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum