0

I've been assig to do the class rectangle, the class has two instance variables: length and width of the rectangle, and the methods methods setLength(), getLength(), setWidth(), getWidth() and computeArea(). Do you have any idea to do this. Anyway i'm to to python.

4
Contributors
3
Replies
20
Views
3 Years
Discussion Span
Last Post by ZZucker
Featured Replies
  • Start by declaring a class named `Rectangle`. Write a constructor (an `__init__()` method) that accepts arguments `self`, `length` and `width`, and assign the latter two to `self.length` and `self.width` inside the constructor. Read More

1

Start by declaring a class named Rectangle. Write a constructor (an __init__() method) that accepts arguments self, length and width, and assign the latter two to self.length and self.width inside the constructor.

Edited by Schol-R-LEA

0

This basic class example should send you on your Python way:

class Rectangle:
    # class constructor
    # supply it with initial length and width values
    # self refers to the instance
    def __init__(self, length, width):
        self.width = width
        self.length = length

    def setLength(self, length):
        self.length = length

    def show(self):
        print("rectangle has length = {}".format(self.length))
        print("rectangle has width = {}".format(self.width))


length = 10
width = 5
# create a class instance
rect = Rectangle(length, width)

# show initial setting
rect.show()

''' result >>
rectangle has length = 10
rectangle has width = 5
'''

print('-'*40)  # a line of  dashes
# now set length to another value
rect.setLength(17)

# show new setting
rect.show()

''' result >>
rectangle has length = 17
rectangle has width = 5
'''

Edited by ZZucker

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.