Question
I need assistance on this. Summary In this lab, you create a programmer-defined class and then use it in a Python program. The program should
I need assistance on this.
Summary
In this lab, you create a programmer-defined class and then use it in a Python program. The program should create two Rectangle objects and find their area and perimeter.
Instructions
- Open the class file named Rectangle.py
- In the Rectangle class, create two attributes namedlengthandwidth.
- Write a publiccalculateAreamethod and a publiccalculatePerimetermethod to calculate and return the area of the rectangle and the perimeter of the rectangle.
- Open the file named MyRectangleClassProgram.py.
- Set the length ofrectangle1to 10.0 and the width to 5.0. Set the length ofrectangle2to 7.0 and the width to 3.0.
- Print the value ofrectangle1's perimeter and area, and then print the value ofrectangle2's perimeter and area.
- Execute the program and verify that the output is correct.
code given:
# MyRectangleClassProgram.py
#Thisprogramusestheprogrammer-definedRectangleclass.
#DoNOTmodifythisprogram.WriteyourcodeinRectangle.py,
#thenselectthisfileandclick"RunCode".
fromRectangleimportRectangle
rectangle1=Rectangle(10.0,5.0)
rectangle2=Rectangle(7.0,3.0)
print("Perimeterofrectangle1is"+str(rectangle1.calculatePerimeter()))
print("Areaofrectangle1is"+str(rectangle1.calculateArea()))
print("Perimeterofrectangle2is"+str(rectangle2.calculatePerimeter()))
print("Areaofrectangle2is"+str(rectangle2.calculateArea()))
#Rectangle.py
classRectangle(object):
#Declarepublicmethodshere
def__init__(self,length,width):
#Setclassinstancevariableshere
defcalculateArea(self):
#WritecalculateAreahere
defcalculatePerimeter(self):
#WritecalculatePerimeterhere
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started