-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture.py
More file actions
95 lines (75 loc) · 3.17 KB
/
lecture.py
File metadata and controls
95 lines (75 loc) · 3.17 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
88
89
90
91
92
93
94
95
###### Lecture 8 ######
class Rectangle():
def __init__(self, coord1, coord2):
"""Creates a rectangle object defined
by two corners, given by coord1 and coord2.
coord1 and coord2: tuples of x,y values."""
self.corner1 = coord1
self.corner2 = coord2
self.color = None
def getCorners(self):
"""returns the coordinates of both corners"""
return self.corner1, self.corner2
def getCorner1(self):
"""returns the coordinates of corner1"""
return self.corner1
def getCorner2(self):
"""returns the coordinates of corner2"""
return self.corner2
def getColor(self):
"""returns the rectangles color"""
return self.color
def setCorner1(self, newCoord):
"""assigns corner1 to a new point"""
self.corner1 = newCoord
def setCorner2(self, newCoord):
"""assigns corner2 to a new point"""
self.corner2 = newCoord
def setColor(self, color):
"""sets the rectangle's color
color: a string"""
self.color = color
def getArea(self):
"""returns the area of the rectangle"""
side1Length = abs(self.corner1[0] - self.corner2[0])
side2Length = abs(self.corner1[1] - self.corner2[1])
return side1Length * side2Length
def getPerimeter(self):
"""returns the perimeter of the rectangle"""
side1Length = abs(self.corner1[0] - self.corner2[0])
side2Length = abs(self.corner1[1] - self.corner2[1])
return 2 * side1Length + 2 * side2Length
rectangle1 = Rectangle((0,0), (5,5))
rectangle1.setColor("blue")
print rectangle1.color # "blue"
rectangle2 = Rectangle((0,5), (5,0))
print rectangle2.color # None because we never set a color
class Square(Rectangle): # child of Rectangle; a Square *is* a Rectangle
def __init__(self, coord1, width):
"""Creates a Square instance by calling the Rectangle
constructor.
coord1: The lower left corner of the square; (x,y) tuple
width: int or float length of side"""
# We're using inheritance to call the Rectangle class
# by only taking one point as an argument to the Square
# __init__ method, we can calculate the other point
# and ensure the object is actually a square
Rectangle.__init__(self, coord1, (coord1[0] + width, coord1[1] + width))
def getArea(self):
"""returns the area of the square"""
# Technically, someone could have accessed the setCorner
# methods from Rectangle to mess things up, but we're
# going to assume that can't happen for simplicity's sake.
sideLength = self.corner1[0] - self.corner2[0]
return sideLength ** 2
def getPerimeter(self):
"""returns the perimeter of the square"""
sideLength = self.corner2[0] - self.corner1[0]
return sideLength * 4
square1 = Square((0,0), 5) # creates a square has the same coordinates as
# the Rectangle from earlier
square1.getArea()
square1.setColor("hot pink")
print "square1 color:", square1.getColor()
Square.setColor(square1, "hot red") # reminder of how self is assigned to arguments
print "square1 color:", square1.getColor()