3.2k views
5 votes
Complete the Rectangle class. It has a rectangle's length and width as class variables and contains methods to compute its area and perimeter. If either parameter is negative, the method should calculate based on its absolute value. Then, you'll take user inputs to create an instance of Rectangle. For example, if you provide the input -13 7 You should receive the output Perimeter = 40.0 Area = 91.0 Note: It is NOT necessary to use the math module to calculate absolute value, though you may if you want. 298800.1775650.qx3zqy7 LAB ACTIVITY 28.4.1: Final Exam 2021 - Problem 4 of 6 0/10 main.py Load default template... 1 class Rectangle: _init__(self, length, width): # your code here 4 5 def area (self): 6 # your code here 7 8 def perimeter (self): 9 # your code here 10 11 if _name__ == '__main__': 12 1 = float(input) 13 w = float(input) 14 r = Rectangle(1, w) 15 print("Perimeter =', r.perimeter()) 16 print('Area =', r.area(), end='')|

User Shadrx
by
6.2k points

1 Answer

4 votes

Answer:

The complete program is as follows:

class Rectangle:

def __init__(self, length, width):

self.width = abs(width)

self.length = abs(length)

def area(self):

area = self.length * self.width

return area

def perimeter (self):

perimeter = 2 * (self.length + self.width)

return perimeter

if __name__ == '__main__':

l = float(input(": "))

w = float(input(": "))

r = Rectangle(l, w)

print('Perimeter =', r.perimeter())

print('Area =', r.area(), end='')

Step-by-step explanation:

This defines the class:

class Rectangle:

This gets the parameters from maiin

def __init__(self, length, width):

This sets the width

self.width = abs(width)

This sets the length

self.length = abs(length)

The area function begins here

def area(self):

Calculate the area

area = self.length * self.width

Return the calculated area back to main

return area

The perimeter function begins here

def perimeter (self):

Calculate the perimeter

perimeter = 2 * (self.length + self.width)

Return the calculated perimeter back to main

return perimeter

The main begins here

if __name__ == '__main__':

This gets input for length

l = float(input(": "))

This gets input for width

w = float(input(": "))

This passes the inputs to the class

r = Rectangle(l, w)

This prints the returned value for perimeter

print('Perimeter =', r.perimeter())

This prints the returned value for area

print('Area =', r.area(), end='')

User Hair
by
6.1k points