loader image

21CSL582 Program 11

11. Develop a C# program to create an abstract class Shape with abstract methods calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle that extend the Shape class and implement the respective methods to calculate the area and perimeter of each shape.

using System;

public abstract class Shape
{
    public abstract double GetPerimeter();
    public abstract double GetArea();
}

public class Circle : Shape
{
    private double radius;
    private readonly double pi = Math.PI;

    public Circle()
    {
        this.radius = 1;
    }

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public override double GetArea()
    {
        return pi * Math.Pow(radius, 2);
    }

    public override double GetPerimeter()
    {
        return 2 * pi * radius;
    }
}

public class Triangle : Shape
{
    private double width, height;

    public Triangle()
    {
        this.width = 1;
        this.height = 1;
    }

    public Triangle(double width, double height)
    {
        this.width = width;
        this.height = height;
    }

    public override double GetArea()
    {
        return width * height;
    }

    public override double GetPerimeter()
    {
        return width + height + Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2));
    }
}

public class ShapeClient
{
    public static void Main(string[] args)
    {
        double width = 13, length = 9;
        Shape triangle = new Triangle(width, length);
        Console.WriteLine("The width of the triangle is: " + width
            + "\nThe height of the triangle is: " + length
            + "\nThe area of the triangle is: " + triangle.GetArea()
            + "\nThe perimeter of the triangle is: " + triangle.GetPerimeter() + ".");

        double radius = 3;
        Shape circle = new Circle(radius);
        Console.WriteLine("\nThe radius of the circle is: " + radius
            + "\nThe area of the circle is: " + circle.GetArea()
            + "\nThe perimeter of the circle is: " + circle.GetPerimeter() + ".");
    }
}
The width of the triangle is: 13
The height of the triangle is: 9
The area of the triangle is: 117
The perimeter of the triangle is: 37.8113883008419.

The radius of the circle is: 3
The area of the circle is: 28.274333882308138
The perimeter of the circle is: 18.84955592153876.

Leave a Reply

Your email address will not be published. Required fields are marked *