Write a program that calculates the rectangle’s perimeter …

Write a program that calculates the rectangle’s perimeter and area. use set and get methods for both length and width. The set methods will verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle.

Answer

In order to address the given problem, we will need to create a program that implements the class “Rectangle” and includes the necessary methods to calculate the perimeter and area of a rectangle. The program will also need to include set and get methods for both the length and width of the rectangle, with these methods verifying that the values provided are floating-point numbers larger than 0.0 and less than 20.0.

Below is an implementation of the class “Rectangle” in Python:

“`python
class Rectangle:
def __init__(self, length, width):
self.__length = self.set_length(length)
self.__width = self.set_width(width)

def set_length(self, length):
if 0.0 < length < 20.0: return length else: raise ValueError("Invalid length value. Length must be a float between 0.0 and 20.0.") def set_width(self, width): if 0.0 < width < 20.0: return width else: raise ValueError("Invalid width value. Width must be a float between 0.0 and 20.0.") def get_length(self): return self.__length def get_width(self): return self.__width def calculate_perimeter(self): return 2 * (self.__length + self.__width) def calculate_area(self): return self.__length * self.__width # Testing the Rectangle class length = float(input("Enter the length of the rectangle: ")) width = float(input("Enter the width of the rectangle: ")) rectangle = Rectangle(length, width) print("Perimeter of the rectangle:", rectangle.calculate_perimeter()) print("Area of the rectangle:", rectangle.calculate_area()) ``` In the above implementation, we define the class "Rectangle" with the constructor `__init__` that takes the length and width as parameters and sets them using the `self.set_length` and `self.set_width` methods respectively. These methods are responsible for verifying the validity of the length and width values. The `set_length` and `set_width` methods check if the values passed are floating-point numbers between 0.0 and 20.0. If the values are valid, they are assigned to the corresponding private variables `self.__length` and `self.__width`. If the values are invalid, a `ValueError` is raised with an appropriate error message. The `get_length` and `get_width` methods are simple getter methods that return the values of the private variables `self.__length` and `self.__width` respectively. The `calculate_perimeter` method calculates the perimeter of the rectangle using the formula `2 * (length + width)`. The `calculate_area` method calculates the area of the rectangle using the formula `length * width`. Finally, we test the `Rectangle` class by creating an instance of it and inputting the values for length and width from the user. We then call the `calculate_perimeter` and `calculate_area` methods to display the results.

Do you need us to help you on this or any other assignment?


Make an Order Now