I would like some help on my CSC HW. It is on classes/objects, and it's a simple class on defining a circle, with the name class Circle(object).
The exact text of the HW (I completed the first two parts of this hw and thus this 3rd part is an expansion on the initial problem):
"""Expand on your Circle class by enabling the comparison of Circle objects using operators such as <, >, >=, <=, ==, and !=, where one Circle is considered "larger" than another if it is in fact larger (i.e., has greater area) the other 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 for displaying the coordinates and radius of the 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 at (3 , 5). Radius: 4:000000"
We were pointed to a certain page of our textbook with math operators for classes: eq(), gt(), ge(), lt(), le(), ne(), etc. So I was thinking, did my professor want something like this?
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 have to make a definition/attribute for each method we want to use in the class? Thank you in advance.