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:

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

Note: Unlike the co-ordinate system you may have encountered before, y, in this question, increases as you move vertically downwards. Example: Be careful to get the spacing right for str - i.e. comma followed by space.
>>> r = rectangle((2,3), 5, 6)
>>> str(r)
'((2, 3),(7, 9))'
>>> r.move((5,5))
>>> str(r)
'((5, 5), (10, 11))'
>>> r.resize(1,1)
>>> str(r)
'((5, 5), (6, 6))'
>>> r.getBottomright()
(6, 6)


if any can help me with I would be thankful for him or her

Recommended Answers

All 6 Replies

Some code and a clear explanation of the problem you have would help.

Cheers and Happy coding

actually I have no idea how to code it
but the out come of the program shoud be
>>> r = rectangle((2,3), 5, 6)
>>> str(r)
'((2, 3),(7, 9))'
>>> r.move((5,5))
>>> str(r)
'((5, 5), (10, 11))'
>>> r.resize(1,1)
>>> str(r)
'((5, 5), (6, 6))'
>>> r.getBottomright()
(6, 6)

I get it now.

Here it is. All you need. Classes

Cheers and Happy coding

thanks a lot

A technical term for creating a class is 'declare'.

When you create a class you are declaring a class, and when you see a class in code it is a class declaration. Just thought you might want to know the lingo.

b

You must understand the question and logics first.

And as lrh9 said, and very good, you declare classes and define their functions.

class rectangle():

    def __init__(self, coords, sizex, sizey):
        self._startx, self._starty = coords
        self._sizex = sizex
        self._sizey = sizey

    def getBottomright(self):
        print '(%s, %s)' % (self._startx + self._sizex, self._starty + self._sizey)

    def move(self, pos):
        self._startx, self._starty = pos

    def resize(self, width, height):
        self._sizex = width
        self._sizey = height

    def __str__(self):
        return '((%s, %s), (%s, %s))' % (self._startx, self._starty, self._startx + self._sizex, self._starty + self._sizey)


r = rectangle((2, 3), 5, 6)
print str(r)

"""'((2, 3), (7, 9))'"""

r.move((5, 5))
print str(r)

"""'((5, 5), (10, 11))'"""

r.resize(1,1)
print str(r)

"""'((5, 5), (6, 6))'"""

r.getBottomright()

"""(6, 6)"""

Cheers and Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.