37.3k views
2 votes
Implement a program that manages shapes. Implement a class named Shape with a method area() which returns the double value 0.0. Implement three derived classes named Rectangle, Square, and Circle. Declare necessary properties in each including getter and setter function and a constructor that sets the values of these properties. Override the area() function in each by calculating the area using the defined properties in that class.

1 Answer

3 votes

Answer:

Step-by-step 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

Implement a program that manages shapes. Implement a class named Shape with a method-example-1
User Saeed Ur Rehman
by
5.5k points