Question
In Python. So, a class Rectangle can be define as follow: class Rectangle: A class to manufacture rectangle objects def __init__(self, posn, w,
In Python.
So, a class Rectangle can be define as follow: 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(Point(0, 0), 100, 200) bomb = Rectangle(Point(100, 80), 5, 10) # In my video game print("box: ", box) print("bomb: ", bomb)
To create_rectangle Input parameters: x, y, width, height Return value: instance of Rectangle Operation: create a new instance of Rectangle To str_rectangle Input parameter: rect Return value: string Operation: convert given Rectangle instance into string of form (x, y, width, height) To shift_rectangle Input parameters: rect, dx, dy Return value: None Operation: change the x and y coordinates of the given Rectangle instance To offset_rectangle Input parameters: rect, dx, dy Return value: instance of Rectangle Operation: create a new Rectangle instance which is offset from the given instance in x and y coordinates by dx and dy respectively
Test your functions with the following code:
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)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started