Clase Cuadrado Ejercicio C# - Curso de Programación C# (C Sharp)

 Ejercicio

Clase Cuadrado

 Objetivo

Complete el proyecto llamado "Formas" (8 de enero), agregándole una clase llamada "Cuadrado". Para cada cuadrado, almacenaremos sus coordenadas iniciales X e Y (la esquina superior izquierda, ya almacenada como "Ubicación") y la longitud de su lado.

Tendrás que crear:
- Un constructor adecuado, para asignar valores iniciales a X, Y y el lateral. (2 puntos)
- Un método Move, para cambiar las coordenadas X e Y. (1 punto)
- Un método scale, para cambiar su lado (por ejemplo, un factor de escala de 2 convertiría un lado de 3 en 6). (1 punto)
- Un método ToString, para devolver una cadena con sus datos (por ejemplo: "Corner (10,5), side 7"). (1 punto)
- Redefinir "GetPerimeter" y "GetArea", para que devuelvan los valores correctos (2 puntos).

- Otro punto corresponde a los atributos y a la estructura general.

- Los 2 puntos restantes corresponden a la prueba de "Main"

Debe entregar un archivo ZIP que contenga todo el proyecto.

 Código de Ejemplo

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

// Define the main class containing the Square class and main program logic
public class ShapesDemo
{
    // Define the Square class to represent a square shape
    public class Square
    {
        // Declare an integer field for the X coordinate of the square
        private int x;
        // Declare an integer field for the Y coordinate of the square
        private int y;
        // Declare an integer field for the length of the side of the square
        private int side;

        // Define a constructor to initialize X, Y, and side length
        public Square(int startX, int startY, int sideLength)
        {
            // Set the initial X coordinate
            x = startX;
            // Set the initial Y coordinate
            y = startY;
            // Set the initial side length
            side = sideLength;
        }

        // Define a method to move the square by changing X and Y coordinates
        public void Move(int newX, int newY)
        {
            // Update the X coordinate with new value
            x = newX;
            // Update the Y coordinate with new value
            y = newY;
        }

        // Define a method to scale the square by multiplying the side by a factor
        public void Scale(int scaleFactor)
        {
            // Multiply the side length by the scale factor
            side *= scaleFactor;
        }

        // Override the ToString method to return the square's data as a string
        public override string ToString()
        {
            // Return a string representation of the square's location and side
            return $"Corner ({x},{y}), side {side}";
        }

        // Define a method to get the perimeter of the square
        public int GetPerimeter()
        {
            // Calculate and return the perimeter of the square
            return 4 * side;
        }

        // Define a method to get the area of the square
        public int GetArea()
        {
            // Calculate and return the area of the square
            return side * side;
        }
    }

    // Define the main entry point of the program for testing
    public static void Main()
    {
        // Create a Square object with initial X, Y, and side length
        Square mySquare = new Square(10, 5, 7);
        // Display the square's initial data by calling ToString
        Console.WriteLine(mySquare.ToString());

        // Move the square to new X and Y coordinates
        mySquare.Move(20, 15);
        // Display the square's new location and same side length
        Console.WriteLine(mySquare.ToString());

        // Scale the square's side by a factor of 2
        mySquare.Scale(2);
        // Display the updated data of the square
        Console.WriteLine(mySquare.ToString());

        // Display the perimeter of the square
        Console.WriteLine("Perimeter: " + mySquare.GetPerimeter());

        // Display the area of the square
        Console.WriteLine("Area: " + mySquare.GetArea());
    }
}

Juan A. Ripoll - Tutoriales y Cursos de Programacion© 2025 Todos los derechos reservados.  Condiciones legales.