Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am creating a program in Python that will utilize object oriented programming to print the properties of a given rectangle. The project has these given constraints:

The purpose of this lab is to give you practice creating your own object. You will be given a main function that expects an instance of the (yet undefined) Rectangle object. Your job is to determine what attributes and methods the Rectangle class requires and create a class that will satisfy the requirements.

  • Add only one feature at a time
  • You may need to comment out parts of the main function for testing
  • Constructor should take 0, 1, or 2 parameters (illustrating polymorphism)
  • Your class should be a subclass of something (illustrating inheritance)
  • Your class should have methods and properties (illustrating encapsulation)
  • Make your instance variables hidden (using the __ trick)
  • Add setter and getter methods for each instance variable
  • Use properties to encapsulate instance variable access
  • Not all instance variables are real... Some are derived, and should be write-only
  • You may not substantially change the main function (unless you're doing the blackbelt challenge)
  • Be sure to add the needed code to run the main function when needed

Here is the rubric

  • Code: main() function is relatively unchanged 3
  • Code: Rectangle class is declared with defaults so it supports 0, 1 and 2 parameters 3
  • Code: Instantiates Rectangle(5,7) 2
  • Code: Instantiates Rectangle() 2
  • Code: Rectangle class defines __ instance variables 2
  • Code: Defines getters and setters for each instance variable 2
  • Code: Rectangle class include area and perimeter methods 4
  • Code: Rectangle class inherits from something, even if it's object 2
  • Code: Rectangle class defines width and length properties 4
  • Code: Rectangle includes derived read-only instance variables 2
  • Code: Invokes main when the python file is executed as main 2
  • Code: Rectangle class defines getStats() method that returns a string 4
  • Execution: prints Rectangle a: 1
  • Execution: prints area: 35 1
  • Execution: prints perimeter: 24 1
  • Execution: prints Rectangle b: 1
  • Execution: prints width: 10 1
  • Execution: prints height: 20 1
  • Execution: prints area: 200 1
  • Execution: prints perimeter: 60 1

Score 40

I am given this code to start off with:

def main():
print "Rectangle a:"
a = Rectangle(5, 7)
print "area:      %d" % a.area
print "perimeter: %d" % a.perimeter

print ""
print "Rectangle b:"
b = Rectangle()
b.width = 10
b.height = 20
print b.getStats()

I am supposed to get this output:

Rectangle a:
area:      35
perimeter: 24
Rectangle b:
width:     10
height:    20
area:      200
perimeter: 60

I have gotten this far but I can not get Rectangle B to print the width and height Can you please take a look?

class Rectangle:
    def __init__(self, width=0, height=0):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

    def perimeter(self):
        return 2 * self.height + 2 * self.width

    def setWidth(self, width):
        self.width = width

    def setHeight(self, height):
        self.height = height

    def getStats(self):
        return "area:      %s\nperimeter: %s" % (self.area(), self.perimeter())


def main():
    print ""
    print "Rectangle a:"
    a = Rectangle(5, 7)
    print "area:      %s" % a.area()
    print "perimeter: %s" % a.perimeter()

    print ""
    print "Rectangle b:"
    b = Rectangle()
    b.width = 10
    b.height = 20
    print b.getStats()
    print ""


main()

I am currently getting this output:

Rectangle a:
area:      35
perimeter: 24

Rectangle b:
area:      200
perimeter: 60


Process finished with exit code 0
share|improve this question
    
Note you haven't fulfilled requirements 4 and 6. – Daniel Roseman Jun 20 at 20:20

1 Answer 1

up vote 0 down vote accepted

Not sure I got your question right, but you may want to try:

def getStats(self):
    return "width:      %s\nheight:      %s\narea:      %s\nperimeter: %s" % (self.width, self.height, self.area(), self.perimeter())

To satisfy requirements 4 and 6, I would suggest something like:

class Shape(object):
    def area(self):
        raise NotImplementedError

    def perimeter(self):
        raise NotImplementedError

class Rectangle(Shape):
    def __init__(self, width=0, height=0):
        super(Rectangle, self).__init__() # this is useless in this case, but it's good practice 
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

    def perimeter(self):
        return 2 * self.height + 2 * self.width

    @property
    def width(self):
        return self.__width

    @property
    def height(self):
        return self.__height

    @width.setter
    def width(self, width):
        self.__width = width

    @height.setter
    def height(self, height):
        self.__height = height

    def getStats(self):
        return "width:      %s\nheight:      %s\narea:      %s\nperimeter: %s" % (self.width, self.height, self.area(), self.perimeter())
share|improve this answer
    
Yes, this helped. I am now getting the correct output but I'm still not fulfilling requirements 4 and 6 – cbandara Jun 20 at 20:32
    
Ok, I'll edit my answer to cover those points. – poros Jun 20 at 20:33
    
I have posted the official rubric in my answer if that helps – cbandara Jun 20 at 20:39
    
Done. I hope this answer to your additional question. – poros Jun 20 at 20:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.