i on csc hw. on classes/objects, , it's simple class on defining circle, name class circle(object).
the exact text of hw (i completed first 2 parts of hw , 3rd part expansion on initial problem):
"""expand on circle class enabling comparison of circle objects using operators such <, >, >=, <=, ==, , !=, 1 circle considered "larger" if in fact larger (i.e., has greater area) other circle.
the following code:
a = circle(2, 5, 1.5) b = circle(-6, 1, 1) print < b, != b, >= b
should generate output:
false true true
this code displaying coordinates , radius of 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 @ (%d , %d). radius: %f" % (self.x, self.y, self.r) def main(): print circle(3, 5, 4.0) main()
the output of class "circle @ (3 , 5). radius: 4:000000"
we pointed page of our textbook math operators classes: eq(), gt(), ge(), lt(), le(), ne(), etc. thinking, did professor want 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 @ (%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 on __lt__(), __le__(), __ne__(), etc def main(): = circle(3,4,1.5) b = circle(1,2,5.0) c = circle(5,7,7) d = circle(9,8,3) print < b, b > c, < c, >= c main() #output should "true, false, true, false"
do have make definition/attribute each method want use in class? thank in advance.
define or override comparison operators class. http://docs.python.org/reference/expressions.html#notin
looks on right track, except need pass second circle object comparison. self refers first circle object. self.r give r of first circle. need return true or false method.
def __gt__(self, circle2): return self.r > circle2.r
note comparing r's of circles.
Comments
Post a Comment