Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: Write a function named moveRect that takes a Rectangle and two paramet

ID: 3619799 • Letter: Q

Question

Question: Write a function named moveRect that takes a
Rectangle and two parameters named dx and dy. It should
change the location of the rectangle by adding dx to the
x coordinate of corner and adding dy to the y coordinate of corner.
--
The reading for this question is below:
---
Objects are mutable
We can change the state of an object by making an assignment to one of its
attributes. For example, to change the size of a rectangle without changing its
position, we could modify the values of width and height:
box.width = box.width + 50
box.height = box.height + 100
We could encapsulate this code in a method and generalize it to grow the rectangle
by any amount:
def growRect(box, dwidth, dheight) :
   box.width = box.width + dwidth
   box.height = box.height + dheight12.8 Copying 135
The variables dwidth and dheight indicate how much the rectangle should grow
in each direction. Invoking this method has the effect of modifying the Rectangle
that is passed as an argument.

For example, we could create a new Rectangle named bob and pass it to
growRect:
>>> bob = Rectangle()
>>> bob.width = 100.0
>>> bob.height = 200.0
>>> bob.corner = Point()
>>> bob.corner.x = 0.0
>>> bob.corner.y = 0.0
>>> growRect(bob, 50, 100)
While growRect is running, the parameter box is an alias for bob. Any changes
made to box also affect bob. Question: Write a function named moveRect that takes a
Rectangle and two parameters named dx and dy. It should
change the location of the rectangle by adding dx to the
x coordinate of corner and adding dy to the y coordinate of corner.
--
The reading for this question is below:
---
Objects are mutable
We can change the state of an object by making an assignment to one of its
attributes. For example, to change the size of a rectangle without changing its
position, we could modify the values of width and height:
box.width = box.width + 50
box.height = box.height + 100
We could encapsulate this code in a method and generalize it to grow the rectangle
by any amount:
def growRect(box, dwidth, dheight) :
   box.width = box.width + dwidth
   box.height = box.height + dheight12.8 Copying 135
The variables dwidth and dheight indicate how much the rectangle should grow
in each direction. Invoking this method has the effect of modifying the Rectangle
that is passed as an argument.

For example, we could create a new Rectangle named bob and pass it to
growRect:
>>> bob = Rectangle()
>>> bob.width = 100.0
>>> bob.height = 200.0
>>> bob.corner = Point()
>>> bob.corner.x = 0.0
>>> bob.corner.y = 0.0
>>> growRect(bob, 50, 100)
While growRect is running, the parameter box is an alias for bob. Any changes
made to box also affect bob.

Explanation / Answer

class rectangle:
   height = 0;
   width = 0;

def growRect(box, dwidth, dheight):
   box.height = box.height + dheight;
   box.width = box.width + dheight;
   print box.height;
   print box.width;

box = rectangle();
box.height = 10;
box.width = 20;
growRect(box,30,30);
raw_input();
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote