Module 5 4930
Daniel Tafmizi
Dr. Friedman
Lis 4930
June 17, 2024
Module 5
GitHub link: DanielDataGit/lis4930 (github.com)
Comments: This is my first time using the class object maker. I see how this can be used to create efficient and reproducible objects. I appreciate the way python classes are constructed and customized. Furthermore, I am starting to enjoy using the pycharm IDE. At first, I had difficulty using the applications features, but I have become more nuanced towards them. The IDE's autocomplete helped me a few times in this assignment.
Question: Is it respected to use the "posn" name as a local name in my functions? I re-used the name in my create and offset functions.
# module 5 assignment
class Rectangle:
""" A class to manufacture rectangle objects """
def __init__(self, posn, w, h):
""" Initialize rectangle at posn, with width w, height h """
self.corner = posn
self.width = w
self.height = h
def __str__(self):
return "({0}, {1}, {2})".format(self.corner, self.width, self.height)
box = Rectangle((0,0), 100, 200)
bomb = Rectangle((100, 80), 5, 10) # In my video game
print("box: ", box)
print("bomb: ", bomb)
def create_rectangle(x, y, width, height):
""" Create a rectangle instance"""
posn = (x, y)
return Rectangle(posn, width, height)
def str_rectangle(rect):
""" Return a string representation of a rectangle """
x, y = rect.corner
return "({0}, {1}, {2}, {3})".format(x, y, rect.width, rect.height)
def shift_rectangle(rect, dx, dy):
""" Shift rectangle by dx and dy """
rect.corner = (rect.corner[0] + dx, rect.corner[1] + dy)
def offset_rectangle(rect, dx, dy):
""" Create rectangle instance and offset rectangle by dx and dy """
posn = (rect.corner[0] + dx, rect.corner[1] + dy)
return Rectangle(posn, rect.width, rect.height)
r1 = create_rectangle(10, 20, 30, 40)
print(str_rectangle(r1))
shift_rectangle(r1, -10, -20)
print(str_rectangle(r1))
r2 = offset_rectangle(r1, 100, 100)
print(str_rectangle(r1)) # should be same as previous
print(str_rectangle(r2))
#run:
box: ((0, 0), 100, 200) bomb: ((100, 80), 5, 10) (10, 20, 30, 40) (0, 0, 30, 40) (0, 0, 30, 40) (100, 100, 30, 40) Process finished with exit code 0
Comments
Post a Comment