Class Shapes - C# Programming Exercise

This exercise involves creating a project in C# that implements several classes based on a class diagram. The goal is to organize the code by splitting the classes into separate files, making the code easier to maintain and reuse. The exercise emphasizes the principles of Object-Oriented Programming (OOP), such as inheritance, encapsulation, and the proper implementation of methods and attributes within classes.

 Category

OOP Object Oriented Programming

 Exercise

Class Shapes

 Objective

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

 Write Your C# Exercise

// 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();
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP Object Oriented Programming

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Class Vehicles

    This exercise involves creating a project in C# and defining the corresponding classes according to a class diagram. Each class must include the attr...

  •  Class Square

    This exercise consists of completing the project called "Shapes" by adding a class named Square. In this class, we will store the starting X and Y coordinat...

  •  Class Orders

    In this exercise, you must create a project and the corresponding classes according to the class diagram. Each class must include the attributes and ...

  •  Class Colored Circle

    In this expanded exercise, you must modify the shapes and square project to also include a new class that stores data about colored circles. You need to...

  •  Classes Student + Teacher

    In this exercise in C#, you will need to write a program that includes the Person class you just created. From this class, you will create...

  •  Class Photo Album

    Write a C# class called "PhotoAlbum" with a private attribute "numberOfPages". The class should also have a public method named "GetNumberOfPages", whic...