Question

Defining a Class Define the class Rectangle. It’s constructor takes a pair of numbers representing the...

Defining a Class

Define the class Rectangle. It’s constructor takes a pair of numbers representing the top-left corner, and two other numbers representing the width and height. It has the following methods:

  • get_bottom_right() - return the bottom right corner as a pair of
    numbers.
  • move(p) - move the rectangle so that p becomes the top-left corner
    (leaving the width and height unchanged).
  • resize(width, height) - set the width and height of the rectangle
    to the supplied arguments (leaving the top-left corner unchanged).
  • __str__() - return the string representation of the rectangle as a
    pair of pairs - i.e. the top left and bottom right corners.

Note: this problem uses the coordinate system commonly used for computer graphics. In this system, the origin is in the top-left corner and the y-axis is flipped. The bottom-right corner will therefore have a larger y coordinate than the top-left corner.

Examples:
Each example below follows on from the previous one. Be careful to get the spacing right for str - i.e. comma followed by space.

>>> r = Rectangle((2,3), 5, 7)
>>> str(r)
'((2, 3), (7, 10))'
>>> r.move((5,6))
>>> str(r)
'((5, 6), (10, 13))'
>>> r.resize(2,3)
>>> str(r)
'((5,6), (7, 9))'
>>> r.get_bottom_right()
(7, 9)

Homework Answers

Answer #1

class Rectangle:
  
def __init__(self, top_left, width, height):
self.top_left = top_left
self.width = width
self.height = height
  
def get_bottom_right(self):
bottom_right=[0, 0]
bottom_right[0] = self.top_left[0] + self.width
bottom_right[1] = self.top_left[1] + self.height
return (bottom_right[0], bottom_right[1])
  
def move(self, p):
self.top_left = p
  
def resize(self, width, height):
self.width = width
self.height = height
  
def __str__(self):
bottom_right = self.get_bottom_right()
return str((self.top_left, self.get_bottom_right()))

The class definition is quite self-explanatory. Please go through it, and let me know if you need any further clarifications.

Also, please rate the answer.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle.java. Save the program to your directory and run it...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT