import math # Import math module for calculations
# Circle class with radius, center coordinates, and color
class Circle:
def __init__(self, radius, x, y, color):
self.radius = radius # Radius of the circle
self.x = x # X coordinate of the circle's center
self.y = y # Y coordinate of the circle's center
self.color = color # Color of the circle
# Method to change the color of the circle
def change_color(self, new_color):
self.color = new_color # Update the color
# Method to calculate the perimeter (circumference)
def calculate_perimeter(self):
return 2 * math.pi * self.radius # Formula for perimeter (circumference)
# Method to calculate the area of the circle
def calculate_area(self):
return math.pi * (self.radius ** 2) # Formula for area
# Method to move the circle by changing its center coordinates
def move(self, new_x, new_y):
self.x = new_x # Update the X coordinate
self.y = new_y # Update the Y coordinate
# Method to describe the circle as a string
def __str__(self):
return f"Circle(radius={self.radius}, center=({self.x}, {self.y}), color={self.color})"
# Test program to create and test circles
if __name__ == "__main__":
# Create a Circle object with radius 5, center at (0, 0), and color red
circle1 = Circle(5, 0, 0, "red")
# Print initial circle details
print("Initial Circle:")
print(circle1)
print("Perimeter (Circumference):", circle1.calculate_perimeter())
print("Area:", circle1.calculate_area())
# Move the circle to new coordinates (2, 3)
circle1.move(2, 3)
print("\nCircle after moving:")
print(circle1)
# Change the color of the circle to blue
circle1.change_color("blue")
print("\nCircle after changing color:")
print(circle1)
# Create another Circle object with radius 10, center at (5, 5), and color green
circle2 = Circle(10, 5, 5, "green")
print("\nSecond Circle:")
print(circle2)
print("Perimeter (Circumference):", circle2.calculate_perimeter())
print("Area:", circle2.calculate_area())
Output
Initial Circle:
Circle(radius=5, center=(0, 0), color=red)
Perimeter (Circumference): 31.41592653589793
Area: 78.53981633974483
Circle after moving:
Circle(radius=5, center=(2, 3), color=red)
Circle after changing color:
Circle(radius=5, center=(2, 3), color=blue)
Second Circle:
Circle(radius=10, center=(5, 5), color=green)
Perimeter (Circumference): 62.83185307179586
Area: 314.1592653589793