Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  1. Open the class file named Rectangle.py
  2. In the Rectangle class, create two attributes namedlengthandwidth.
  3. Write a publiccalculateAreamethod and a publiccalculatePerimetermethod to calculate and return the area of the rectangle and the perimeter of the rectangle.
  4. Open the file named MyRectangleClassProgram.py.
  5. Set the length ofrectangle1to 10.0 and the width to 5.0. Set the length ofrectangle2to 7.0 and the width to 3.0.
  6. Print the value ofrectangle1's perimeter and area, and then print the value ofrectangle2's perimeter and area.
  7. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions