Question

In this lab, you create a programmer-defined class and then use it in a Python program....

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 named length and width.
  3. Write a public calculateArea method and a public calculatePerimeter method 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 of rectangle1 to 10.0 and the width to 5.0. Set the length of rectangle2 to 7.0 and the width to 3.0.
  6. Print the value of rectangle1’s perimeter and area, and then print the value of rectangle2’s perimeter and area.
  7. Execute the program and verify that the output is correct.

*My Rectangle Class Program

# This program uses the programmer-defined Rectangle class.

# Do NOT modify this program. Write your code in Rectangle.py,
# then select this file and click "Run Code".

from Rectangle import Rectangle

rectangle1 = Rectangle(10.0, 5.0)
rectangle2 = Rectangle(7.0, 3.0)

print("Perimeter of rectangle1 is " + str(rectangle1.calculatePerimeter()))
print("Area of rectangle1 is " + str(rectangle1.calculateArea()))
print("Perimeter of rectangle2 is " + str(rectangle2.calculatePerimeter()))
print("Area of rectangle2 is " + str(rectangle2.calculateArea()))

*Rectangle

# Rectangle.py

class Rectangle(object):
    # Declare public methods here
    def __init__(self, length, width):
        # Set class instance variables here

    def calculateArea(self):
        # Write calculateArea here

    def calculatePerimeter(self):
        # Write calculatePerimeter here

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN PYTHON:

Rectangle.py

class Rectangle(object):

#parametrized constructor

def __init__(self,length,width):

self.length=length

self.width=width

#method to calculate and return area

def calculateArea(self):

return self.length*self.width

#method to calculate and return perimeter

def calculatePerimeter(self):

return 2*(self.length+self.width)

MyRectangleClassProgram.py

from Rectangle import Rectangle

#testing the Rectangle class

rectangle1 = Rectangle(10.0, 5.0)

rectangle2 = Rectangle(7.0, 3.0)

print("Perimeter of rectangle1 is " + str(rectangle1.calculatePerimeter()))

print("Area of rectangle1 is " + str(rectangle1.calculateArea()))

print("Perimeter of rectangle2 is " + str(rectangle2.calculatePerimeter()))

print("Area of rectangle2 is " + str(rectangle2.calculateArea()))

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
In this lab, you create a programmer-defined class and then use it in a Python program....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT