Class Square C# Exercise - C# Programming Course

 Exercise

Class Square

 Objetive

Complete the project named "Shapes", adding a class named "Square" to it. For each square, we will store its starting X and Y coordinates (the upper left corner, already stored as a "Location") and the length of its side.

You will have to create:
- A suitable constructor, to assign starting values to X, Y and the side. (2 points)
- A Move method, to change X and Y coordinates. (1 point)
- A Scale method, to change its side (for example, a scale factor of 2 would turn a side of 3 into 6). (1 point)
- A method ToString, to return a string with its data (for example: "Corner (10,5), side 7". (1 point)
- Redefine "GetPerimeter" and "GetArea", so that they return the correct values (2 points).

- Another point corresponds to the attributes and the overall structure.

- The remaining 2 points correspond to the test from "Main"

You must deliver a ZIP file containing the entire project.

 Example Code

// 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 - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.