1+ # Student: Chi Kin Ho
2+ # Date: Wednesday, March 2, 2016
3+
4+ import math
5+
6+
7+ class Circle (object ):
8+
9+ def __init__ (self , the_radius ):
10+ # Keep track of the radius of the circle.
11+ self ._radius = the_radius
12+ # Calculate the diameter of the circle.
13+ self ._diameter = self ._radius * 2
14+
15+ # alternate constructor
16+ @classmethod
17+ def from_diameter (cls , the_diameter ):
18+ return cls (the_diameter / 2 )
19+
20+ @property
21+ def radius (self ):
22+ return self ._radius
23+
24+ @radius .setter
25+ def radius (self , the_radius ):
26+ if the_radius is not None :
27+ # Update the radius.
28+ self ._radius = the_radius
29+ # Update the diameter.
30+ self ._diameter = self ._radius * 2
31+
32+ @property
33+ def diameter (self ):
34+ return self ._diameter
35+
36+ # Set the diameter of the circle and update its radius.
37+ @diameter .setter
38+ def diameter (self , the_diameter ):
39+ if the_diameter is not None :
40+ # Update the diameter.
41+ self ._diameter = the_diameter
42+ # Update the radius.
43+ self ._radius = self ._diameter / 2
44+
45+ @property
46+ def area (self ): # area property
47+ # Return the area of the circle: pi * radius * radius
48+ return math .pi * self ._radius * self ._radius
49+
50+ def __repr__ (self ):
51+ return 'Circle({})' .format (self ._radius )
52+
53+ def __str__ (self ):
54+ return 'Circle({})' .format (self ._radius )
55+
56+ # + operator overloading
57+ def __add__ (self , other ):
58+ return Circle (self ._radius + other .radius )
59+
60+ # augmented assignment operator += overloading
61+ def __iadd__ (self , other ):
62+ self = self + other
63+ return self
64+
65+ # * operator overloading
66+ def __mul__ (self , other ):
67+ return Circle (self ._radius * other )
68+
69+ # augmented assignment operator *= overloading
70+ def __imul__ (self , other ):
71+ self = self * other
72+ return self
73+
74+ # reflected numerics for the * operator overloading
75+ def __rmul__ (self , other ):
76+ return self * other
77+
78+ # > operator overloading
79+ def __lt__ (self , other ):
80+ return self ._radius < other .radius
81+
82+ # >= operator overloading
83+ def __le__ (self , other ):
84+ return self ._radius <= other .radius
85+
86+ # > operator overloading
87+ def __gt__ (self , other ):
88+ return self ._radius > other .radius
89+
90+ # >= operator overloading
91+ def __ge__ (self , other ):
92+ return self ._radius >= other .radius
93+
94+ # == operator overloading
95+ def __eq__ (self , other ):
96+ return self ._radius == other .radius
97+
98+ # Steps 1, 2, 3, and 3:
99+
100+ c = Circle (4 )
101+ print (c .radius )
102+ print (c .diameter )
103+ print (c .area )
104+
105+ # Step 5
106+
107+ c = Circle .from_diameter (20 )
108+ print (c .diameter )
109+ print (c .radius )
110+
111+ # Step 6
112+
113+ print (repr (c ))
114+ d = eval (repr (c ))
115+ print (d )
116+ print (c )
117+
118+ # Step 7
119+
120+ c1 = Circle (2 )
121+ c2 = Circle (4 )
122+ print (c1 + c2 )
123+ print (c2 * 3 )
124+ print (3 * c2 )
125+
126+ # Step 8:
127+
128+ print (c1 > c2 )
129+ print (c1 < c2 )
130+ print (c1 == c2 )
131+ c3 = Circle (4 )
132+ print (c2 == c3 )
133+
134+ circles = [Circle (6 ), Circle (7 ), Circle (8 ), Circle (4 ), Circle (0 ), Circle (2 ), Circle (3 ), Circle (5 ), Circle (9 ), Circle (1 )]
135+ print (circles )
136+ circles .sort ()
137+ print (circles )
138+
139+
140+ # Step 8: Optional Features
141+
142+ print (3 * c2 == c2 * 3 )
143+
144+ a_circle = Circle (4 )
145+ another_circle = Circle (9 )
146+ a_circle += another_circle
147+ print (a_circle )
148+ print (another_circle )
149+
150+ a_circle = Circle (4 )
151+ a_circle *= 2
152+ print (a_circle )
0 commit comments