Skip to content

Commit 0f7175e

Browse files
author
KitN
committed
More OOP practice
1 parent d089072 commit 0f7175e

2 files changed

Lines changed: 54 additions & 21 deletions

File tree

‎code/Point1.py‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,37 @@ class Point:
1717
1818
attributes: x, y
1919
"""
20-
20+
def __init__(self, x=0, y=0):
21+
self.x = x
22+
self.y = y
23+
24+
def __str__(self):
25+
return('(%g, %g)' % (self.x, self.y))
26+
27+
def __add__(self, other):
28+
if isinstance(other, Point):
29+
return self.add_point(other)
30+
else:
31+
return self.add_tuple(other)
32+
33+
def __radd__(self, other): # Make point addition commutative
34+
return self.__add__(other)
35+
36+
def add_point(self, other):
37+
sumpoint = Point()
38+
x3 = self.x + other.x
39+
y3 = self.y + other.y
40+
sumpoint.x = x3
41+
sumpoint.y = y3
42+
return sumpoint
43+
44+
def add_tuple(self, twople):
45+
sumpoint = Point()
46+
x3 = self.x + twople[0]
47+
y3 = self.y + twople[1]
48+
sumpoint.x = x3
49+
sumpoint.y = y3
50+
return sumpoint
2151

2252
def print_point(p):
2353
"""Print a Point object in human-readable format."""
@@ -60,7 +90,7 @@ def main():
6090
blank.x = 3
6191
blank.y = 4
6292
print('blank', end=' ')
63-
print_point(blank)
93+
print(blank)
6494

6595
box = Rectangle()
6696
box.width = 100.0
@@ -71,14 +101,19 @@ def main():
71101

72102
center = find_center(box)
73103
print('center', end=' ')
74-
print_point(center)
104+
print(center)
75105

76106
print(box.width)
77107
print(box.height)
78108
print('grow')
79109
grow_rectangle(box, 50, 100)
80110
print(box.width)
81111
print(box.height)
112+
three = Point(2, 3)
113+
four = Point(4,5)
114+
print(three+four)
115+
print(three + (1,1))
116+
print((1,1) + four)
82117

83118

84119
if __name__ == '__main__':

‎code/Time1.py‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@ class Time:
1717
1818
attributes: hour, minute, second
1919
"""
20+
def print_time(self):
21+
"""Prints a string representation of the time.
2022
23+
t: Time object
24+
"""
25+
print('%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second))
2126

22-
def print_time(t):
23-
"""Prints a string representation of the time.
24-
25-
t: Time object
26-
"""
27-
print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))
27+
def time_to_int(self):
28+
"""Computes the number of seconds since midnight.
2829
30+
time: Time object.
31+
"""
32+
minutes = self.hour * 60 + self.minute
33+
seconds = minutes * 60 + self.second
34+
return seconds
2935

3036
def int_to_time(seconds):
3137
"""Makes a new Time object.
@@ -38,14 +44,6 @@ def int_to_time(seconds):
3844
return time
3945

4046

41-
def time_to_int(time):
42-
"""Computes the number of seconds since midnight.
43-
44-
time: Time object.
45-
"""
46-
minutes = time.hour * 60 + time.minute
47-
seconds = minutes * 60 + time.second
48-
return seconds
4947

5048

5149
def add_times(t1, t2):
@@ -56,7 +54,7 @@ def add_times(t1, t2):
5654
returns: Time
5755
"""
5856
assert valid_time(t1) and valid_time(t2)
59-
seconds = time_to_int(t1) + time_to_int(t2)
57+
seconds = t1.time_to_int() + t2.time_to_int()
6058
return int_to_time(seconds)
6159

6260

@@ -82,18 +80,18 @@ def main():
8280
noon_time.second = 0
8381

8482
print('Starts at', end=' ')
85-
print_time(noon_time)
83+
noon_time.print_time()
8684

8785
# and the run time of the movie is 109 minutes...
8886
movie_minutes = 109
8987
run_time = int_to_time(movie_minutes * 60)
9088
print('Run time', end=' ')
91-
print_time(run_time)
89+
run_time.print_time()
9290

9391
# what time does the movie end?
9492
end_time = add_times(noon_time, run_time)
9593
print('Ends at', end=' ')
96-
print_time(end_time)
94+
end_time.print_time()
9795

9896

9997
if __name__ == '__main__':

0 commit comments

Comments
 (0)