// 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());
}
}
Salida
Square details: Corner (10,5), side 7
Square perimeter: 28
Square area: 49
Circle details: Center (15,10), radius 5, color Red
Circle perimeter: 31.41592653589793
Circle area: 78.53981633974483
Updated square details: Corner (13,8), side 14
Updated circle details: Center (20,15), radius 15, color Red
Código de Ejemplo Copiado!