Question: Implement the following shape hierarchy: The TwoDimShape class should contain read-only abstract property Area to calculate the area of the two-dimensional shape. Each derived class

Implement the following shape hierarchy:

Implement the following shape hierarchy: The TwoDimShape class should contain read-only abstract

The TwoDimShape class should contain read-only abstract property Area to calculate the area of the two-dimensional shape. Each derived class should have its own implementation of this property. The Rectangle class should have two private instance variables length and width, and their corresponding public properties Length and Width. Its constructor should take the length and the width of the rectangle as arguments. The area of the rectangle is equal to the product of its length and width. The Triangle class should have two private instance variables base and height, and their corresponding public properties Base and Height. Its constructor should take the base and the height of the triangle as arguments. The area of the triangle is equal to one half of the product of its base and height. The Circle class should have one private instance variable radius, and its corresponding public property Radius. Its constructor should take the radius of the circle as argument. The area of the circle is equal to the square of the radius multiplied by pi. Use Math.PI (from the system namespace) as the pi value.

We are going to use a TestShapeHierarchy class to test your shape hierarchy. In the Main method, an array of TwoDimShape is created. This array has three elements: a Rectangle object, a Triangle object and a Circle object. For each element in the array, a static method ProcessShape is called to display what shape the object is (rectangle, triangle or circle). It also displays the measurements (i.e. length and width for rectangle, base and height for triangle, radius for circle) and the area.

class TestShapeHierarchy

{

static void Main(string[] args)

{

TwoDimShape[] myShapes = new TwoDimShape[3];

myShapes[0] = new Rectangle(10, 5);

myShapes[1] = new Triangle(12, 6);

myShapes[2] = new Circle(8);

foreach (TwoDimShape tds in myShapes)

{

ProcessShape(tds);

Console.WriteLine("");

}

}

// you need to write a static method processShape

}

The following is the expected output:

It is a rectangle.

length=10 width=5 area=50

It is a triangle.

base=12 height=6 area=36

It is a circle.

radius=8 area=201.061929829747

Press any key to continue . . .

TwoDimShape Rectangle Triangle Circle

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!