Class Colored Sphere - Python Programming Exercise

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 coordinates, and a color. 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 Colored Sphere

 Objective

Develop a Python and Extend the previous exercise to include "Colored Circles". Each circle should have a radius, center coordinates, and a color. Implement methods to:

- Initialize the circle with radius, coordinates, and color.
- Change the circle’s color.
- Calculate and return the perimeter (circumference) and area.
- Move the circle by updating its center coordinates.
- Implement a ToString method to describe the circle.

Test the circle by creating objects with different attributes and verifying their properties.

 Example Python Exercise

 Copy Python Code
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

 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.

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

  •  Class Rectangle

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

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