Exercise 44. Circle class

1 - Define a Circle class allowing to create a circleC (O, r) with center O(a, b) and radius r using the constructor:

    def __init__(self,a,b,r):         
self.a = a
self.b = b
self.r = r

2 - Define a Area() method of the class which calculates the area of ​​the circle.
3 - Define a Perimeter() method of the class which allows you to calculate the perimeter of the circle.
4 - Define a testBelongs() method of the class which allows to test whether a point A(x, y) belongs to the circle C(O, r) or not.

Solution

from math import pi
class Circle:
def __init__(self, a, b, r):
self.a = a
self.b = b
self.r = r

def perimeter (self):
return 2 * pi * self.r


def area (self):
return pi * self.r**2


# form of the cercle equation
def formEquation (self, x, y):
return (x-self.a)**2 + (y-self.b)**2 - self.r**2

# method to test if given point blong to the circle or not
def test_belong (self, x, y):
if (self.formEquation (x, y) == 0):
print ("the point: (", x, y, ") belongs to the circle C")
else:
print ("the point: (", x, y, ") does not belong to the circle C")


# Creating of an instance object
C = Circle (1,2,1)

print ("the perimeter of the circle C is:", C.perimeter() )
print ("the area of circle C is:", C.area())
# we test if the point(1,1) belong to the circle or not
C.test_belong(1,1)
# The output is:
#the perimeter of the circle C is: 6.283185307179586
#the area of circle C is: 3.141592653589793
#the point: ( 1 1 ) belongs to the circle C
Younes Derfoufi
my-courses.net
One thought on “Solution Exercise 44: class circle en python”
  1. import math
    class Circle:
    def __init__(self, a, b, r):
    self.a = a
    self.b = b
    self.r = r
    def area(self):
    return math.pi * self.r**2
    def perimeter(self):
    return 2*math.pi*self.r
    def testBelongs(self, x, y):
    if (x – self.a)**2 + (y – self.b)**2 == self.r**2:
    return print(f"{(x,y)} points are belongs to the circle(O,{self.r})"
    f" with center O{self.a,self.b} ")
    else:
    return print(f"{(x, y)} points are not belongs to the circle(O,{self.r})"
    f" with center O{self.a, self.b} ")

Leave a Reply