Create an abstract class SHAPE. Define member functions get_data() and display_area(). Derive five sub classes namely square, triangle, rectangle, circle and ellipse. Compute the area of rectangle, square, triangle, circle and ellipse. Display the result using display_area().Override the display_area() in all the sub classes. Use suitable mechanism to avoid the duplication and link the function display_area() at run time.
Question
Create an abstract class SHAPE. Define member functions get_data() and display_area(). Derive five sub classes namely square, triangle, rectangle, circle and ellipse. Compute the area of rectangle, square, triangle, circle and ellipse. Display the result using display_area().Override the display_area() in all the sub classes. Use suitable mechanism to avoid the duplication and link the function display_area() at run time.
Solution
This seems like a task for creating classes in Python. Here's how you can do it:
from abc import ABC, abstractmethod
import math
class Shape(ABC):
@abstractmethod
def get_data(self):
pass
@abstractmethod
def display_area(self):
pass
class Square(Shape):
def get_data(self):
self.side = float(input("Enter side of square: "))
def display_area(self):
return self.side * self.side
class Rectangle(Shape):
def get_data(self):
self.length = float(input("Enter length of rectangle: "))
self.breadth = float(input("Enter breadth of rectangle: "))
def display_area(self):
return self.length * self.breadth
class Triangle(Shape):
def get_data(self):
self.base = float(input("Enter base of triangle: "))
self.height = float(input("Enter height of triangle: "))
def display_area(self):
return 0.5 * self.base * self.height
class Circle(Shape):
def get_data(self):
self.radius = float(input("Enter radius of circle: "))
def display_area(self):
return math.pi * self.radius * self.radius
class Ellipse(Shape):
def get_data(self):
self.major_axis = float(input("Enter major axis of ellipse: "))
self.minor_axis = float(input("Enter minor axis of ellipse: "))
def display_area(self):
return math.pi * self.major_axis * self.minor_axis
# Create objects for each class and call their methods
s = Square()
s.get_data()
print("Area of square: ", s.display_area())
r = Rectangle()
r.get_data()
print("Area of rectangle: ", r.display_area())
t = Triangle()
t.get_data()
print("Area of triangle: ", t.display_area())
c = Circle()
c.get_data()
print("Area of circle: ", c.display_area())
e = Ellipse()
e.get_data()
print("Area of ellipse: ", e.display_area())
This code first creates an abstract class Shape with two abstract methods get_data and display_area. Then it creates five subclasses Square, Rectangle, Triangle, Circle, and Ellipse each of which implements the get_data method to get the necessary parameters for the shape and display_area method to calculate and return the area of the shape. Finally, it creates an object for each class, calls their get_data method to get the necessary parameters, and then calls their display_area method to calculate and print the area.
Similar Questions
Create a class named ‘Shape’ which has methods ‘getArea’, ‘getPerimeter’and ‘getSide’.Make three subclasses for three different shapes - ‘Circle’, ‘Triangle’ and‘Rectangle’. These subclasses inherit the ‘Shape’ class and they have theirown ‘getArea’, ‘getPerimeter’, and ‘getSide’ methods definition.Each of these classes has its own data members likeThe circle will have a Radius,Triangle will have three sides,A rectangle will have width and heightWrite a program for the above scenario and display the solution.
Create an abstract class named shape that contains two integers and an empty method named printarea().Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape. Each of the inherited class from shape class should provide the implementation for the method printarea(). Get the input and calculate the area of rectangle, circle and triangle.In the Main class, create the objects for the three inherited classes and invoke the methods and display the area values of the different shapes.2(length of rectangle)3(breadth)5(triangle breadth)6(Triangle height)4(circle radius)
Implement the abstract class Shape with the abstract method calculateArea.Create the derived classes Circle and Rectangle that extend Shape and implement the calculateArea method according to the formulas for each shapeWrite a main program that reads inputs for creating a circle and a rectangle, calculates their areas, and prints the results.Input2 //number of objectsC 10 //Circle with radousR 10 10 //Rectangle with width and height....Output2 lines display the respective area and circumference314.0 62.8100 40Input3C 100R 5 5R 10 10oUTPUT31400.0 62825 20100 40uSE iNHERITANCE WITH ABSTRACT CLASS SHAPE// Find area and Find circumference must be abstract methods Circle and Rectangle should be sub classes
Create a module named area having four functions named triangle, square, rectangle,and circle. All these functions takes arguments according to their shape; compute andreturns the area of the shape. Import the module in the main program and call therespective function depending on the user requirement and compute the area ofrespective shape
Define a base class called Shape with two pure virtual functionscalculateArea() and calculatePerimeter(). Then, create two derived classes Rectangleand Circle. Implement these classes such that they inherit from the Shape class andoverride the pure virtual functions to calculate the area and perimeter of rectangles andcircles respectively.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.