You are so close already...
This is the code you have already put into functions yourself...
static double GetSquareArea(double squareside)
{
double squarearea = squareside * squareside;
return squarearea;
}
static double GetSquarePerimeter(double squareside)
{
double squareperimeter = 4 * squareside;
return squareperimeter;
}
So let's revisit the code you have to refactor and see what we have left...the bits in bold are the bits that change because we now have your two functions ...
class Program
{
static void Main(string[] args)
{
double tribase = 9;
double triwidth = 6;
double trihypotenuse = Math.Sqrt((tribase * tribase) + (triwidth * triwidth));
double triarea = 0.5 * (tribase * triwidth);
double triperimeter = tribase + triwidth + trihypotenuse;
Console.WriteLine("Area of right triangle = {0}\nPerimeter of rigth triangle = {1}\n", triarea, triperimeter);
double squareside = 8;
double squarearea = GetSquareArea(squareside);
double squareperimeter = GetSquarePerimeter(squareside);
Console.WriteLine("Area of square = {0}\nPerimeter of square = {1}\n", squarearea, squareperimeter);
}
}
So do the same with the perimeter of a triangle ... the bits in bold need to go out into a separate function into which you will pass the values of
tribase
and
triwidth
class Program
{
static void Main(string[] args)
{
double tribase = 9;
double triwidth = 6;
double trihypotenuse = Math.Sqrt((tribase * tribase) + (triwidth * triwidth));
double triarea = 0.5 * (tribase * triwidth);
double triperimeter = tribase + triwidth + trihypotenuse;
Console.WriteLine("Area of right triangle = {0}\nPerimeter of rigth triangle = {1}\n", triarea, triperimeter);
double squareside = 8;
double squarearea = GetSquareArea(squareside);
double squareperimeter = GetSquarePerimeter(squareside);
Console.WriteLine("Area of square = {0}\nPerimeter of square = {1}\n", squarearea, squareperimeter);
}
}
Notice that
trihypotenuse
is an interim calculation to get to the perimeter ... you don't need it anywhere else in
Main
so that needs to go into the function as well.
Have a go at it and post your attempt as a comment to this solution and I'll tell you if you've got it right