Design and implement a class named Circle2D that contains: Two data fields (i.e.
ID: 3665594 • Letter: D
Question
Design and implement a class named Circle2D that contains: Two data fields (i.e., instance variables) named x and y that specify the center of the circle. A data field radius with get/set methods. The method getRadius() returns the value of the radius while setRadius() sets a new value for the radius. A constructor (i.e., __init__ method) that creates a circle with specified x, y, and radius. Use the default value 0 for all parameters. An __str__ method that return a string representation of the form "Circle with center (x, y) and radius radius", where x, y, and radius will be replaced by the circle's actual values of the center and raduis. For example, on a circle object with center at (2, 3) and radius 10, this method will return the string "Circle with center (2, 3) and radius 10". A getX() and getY() methods. A method getArea() that returns the area of the circle. A method getPerimeter() that returns the perimeter of the circle. A method containsPoint(x, y) that returns True if the specified point (x, y) is inside this circle. (Hint: see Figure 1. A point P is inside the circle c iff distance between c.center and P is less than c.radius.)
Explanation / Answer
Code:
import math
class Circle2D:
def __init__(self,x=0,y=0,radius=0):
self.x=x
self.y=y
self.radius=radius
def getRadius(self):
return self.radius
def setRadius(self,radius):
self.radius=radius
def getX(self):
return self.x
def getY(self):
return self.y
def getArea(self):
return math.pi * self.radius * self.radius
def getPerimeter(self):
return 2 * math.pi * self.radius
def __str__(self):
return "Circle with center ("+str(self.x)+", "+str(self.y)+") and radius "+str(self.radius)
def containsPoint(self,x,y):
distance = math.sqrt((self.x-x)*(self.x-x) + (self.y-y)*(self.y-y))
if distance <= self.radius:
return True
return False
c = Circle2D(1,2,3)
print c
print c.getRadius()
print c.setRadius(4)
print c.getRadius()
print c.getX()
print c.getY()
print c.getArea()
print c.getPerimeter()
print c.containsPoint(2,3)
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.