Python Rectangle Class: Understanding Objects, Methods, and Inheritance, Exams of Object Oriented Programming

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

2021/2022

Uploaded on 09/27/2022

explain
explain 🇺🇸

4

(2)

230 documents

1 / 47

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 11
More Object-Oriented Programming
based in part on notes from the CS-for-All curriculum developed at Harvey Mudd College
1
Unrelated but do you see how happy dogs make Milla???
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

Download Python Rectangle Class: Understanding Objects, Methods, and Inheritance and more Exams Object Oriented Programming in PDF only on Docsity!

Lecture 11

More Object-Oriented Programming

based in part on notes from the CS-for-All curriculum developed at Harvey Mudd College

Unrelated but do you see how happy dogs make Milla???

Recall: Our Rectangle Class

rectangle.py

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 *

construct two Rectangle objects

r1 = Rectangle(100, 50) r2 = Rectangle(75, 350)

print dimensions and area of each

print('r1:', r1.width, 'x', r1.height) print('area =', r1.area()) print('r2:', r2.width, 'x', r2.height) print('area =', r2.area())

grow both Rectangles

r1.grow(50, 10) r2.grow(5, 30)

print new dimensions

print('r1:', r1.width, 'x', r1.height) print('r2:', r2.width, 'x', r2.height)

Recall: Our Rectangle Class

rectangle.py

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 ==)

  • The eq method of a class allows us to implement our own version of the == operator.
  • If we don't write a eq method for a class, we get a default version that compares the object's memory addresses - see the previous example!

repr (Printing/Evaluating an Object)

  • The repr method of a class returns a string representation of objects of that class.
  • It gets called when you:
  • evaluate an object in the Shell: >> r1 = Rectangle(100, 80) >> r1 # calls repr
  • apply str(): >> r1string = str(r1) # also calls repr
  • print an object: >> print(r1) # also calls repr

repr (Printing/Evaluating an Object)

  • If we don't write a repr method for a class, we get a default version that isn't very helpful! >>> r2 = Rectangle(50, 20) >>> r <main.Rectangle object at 0x03247C30>

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...

from rectangle import *

# Construct two Rectangle objects

r1 = Rectangle(100, 50)

r2 = Rectangle(75, 350)

# Print dimensions and area of each

print('r1:', r1.width, 'x', r1.height)

print('area =', r1.area())

print('r2:', r2.width, 'x', r2.height)

print('area =', r2.area())

# grow both Rectangles

r1.grow(50, 10)

r2.grow(5, 30)

# Print new dimensions

print('r1:', r1.width, 'x', r1.height)

print('r2:', r2.width, 'x', r2.height)