Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

need help with get and set method

awillieawillie Posts: 17Member

This is what I have so for. I create a class call Rectangle. It has the attributes length and width with default 1. It has the method that calculate the perimeter and area of the rectangle. It has get and set method for length and width with floating point numbers larger than 0 and less than 20. This is what I have.


class Rectangle:

def __init__(self, length=1, width=1):
self._length = 1
self._width = 1

def setperimeter( self, length, width ):
self.setLength( length )
self.setWidth( width )

def setLength ( self, length ):

if 0 <= length < 20:
self._length = length
else:
raise ValueError, % length

def setWidth ( self, width ):

if 0 <= width < 20:
self._width = width
else:
raise ValueError, % width

def getLength ( self ):

return self._length

def getWidth ( self ):

return self._width

def printPerimeter( self ):

print %
( self._length, self._width ),

def printArea( self ):

Comments

  • infidelinfidel Posts: 2,900Member
    [b][red]This message was edited by Moderator at 2004-2-25 8:17:23[/red][/b][hr]
    [b][red]This message was edited by Moderator at 2004-2-25 8:15:20[/red][/b][hr]
    helpful tip, HTML does not preserve whitespace, so when posting code, especially python code which is dependent on whitespace, use the [leftbr]code[rightbr] and [leftbr]/code[rightbr] stylecodes.

    : This is what I have so for. I create a class call Rectangle. It has the attributes length and width with default 1. It has the method that calculate the perimeter and area of the rectangle. It has get and set method for length and width with floating point numbers larger than 0 and less than 20. This is what I have.
    :
    [code]
    : class Rectangle:
    :
    : def __init__(self, length=1, width=1):
    : self._length = 1
    : self._width = 1
    :
    : def setperimeter( self, length, width ):
    : self.setLength( length )
    : self.setWidth( width )
    :
    : def setLength ( self, length ):
    :
    : if 0 <= length < 20:
    : self._length = length
    : else:
    : raise ValueError, % length
    :
    : def setWidth ( self, width ):
    :
    : if 0 <= width < 20:
    : self._width = width
    : else:
    : raise ValueError, % width
    :
    : def getLength ( self ):
    :
    : return self._length
    :
    : def getWidth ( self ):
    :
    : return self._width
    :
    : def printPerimeter( self ):
    :
    : print %
    : ( self._length, self._width ),
    :
    : def printArea( self ):

    [/code]

    I'm guessing you're getting syntax errors. You have a lot of % operators in invalid places.

    Here is how I would code a Rectangle

    [code]
    class Size(object):

    def __init__(self, width, height):
    self.width = width
    self.height = height

    class Rectangle(object):

    def __init__(self, size = Size(1,1)):
    self._width = 0
    self._height = 0
    self.size = size

    def setSize(self, size):
    if isinstance(size, Size):
    self._width = size.width
    self._height = size.height
    elif isinstance(size, tuple):
    if len(size) != 2:
    raise ValueError('invaild number of dimensions')
    self._width = size[0]
    self._height = size[1]
    else:
    raise TypeError('invalid data type')

    def getSize(self):
    return Size(self._width, self._height)

    size = property(getSize, setSize)

    def setWidth(self, width):
    if isinstance(width, int):
    if 0 <= width < 20:
    self._width = width
    else:
    raise ValueError('width must be 0 <= width < 20')
    else:
    TypeError('invalid data type')

    def getWidth(self):
    return self._width

    width = property(getWidth, setWidth)

    def setHeight(self, height):
    if isinstance(height, int):
    if 0 <= height < 20:
    self._height = height
    else:
    raise ValueError('height must be 0 <= height < 20')
    else:
    TypeError('invalid data type')

    def getHeight(self):
    return self._height

    height = property(getHeight, setHeight)

    def area(self):
    return self._height * self._width

    def perimeter(self):
    return (self._height * 2) + (self._width * 2)
    [/code]

    And here is how this code can be used:

    [code]
    >>> from shapes import Rectangle, Size
    >>> r = Rectangle(Size(10.5, 10.5))
    >>> r.area()
    110.25
    >>> r.perimeter()
    42.0
    >>> r.size.width
    10.5
    >>> r.size.height
    10.5
    >>> r.height = 21
    Traceback (most recent call last):
    File "", line 1, in ?
    File "k:srcpykurosch
    ect.py", line 48, in setHeight
    raise ValueError('height must be 0 <= height < 20')
    ValueError: height must be 0 <= height < 20
    >>> r.size = (3, 5)
    >>> r.area()
    15
    >>>
    [/code]

    I personally don't like putting print statements inside class methods except for debugging purposes. It makes it hard to change the output destination if you want something other than stdout.

    Your biggest problems seem to be in understanding how the print and % statements work.

    And FYI, the perimeter is the sum of ALL sides, not just the height and width.

    Anyways, you didn't ask any specific questions so this is a pretty general "answer". Let me know if you need anything else.

    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]





  • awillieawillie Posts: 17Member
    : [b][red]This message was edited by Moderator at 2004-2-25 8:17:23[/red][/b][hr]
    : [b][red]This message was edited by Moderator at 2004-2-25 8:15:20[/red][/b][hr]
    : helpful tip, HTML does not preserve whitespace, so when posting code, especially python code which is dependent on whitespace, use the [leftbr]code[rightbr] and [leftbr]/code[rightbr] stylecodes.
    :
    : : This is what I have so for. I create a class call Rectangle. It has the attributes length and width with default 1. It has the method that calculate the perimeter and area of the rectangle. It has get and set method for length and width with floating point numbers larger than 0 and less than 20. This is what I have.
    : :
    : [code]
    : : class Rectangle:
    : :
    : : def __init__(self, length=1, width=1):
    : : self._length = 1
    : : self._width = 1
    : :
    : : def setperimeter( self, length, width ):
    : : self.setLength( length )
    : : self.setWidth( width )
    : :
    : : def setLength ( self, length ):
    : :
    : : if 0 <= length < 20:
    : : self._length = length
    : : else:
    : : raise ValueError, % length
    : :
    : : def setWidth ( self, width ):
    : :
    : : if 0 <= width < 20:
    : : self._width = width
    : : else:
    : : raise ValueError, % width
    : :
    : : def getLength ( self ):
    : :
    : : return self._length
    : :
    : : def getWidth ( self ):
    : :
    : : return self._width
    : :
    : : def printPerimeter( self ):
    : :
    : : print %
    : : ( self._length, self._width ),
    : :
    : : def printArea( self ):
    :
    : [/code]
    :
    : I'm guessing you're getting syntax errors. You have a lot of % operators in invalid places.
    :
    : Here is how I would code a Rectangle
    :
    : [code]
    : class Size(object):
    :
    : def __init__(self, width, height):
    : self.width = width
    : self.height = height
    :
    : class Rectangle(object):
    :
    : def __init__(self, size = Size(1,1)):
    : self._width = 0
    : self._height = 0
    : self.size = size
    :
    : def setSize(self, size):
    : if isinstance(size, Size):
    : self._width = size.width
    : self._height = size.height
    : elif isinstance(size, tuple):
    : if len(size) != 2:
    : raise ValueError('invaild number of dimensions')
    : self._width = size[0]
    : self._height = size[1]
    : else:
    : raise TypeError('invalid data type')
    :
    : def getSize(self):
    : return Size(self._width, self._height)
    :
    : size = property(getSize, setSize)
    :
    : def setWidth(self, width):
    : if isinstance(width, int):
    : if 0 <= width < 20:
    : self._width = width
    : else:
    : raise ValueError('width must be 0 <= width < 20')
    : else:
    : TypeError('invalid data type')
    :
    : def getWidth(self):
    : return self._width
    :
    : width = property(getWidth, setWidth)
    :
    : def setHeight(self, height):
    : if isinstance(height, int):
    : if 0 <= height < 20:
    : self._height = height
    : else:
    : raise ValueError('height must be 0 <= height < 20')
    : else:
    : TypeError('invalid data type')
    :
    : def getHeight(self):
    : return self._height
    :
    : height = property(getHeight, setHeight)
    :
    : def area(self):
    : return self._height * self._width
    :
    : def perimeter(self):
    : return (self._height * 2) + (self._width * 2)
    : [/code]
    :
    : And here is how this code can be used:
    :
    : [code]
    : >>> from shapes import Rectangle, Size
    : >>> r = Rectangle(Size(10.5, 10.5))
    : >>> r.area()
    : 110.25
    : >>> r.perimeter()
    : 42.0
    : >>> r.size.width
    : 10.5
    : >>> r.size.height
    : 10.5
    : >>> r.height = 21
    : Traceback (most recent call last):
    : File "", line 1, in ?
    : File "k:srcpykurosch
    ect.py", line 48, in setHeight
    : raise ValueError('height must be 0 <= height < 20')
    : ValueError: height must be 0 <= height < 20
    : >>> r.size = (3, 5)
    : >>> r.area()
    : 15
    : >>>
    : [/code]
    :
    : I personally don't like putting print statements inside class methods except for debugging purposes. It makes it hard to change the output destination if you want something other than stdout.
    :
    : Your biggest problems seem to be in understanding how the print and % statements work.
    :
    : And FYI, the perimeter is the sum of ALL sides, not just the height and width.
    :
    : Anyways, you didn't ask any specific questions so this is a pretty general "answer". Let me know if you need anything else.
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :
    :
    :
    :
    :

Sign In or Register to comment.