Class Rectangle - Python Programming Exercise

In this exercise, you will develop a Python project called "Shapes," incorporating a class named "Square." Each square will store the starting X and Y coordinates (representing the upper-left corner, stored in a "Location" class) and the length of its side. This exercise is perfect for practicing class definition, method implementation, and object-oriented programming in Python. By implementing this project, you will gain hands-on experience in handling class definitions, method implementation, and object-oriented programming in Python. This exercise not only reinforces your understanding of object-oriented programming but also helps you develop efficient coding practices for managing user interactions.

 Category

OOP Object-Oriented Programming

 Exercise

Class Rectangle

 Objective

Develop a Python project called "Shapes," incorporating a class named "Square." Each square will store the starting X and Y coordinates (representing the upper-left corner, stored in a "Location" class) and the length of its side.

You need to create the following:

- A suitable constructor to initialize the X, Y coordinates, and side length.

- A move method to change the X and Y coordinates.

- A scale method to modify the side length (e.g., a scaling factor of 2 would transform a side of 3 into 6).

- A __str__ method to return a string with the square’s information, such as "Corner (10,5), side 7."

- Redefine get_perimeter and get_area methods to return the correct values.

- Another point will be awarded for the attributes and the overall class structure.

- The remaining points are for the test in the "main" function.

 Example Python Exercise

 Copy Python Code
# location.py
class Location:
    def __init__(self, x, y):
        self.x = x  # X coordinate of the upper-left corner
        self.y = y  # Y coordinate of the upper-left corner

    def __str__(self):
        return f"({self.x}, {self.y})"  # Return the coordinates as a string


# square.py
class Square:
    def __init__(self, x, y, side_length):
        self.location = Location(x, y)  # Create a Location instance for the square's corner
        self.side_length = side_length  # The length of the side of the square

    def move(self, new_x, new_y):
        self.location.x = new_x  # Update the X coordinate of the square's upper-left corner
        self.location.y = new_y  # Update the Y coordinate of the square's upper-left corner

    def scale(self, factor):
        self.side_length *= factor  # Scale the side length by the given factor

    def get_perimeter(self):
        return 4 * self.side_length  # Perimeter of a square is 4 times the side length

    def get_area(self):
        return self.side_length ** 2  # Area of a square is side length squared

    def __str__(self):
        return f"Corner {self.location}, side {self.side_length}"  # Return the square's description


# main.py (Test program)
if __name__ == '__main__':
    # Create a Square object with initial coordinates (10, 5) and side length 7
    square1 = Square(10, 5, 7)
    print(square1)  # Output the square's details

    # Get the perimeter and area of the square
    print(f"Perimeter: {square1.get_perimeter()}")  # Perimeter should be 28
    print(f"Area: {square1.get_area()}")  # Area should be 49

    # Move the square to a new position (15, 20)
    square1.move(15, 20)
    print("After moving:", square1)  # Output the new square's details

    # Scale the square by a factor of 2
    square1.scale(2)
    print("After scaling:", square1)  # Output the scaled square's details

    # Get the new perimeter and area after scaling
    print(f"New Perimeter: {square1.get_perimeter()}")  # New perimeter should be 56
    print(f"New Area: {square1.get_area()}")  # New area should be 196

 Output

Corner (10, 5), side 7
Perimeter: 28
Area: 49
After moving: Corner (15, 20), side 7
After scaling: Corner (15, 20), side 14
New Perimeter: 56
New Area: 196

 Share this Python Exercise

 More Python Programming Exercises of OOP Object-Oriented Programming

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. 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 Python.

  •  Class Purchases

    In this exercise, you will develop a Python project with the necessary classes (distributed across multiple files) based on the given class diagram. Each class should...

  •  Class Colored Sphere

    In this exercise, you will develop a Python project and extend the previous exercise to include "Colored Circles". Each circle should have a radius, center coordinate...

  •  Classes: Learner and Instructor

    In this exercise, you will develop a Python program that incorporates a Person class. Then, create two additional classes, Student and Teacher, that inherit from Pers...

  •  Class Picture Collection

    In this exercise, you will develop a Python program with a class named "PhotoAlbum" that includes a private attribute "pageCount." This exercise is perfect for...

  •  Class GeometricShapes

    In this exercise, you will develop a Python project with the required classes, organizing them into separate files, according to a class diagram. This exercise...

  •  Vehicle Classes

    In this exercise, you will develop a Python project with corresponding classes, spread across multiple files, based on the provided class diagram. Each class should h...