# 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