Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question for Help : Would ask for help in Python on how to Create a class Rectan

ID: 3828862 • Letter: Q

Question

Question for Help: Would ask for help in Python on how to Create a class Rectangle with double attributes length and width. The default constructor should set these attributes to :

1. I must Provide methods that calculate the rectangle's perimeter and area, as well as accessors/getters and mutators/setters for both data fields. The mutator methods for length and width should verify that the number being passed in is larger than 0.0 and less than 20.0 -- if it doesn't fit those criteria, the value of the field should not be changed. Additionally I have to implement two methods called makeCopy() and fromRectangle().

- makeCopy() is class method: takes a rectangle as input parameter and return the copy of that rectangle

- fromRectangle() is an instance method that takes a rectangle as input and copies the data attributes of rectangle into the calling object.

Write a function called main() in the same file to test your Rectangle class . It should prompt the user to enter a length and width of a rectangle, and then print out the area and perimeter of the rectangle. (Use the mutators to set the length and width of the rectangle, not the constructor .) Please give comments to I can better understand and study, thanks ahead for the teaching.

Sample runs Runs given:

>>>Enter length of rectangle:7.5

>>>Enter width of rectangle:3.0

Area: 22.5, Perimeter: 21.0

>>>Enter length of rectangle:23.4

>>>Enter width of rectangle:6.8

Area: 6.8, Perimeter: 15.6

>>>Enter length of rectangle:10.3

>>>Enter width of rectangle:-8.7

Area: 10.3, Perimeter: 22.6

>>>l2, w2 = 10.3, -8.7

>>>rect = Rectangle();

>>>rect.setLength(l2)

>>>rect.setWidth(w2)

>>>rect2 = Rectangle.makeCopy(rect)

>>>print(rect2)

Area: 10.3, Perimeter: 22.6

>>>l1, w1 = 7.5, 3

>>>rect = Rectangle();

>>>rect.setLength(l1)

>>>rect.setWidth(w1)

>>>rect2 = Rectangle()

>>>rect2.fromRectangle(rect)

>>>print(rect2)

Area: 22.5, Perimeter: 21.0

Explanation / Answer

class Rectangle:
def __init__(self):
self.length = 1
self.width = 1
def getLength(self):
return self.length
def setLength(self, length):
if length <= 0 or length > 20:
return
self.length = length
def getWidth(self):
return self.width
def setWidth(self, width):
if width <= 0 or width > 20:
return
self.width = width
  
def getArea(self):
return self.length * self.width
  
def getPerimeter(self):
return (self.length + self.width)*2
  
def __str__(self):
return "Area: " + "{0:.2f}".format(self.getArea()) + ", Perimeter: " + "{0:.2f}".format(self.getPerimeter())
  
@classmethod
def makeCopy(cls, other):
rec = Rectangle()
rec.setLength(other.length)
rec.setWidth(other.width)
return rec
  
  
  
def fromRectangle(self, other):
self.length = other.length
self.width = other.width
  
def main():
rect = Rectangle()
length = float(input("Enter length: "))
rect.setLength(length)
width = float(input("Enter width: "))
rect.setWidth(width)
print(rect)

if __name__ == '__main__':
main()
  
  #code link: https://paste.ee/p/ZNHFF

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote