In: Computer Science
Write a method named area with one double parameter name radius, the method needs to return a double value that represents the area of a circle. If the parameter radius is negative, then return -1.0 to represents an invalid value write another overload method with 2 parameter side 1 and side 2 (double) where side 1 and side 2 represents the sides of a rectangle. The method needs to return an area of a rectangle. If either or both parameters is/ are negative return -1.0 indicate in invalided value
Example of input/output
area (5, 0); should return 78.53975
area(-1) ; should return -1 since the parameter is negative
area (5,0,6,0) should return 30.0 (5*6 = 30)
area (-1.0, 8.0); should return -1 since ghe first parameter is negative
Explanation:
Here are the generic methods named area for both, overloaded with the different number of parameters.
One returns the area of circle and other returns the area of the rectangle.
Methods:
double area(double radius)
{
if(radius<0)
return -1;
return 3.14*radius*radius;
}
double area(double side1, double side2)
{
if(side1<0 || side2<0)
return -1;
return side1*side2;
}
Sample code:
#include <iostream>
using namespace std;
double area(double radius)
{
if(radius<0)
return -1;
return 3.14*radius*radius;
}
double area(double side1, double side2)
{
if(side1<0 || side2<0)
return -1;
return side1*side2;
}
int main()
{
cout<<area(5.0)<<" "<<area(-1)<<"
"<<area(5.0, 6.0)<<" "<<area(-1.0 ,8.0);
return 0;
}
output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!