Shan Shan - 9 days ago 7
C# Question

CalculateArea using poymorphism and virtual method - not getting proper result.

I have write a code to calculate the area of different shapes using polymorphism (Virtua & Override Method) but not getting correct result. :(

Below is my code:

class Program
{
static void Main(string[] args)
{
double side = 0;
double length = 0;
double width = 0;
double height = 0;
double baseoftriangle = 0;
double radius = 0;

UserChoice:
Console.WriteLine("For what shape you want to calculate the Area:\n1. Sqaure\n2. Rectangle\n3. Triangle\n4. Circle");
Console.Write("Please Select the number from above options: ");
int choice = int.Parse(Console.ReadLine());



switch (choice)
{
case 1:
Console.Write("Please enter the side of square: ");
side = double.Parse(Console.ReadLine());
break;
case 2:
Console.Write("Please enter the length of rectangle: ");
length = double.Parse(Console.ReadLine());
Console.Write("Please enter the width of rectangle: ");
width = double.Parse(Console.ReadLine());
break;
case 3:
Console.Write("Please enter the height of triangle: ");
height = double.Parse(Console.ReadLine());
Console.Write("Please enter the base of triangle: ");
baseoftriangle = double.Parse(Console.ReadLine());
break;
case 4:
Console.Write("Please enter the radius of circle: ");
radius = double.Parse(Console.ReadLine());
break;
default:
Console.WriteLine("Incorrect Choice, please try again!");
goto UserChoice;
}

CalculateArea Sqa = new Square();
Sqa = new Rectangle();
Sqa = new Triangle();
Sqa = new Circle();
if (choice == 1)
{
Sqa.Area(side);
Sqa.ShowResult();
}
else if(choice==2)
{
Sqa.Area(length,width);
Sqa.ShowResult();
}
else if(choice==3)
{
Sqa.Area(height, baseoftriangle);
Sqa.ShowResult();
}
else
{
Sqa.Area(radius);
Sqa.ShowResult();
}


ChoiceOfAnotherCalculation:
Console.Write("\nDo you want to calculate area of any other shape? Give input in Yes or NO: ");
string choice1 = Console.ReadLine();

switch (choice1.ToUpper())
{
case "YES":
goto UserChoice;
case "NO":
break;
default:
Console.WriteLine("Incorrect Choice, please try again!");
goto ChoiceOfAnotherCalculation;
}
}
}
class CalculateArea
{
public double result;
public virtual void Area(double side)
{
}
public virtual void Area(double length, double width)
{
}
public void ShowResult()
{
Console.WriteLine($"Your Result is {result}");
}
}
class Square: CalculateArea
{

public override void Area(double side)
{
result = side * side;
}

}
class Rectangle:CalculateArea
{
public override void Area(double length, double width)
{
result = length * width;
}
}
class Triangle:CalculateArea
{
public override void Area(double height, double baseoftriangle)
{
result = (height * baseoftriangle)/2;
}
}
class Circle:CalculateArea
{
public override void Area(double radius)
{
result = 3.14159 * radius * radius;
}
}


For Rectangle & Triangle, I am always getting 0 as a result and for square I am getting result from circle. I think this issue is causing because of the way I have created objects for class (May be because of lack of understanding in polymorphism).

Can someone please have a look and let me know what is the actual problem here.

Apart from this also I would appreciate your efforts if you can provide me the solution with get & set properties for each class. I have tried to write the code (Where I have tried to define the property in each derive class as well as in base class) but was getting various errors that's why I have jump on above solution.

I will also request please do not downvote the question as system won't allow me ask question for a long time. As this is the only platform where I can understood the things properly.

Many Thanks.

Answer

Inside the switch statement, after case 1, your program will exit. This is because you have used return;, you might have intended to use break; instead. Break statement ensures that your switch wont fall through to the next statement, and a return statement would terminate the execution immediately.

Correct case statement should be like this

case 1:
    Console.Write("Please enter the side of square: ");
    side = int.Parse(Console.ReadLine());
    break;