Object Oriented Python – rectangle using classes and functions

Question:

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:      %snperimeter: %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
Asked By: cbandara

||

Answers:

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

def getStats(self):
    return "width:      %snheight:      %snarea:      %snperimeter: %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:      %snheight:      %snarea:      %snperimeter: %s" % (self.width, self.height, self.area(), self.perimeter())
Answered By: poros

NOTE: This homework is an exact assignment from my computer science class. While you are welcome to get help from sites like stack overflow, you are still responsible for your own work, and if you turn in code from this site (which may or may not be acceptable) we will know, and we will consider it academic misconduct. We may also have made some quiet changes to the assignment, so if you turn this answer in without careful consideration, you are unlikely to get full credit even

Answered By: Two pi
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.