Class Shapes C# Exercise - C# Programming Course

 Exercise

Class Shapes

 Objetive

Create a project and the corresponding classes (using several files) for this classes diagram.

 Example Code

// Import the System namespace for basic functionality
using System;

// Define the main class containing all shape-related classes
public class ShapesDemo
{
    // Define the base class Shape as an abstract class
    public abstract class Shape
    {
        // Define an abstract method to calculate area (must be implemented in derived classes)
        public abstract double GetArea();

        // Define an abstract method to calculate perimeter (must be implemented in derived classes)
        public abstract double GetPerimeter();

        // Define a virtual method to display shape details (can be overridden in derived classes)
        public virtual void Display()
        {
            // Print a general message about the shape
            Console.WriteLine("This is a shape.");
        }
    }

    // Define the Rectangle class, inheriting from Shape
    public class Rectangle : Shape
    {
        // Declare private fields for the width and height of the rectangle
        private double width;
        private double height;

        // Define a constructor to initialize width and height
        public Rectangle(double width, double height)
        {
            // Assign width and height parameters to the respective fields
            this.width = width;
            this.height = height;
        }

        // Override the GetArea method to calculate the area of the rectangle
        public override double GetArea()
        {
            // Return the area calculated as width * height
            return width * height;
        }

        // Override the GetPerimeter method to calculate the perimeter of the rectangle
        public override double GetPerimeter()
        {
            // Return the perimeter calculated as 2 * (width + height)
            return 2 * (width + height);
        }

        // Override the Display method to show details specific to the rectangle
        public override void Display()
        {
            // Print details about the rectangle, including area and perimeter
            Console.WriteLine("Rectangle: ");
            Console.WriteLine("Width: " + width);
            Console.WriteLine("Height: " + height);
            Console.WriteLine("Area: " + GetArea());
            Console.WriteLine("Perimeter: " + GetPerimeter());
        }
    }

    // Define the Circle class, inheriting from Shape
    public class Circle : Shape
    {
        // Declare a private field for the radius of the circle
        private double radius;

        // Define a constructor to initialize the radius
        public Circle(double radius)
        {
            // Assign the radius parameter to the radius field
            this.radius = radius;
        }

        // Override the GetArea method to calculate the area of the circle
        public override double GetArea()
        {
            // Return the area calculated as π * radius * radius
            return Math.PI * radius * radius;
        }

        // Override the GetPerimeter method to calculate the perimeter of the circle
        public override double GetPerimeter()
        {
            // Return the perimeter calculated as 2 * π * radius
            return 2 * Math.PI * radius;
        }

        // Override the Display method to show details specific to the circle
        public override void Display()
        {
            // Print details about the circle, including area and perimeter
            Console.WriteLine("Circle: ");
            Console.WriteLine("Radius: " + radius);
            Console.WriteLine("Area: " + GetArea());
            Console.WriteLine("Perimeter: " + GetPerimeter());
        }
    }

    // Define the main entry point of the program
    public static void Main()
    {
        // Create a Rectangle object with specified width and height
        Rectangle rectangle = new Rectangle(5, 10);
        // Display the details of the rectangle
        rectangle.Display();

        // Create a Circle object with a specified radius
        Circle circle = new Circle(7);
        // Display the details of the circle
        circle.Display();
    }
}

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.