Exercise 44

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

# import pi number of math module
from math import pi
class Circle:
# create a constructor with parameters center (a,b) and radius r
def __init__(self,a,b,r):
self.a = a
self.b = b
self.r = r

# create Area() method
def Area(self):
# compute and return the area of circle
return pi*(self.r)**2

# create the Perimeter method
def Perimeter(self):
# compute and return the perimeter of circle
return 2*pi*self.r
def tetbelongs(self, A):
if ((self.a - A[0])**2 + (self.b - A[1])**2== (self.r)**2):
return True
else:
return False
# Testing the class circle
myCircle = Circle(1,0 , 1)
print("The area of myCircle is : " , myCircle.Area() )
print("The perimeter of myCircle is : " , myCircle.Perimeter() )
A = (1,1)
print(myCircle.tetbelongs(A))

The output is:
The area of myCircle is :  3.141592653589793
The perimeter of myCircle is :  6.283185307179586
True

Younes Derfoufi
my-courses.net

Leave a Reply