







































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth analysis of a Python Rectangle class, including its methods, attributes, and interactions with other classes. It covers topics such as object creation, method definitions, and inheritance. Students can use this document as a study guide for understanding object-oriented programming concepts.
Typology: Exams
1 / 47
This page cannot be seen from the preview
Don't miss anything!








































based in part on notes from the CS-for-All curriculum developed at Harvey Mudd College
Recall: Our Rectangle Class
class Rectangle: def init(self, init_width, init_height): self.x = 0 self.y = 0 self.width = init_width self.height = init_height def grow(self, dwidth, dheight): self.width += dwidth self.height += dheight def area(self): return self.width * self.height def perimeter(self): return 2self.width + 2self.height def scale(self, factor): self.width *= factor self.height *= factor
Simplified Client Program from rectangle import *
r1 = Rectangle(100, 50) r2 = Rectangle(75, 350)
print('r1:', r1.width, 'x', r1.height) print('area =', r1.area()) print('r2:', r2.width, 'x', r2.height) print('area =', r2.area())
r1.grow(50, 10) r2.grow(5, 30)
print('r1:', r1.width, 'x', r1.height) print('r2:', r2.width, 'x', r2.height)
Recall: Our Rectangle Class
class Rectangle: def init(self, init_width, init_height): self.x = 0 self.y = 0 self.width = init_width self.height = init_height def grow(self, dwidth, dheight): self.width += dwidth self.height += dheight def area(self): return self.width * self.height def perimeter(self): return 2self.width + 2self.height **def scale(self, factor): self.width = factor self.height = factor
What is the output of this program? A. 40 40 40 B. 80 40 40 C. 80 40 80 D. 80 80 80 E. none of these from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r r1.scale(2) print(r1.width, r2.width, r3.width)
What is the output of this program? from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r r1.scale(2) print(r1.width, r2.width, r3.width) height 75 width 40 y 0 x 0 height 75 width 40 y 0 x 0 r r global r
What is the output of this program? from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r r1.scale(2) # changes are still inside the object! print(r1.width, r2.width, r3.width) height 75 width 40 y 0 x 0 height 150 width 80 y 0 x 0 r r global r
What is the output of this program? from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r r1.scale(2) print(r1.width, r2.width, r3.width) height 75 width 40 y 0 x 0 height 150 width 80 y 0 x 0 r r global r output: 80 40 80
What is the output of this client program? from rectangle import * r1 = Rectangle(40, 75) r2 = Rectangle(40, 75) r3 = r print(r1 == r2) # outputs False print(r1 == r3) # outputs True height 75 width 40 y 0 x 0 height 75 width 40 y 0 x 0 r r global r
eq (Implementing Our Own ==)
repr (Printing/Evaluating an Object)
repr (Printing/Evaluating an Object)
Updated Rectangle Class class Rectangle: def init(self, init_width, init_height): ... def grow(self, dwidth, dheight): self.width += dwidth self.height += dheight def area(self): return self.width * self.height def perimeter(self): return 2self.width + 2self.height def scale(self, factor): self.width *= factor self.height *= factor def eq(self, other): if self.width == other.width and self.height == other.height: return True return False def repr(self): return str(self.width) + ' x ' + str(self.height)
Simplifying the Client Program Again...