Class Square - C# Programming Exercise

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 coordinates of the square (the upper left corner, already stored as "Location") as well as the length of its side. A suitable constructor must be created to assign initial values, a Move method to change the coordinates, a Scale method to change the side length, a ToString method to return a string with its data, and redefine the GetPerimeter and GetArea methods to calculate perimeter and area. Additionally, a test program must be implemented in the Main to instantiate a Square object and test its methods.

 Category

OOP Object Oriented Programming

 Exercise

Class Square

 Objective

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.

 Write Your C# Exercise

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

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

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