I need to create a Rectangle data class and an executable program to test it in
ID: 3785074 • Letter: I
Question
I need to create a Rectangle data class and an executable program to test it in Python.
The following needs to be included:
NOTE: the area() method returns the area and perimeter() returns the perimeter. In the executable's main function:
•prompt the user to enter the length and width of a rectangle.
•create a new Rectangle instance with the dimensions entered by the user.
•to verify the above step, print both dimensions using their respective "getter" methods.
•test the area() method by printing the rectangle area accurate to two decimal places.
•test the perimeter() method by printing the rectangle perimeter accurate to two decimal places.
•change the length to 22.345 and change the width to 15.789.
•test the area() and perimeter() methods again. You should get the results shown in the sample output.
SAMPLE OUTPUT
Enter the length 12.45
Enter the width 8.973
Length: 12.45
Width: 8.973
Rectangle area is 111.71
Rectangle perimeter is 42.85
Changed rectangle area is 352.81
Changed rectangle perimeter is 76.27
Explanation / Answer
The rectangle data class in python can be like this:
class Rectangle:
def __init__(self, length, width):
self.l = length
self.w = width
def perimeter(self):
return (2 * self.l) + (2 * self.w)
def area(self):
return self.l * self.w
We can create the object and then call the method of rectangle class to get the area as well as the rectange.
>>> r = Rectangle(22.345,15.789)
>>> r.perimeter()
>>> r.area()
After calling above two methods you will get your result.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.