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