I need help at CSC HW. This is on classes / objects, and this is a simple class for defining a circle with a class name Circle (object).
The exact text is HW (I completed the first two parts of this hw, and thus this 3rd part is an extension of the original task):
"" Expand the Circle class by enabling the comparison of Circle objects using operators such as <,>,> =, <=, == and and = =, where one circle is considered "larger" than the other if it is actually larger (i.e. has a large area) another circle.
The following code:
A = Circle(2, 5, 1.5) B = Circle(-6, 1, 1) print A < B, A != B, A >= B
Should generate this output:
False True True
This is my code to display the coordinates and radius of a circle:
class Circle(object): def __init__(self, x=0, y=0, r=0): self.x = x self.y = y self.r = r def __str__(self): return "Circle at (%d , %d). Radius: %f" % (self.x, self.y, self.r) def main(): print Circle(3, 5, 4.0) main()
The output of this class is "Circle in (3, 5). Radius: 4: 000000"
We pointed to a specific page of our textbook with mathematical operators for classes: eq (), gt (), ge (), lt (), le (), ne (), etc. So I thought my professor wanted something like that?
import math class Circle(object): def __init__(self, x=0, y=0, r=0): self.x = x self.y = y self.r = r def __str__(self): return "Circle at (%d , %d). Radius: %f" % (self.x, self.y, self.r) def calcArea(self, r): self.r = r return (math.pi)*(r**2) def __gt__(self, circ1Radius, circ2Radius) self.circ1Radius = circ1Radius self.circ2Radius = circ2Radius r1 = circ1Radius r2 = circ2Radius r1 > r2 or r2 > r1 def __ge__(self, circ1Radius, circ2Radius) #And so on for __lt__(), __le__(), __ne__(), etc def main(): A = Circle(3,4,1.5) B = Circle(1,2,5.0) C = Circle(5,7,7) D = Circle(9,8,3) print A < B, B > C, A < C, A >= C main() #Output should be "True, False, True, False"
Do we need to create a definition / attribute attribute for each method that we want to use in the class? Thanks in advance.