IDNLearn.com: Your trusted source for finding accurate answers. Our experts provide timely and accurate responses to help you navigate any topic or issue with confidence.
Sagot :
Answer:
Explanation:
The following code is written in Python. It creates the parent class Shape and the three subclasses Rectangle, Square, and Circle that extend Shape. Shape has the constructor which is empty and the area method which returns 0.0, while the three subclasses take in the necessary measurements for its constructor. Each subclass also has getter and setter methods for each variable and an overriden area() method which returns the shapes area.
class Shape:
def __init__(self):
pass
def area(self):
return 0.0
class Square(Shape):
_length = 0
_width = 0
def __init__(self, length, width):
self._width = width
self._length = length
def area(self):
area = self._length * self._width
return area
def get_length(self):
return self._length
def get_width(self):
return self._width
def set_length(self, length):
self._length = length
def set_width(self, width):
self._width = width
class Rectangle(Shape):
_length = 0
_width = 0
def __init__(self, length, width):
self._width = width
self._length = length
def area(self):
area = self._length * self._width
return area
def get_length(self):
return self._length
def get_width(self):
return self._width
def set_length(self, length):
self._length = length
def set_width(self, width):
self._width = width
class Circle(Shape):
_radius = 0
def __init__(self, radius):
self._radius = radius
def area(self):
area = 2 * 3.14 * self._radius
return area
def get_radius(self):
return self._radius
def set_radius(self, radius):
self._radius = radius
Thank you for being part of this discussion. Keep exploring, asking questions, and sharing your insights with the community. Together, we can find the best solutions. Thank you for visiting IDNLearn.com. We’re here to provide accurate and reliable answers, so visit us again soon.