-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint_and_Rectangle_Classes.py
More file actions
87 lines (68 loc) · 2.01 KB
/
Point_and_Rectangle_Classes.py
File metadata and controls
87 lines (68 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Mohammad Hossein Zehtab
# Advanced_Python_Wednesdays
# Project03: Point_and_Rectangle_Classes
def test(did_pass):
"""
Print the result of a test.
"""
if did_pass:
print('True')
else:
print('Failed')
class Point:
"""
A class for point objects with coordinates of x and y
"""
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"({self.x}, {self.y})"
class Rectangle:
"""
A class for rectangle objects with corner coordinate, width and height.
"""
def __init__(self, cornerpos, width, height):
self.corner = cornerpos
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return(self.width + self.height) * 2
def flip(self):
self.width, self.height = self.height,self.width
def contains(self, point1):
if self.corner.x <= point1.x < self.width\
and\
self.corner.y <= point1.y < self.height:
return True
else:
return False
def __str__(self):
return f"({self.corner}, {self.width}, {self.height})"
def test_suite():
"""
Run the suite of tests for code in this module.
"""
print("Question 1:")
r = Rectangle(Point(0, 0), 10, 5)
test(r.area() == 50)
print("\nQuestion 2:")
r = Rectangle(Point(0, 0), 10, 5)
test(r.perimeter() == 30)
print("\nQuestion 3:")
r = Rectangle(Point(100, 50), 10, 5)
test(r.width == 10 and r.height == 5)
r.flip()
test(r.width == 5 and r.height == 10)
print("\nQuestion 4:")
r = Rectangle(Point(0, 0), 10, 5)
test(r.contains(Point(0, 0)))
test(r.contains(Point(3, 3)))
test(not r.contains(Point(3, 7)))
test(not r.contains(Point(3, 5)))
test(r.contains(Point(3, 4.99999)))
test(not r.contains(Point(-3, -3)))
### Driver Code ###
test_suite()