Class Colored Circle - C# Programming Exercise

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 create a class called ColoredCircle that holds the following attributes: the radius of the circle and its color. This class should include methods to change the color and the radius of the circle. Additionally, the class should redefine the methods GetPerimeter and GetArea to calculate the perimeter and area of the circle, respectively, using the appropriate formulas. The project should allow working with both circles and squares together, displaying the properties of both shapes clearly in the output. The structure of the project should follow conventions and be properly organized to handle the different shapes.

 Category

OOP Object Oriented Programming

 Exercise

Class Colored Circle

 Objective

Expand the exercise (shapes + square), so that it can also store data about "Colored Circles":

 Write Your C# Exercise

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

// Define the main class containing all classes for Shapes and Colored Circle
public class ShapesDemo
{
    // Define the Square class with attributes for position and side length
    public class Square
    {
        // Integer fields to store X and Y coordinates for the top-left corner
        private int x;
        private int y;
        // Integer field to store the length of the side of the square
        private int side;

        // Constructor to initialize the square's position and side length
        public Square(int startX, int startY, int initialSide)
        {
            // Set the X coordinate of the square
            x = startX;
            // Set the Y coordinate of the square
            y = startY;
            // Set the side length of the square
            side = initialSide;
        }

        // Method to move the square by adjusting X and Y coordinates
        public void Move(int deltaX, int deltaY)
        {
            // Increment X by deltaX
            x += deltaX;
            // Increment Y by deltaY
            y += deltaY;
        }

        // Method to scale the square by changing its side length
        public void Scale(int scaleFactor)
        {
            // Multiply the side length by the scale factor
            side *= scaleFactor;
        }

        // Method to return a string with the square's data
        public override string ToString()
        {
            // Return formatted string with square's corner and side
            return $"Corner ({x},{y}), side {side}";
        }

        // Method to calculate and return the perimeter of the square
        public int GetPerimeter()
        {
            // Return 4 times the side length for the perimeter
            return 4 * side;
        }

        // Method to calculate and return the area of the square
        public int GetArea()
        {
            // Return the side length squared for the area
            return side * side;
        }
    }

    // Define the ColoredCircle class with attributes for position, radius, and color
    public class ColoredCircle
    {
        // Integer fields to store X and Y coordinates for the center of the circle
        private int x;
        private int y;
        // Integer field to store the radius of the circle
        private int radius;
        // String field to store the color of the circle
        private string color;

        // Constructor to initialize the circle's position, radius, and color
        public ColoredCircle(int startX, int startY, int initialRadius, string circleColor)
        {
            // Set the X coordinate of the circle's center
            x = startX;
            // Set the Y coordinate of the circle's center
            y = startY;
            // Set the radius of the circle
            radius = initialRadius;
            // Set the color of the circle
            color = circleColor;
        }

        // Method to move the circle by adjusting X and Y coordinates
        public void Move(int deltaX, int deltaY)
        {
            // Increment X by deltaX
            x += deltaX;
            // Increment Y by deltaY
            y += deltaY;
        }

        // Method to scale the circle by changing its radius
        public void Scale(int scaleFactor)
        {
            // Multiply the radius by the scale factor
            radius *= scaleFactor;
        }

        // Method to return a string with the circle's data
        public override string ToString()
        {
            // Return formatted string with circle's center, radius, and color
            return $"Center ({x},{y}), radius {radius}, color {color}";
        }

        // Method to calculate and return the perimeter (circumference) of the circle
        public double GetPerimeter()
        {
            // Return the circumference using the formula 2 * π * radius
            return 2 * Math.PI * radius;
        }

        // Method to calculate and return the area of the circle
        public double GetArea()
        {
            // Return the area using the formula π * radius squared
            return Math.PI * radius * radius;
        }
    }

    // Define the Main entry point of the program for testing
    public static void Main()
    {
        // Create a Square object with specified coordinates and side length
        Square square = new Square(10, 5, 7);
        // Display the square's details by calling ToString on the Square object
        Console.WriteLine("Square details: " + square.ToString());
        // Display the square's perimeter
        Console.WriteLine("Square perimeter: " + square.GetPerimeter());
        // Display the square's area
        Console.WriteLine("Square area: " + square.GetArea());

        // Create a ColoredCircle object with specified coordinates, radius, and color
        ColoredCircle circle = new ColoredCircle(15, 10, 5, "Red");
        // Display the circle's details by calling ToString on the ColoredCircle object
        Console.WriteLine("Circle details: " + circle.ToString());
        // Display the circle's perimeter
        Console.WriteLine("Circle perimeter: " + circle.GetPerimeter());
        // Display the circle's area
        Console.WriteLine("Circle area: " + circle.GetArea());

        // Move the square by specified delta values
        square.Move(3, 3);
        // Scale the square by a factor of 2
        square.Scale(2);
        // Display the updated square details
        Console.WriteLine("Updated square details: " + square.ToString());

        // Move the circle by specified delta values
        circle.Move(5, 5);
        // Scale the circle by a factor of 3
        circle.Scale(3);
        // Display the updated circle details
        Console.WriteLine("Updated circle details: " + circle.ToString());
    }
}

 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#.

  •  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...

  •  Class Shapes

    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 ...

  •  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 ...