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.
Question
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.
Solution
Here is a Python program that implements the scenario you described:
import math
class Shape:
def getArea(self):
pass
def getPerimeter(self):
pass
def getSide(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def getArea(self):
return math.pi * (self.radius ** 2)
def getPerimeter(self):
return 2 * math.pi * self.radius
def getSide(self):
return None
class Triangle(Shape):
def __init__(self, side1, side2, side3):
self.side1 = side1
self.side2 = side2
self.side3 = side3
def getArea(self):
s = (self.side1 + self.side2 + self.side3) / 2
return math.sqrt(s * (s - self.side1) * (s - self.side2) * (s - self.side3))
def getPerimeter(self):
return self.side1 + self.side2 + self.side3
def getSide(self):
return [self.side1, self.side2, self.side3]
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def getArea(self):
return self.width * self.height
def getPerimeter(self):
return 2 * (self.width + self.height)
def getSide(self):
return [self.width, self.height]
# Test the classes
circle = Circle(5)
print(circle.getArea())
print(circle.getPerimeter())
triangle = Triangle(3, 4, 5)
print(triangle.getArea())
print(triangle.getPerimeter())
print(triangle.getSide())
rectangle = Rectangle(4, 5)
print(rectangle.getArea())
print(rectangle.getPerimeter())
print(rectangle.getSide())
This program first defines a base class Shape with three methods: getArea, getPerimeter, and getSide. Then it defines three subclasses Circle, Triangle, and Rectangle that inherit from Shape and override its methods. Each subclass has its own data members: Circle has radius, Triangle has side1, side2, and side3, and Rectangle has width and height. The program then creates instances of each subclass and calls their methods to demonstrate their functionality.
Similar Questions
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)
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.
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
Consider the base class "Shape" with methods draw() [prints message "Draw"]. Create three subclasses: Circle, Square, and Triangle. Override the draw() method in each subclass to draw the respective shape, and override the calculateArea() method to calculate and return the area of each shape. Implement the above scenario with suitable pseudocode and appropiate java application.User Choice may be "C/S/T". a.) C (Circle), then must read "Radius"b.) S (Square), then must read "Side"c.) T (Triange), then must read both "Height and Base".Identify the suitable data members for the above derived classes and ensure then all are private.
Create a 2 separate Classes CIRCLE with private data members - Radius and Area, and RECTANGLE with private data members - Length, Breadth and Area. In each of these classes declare and define the member methods putData() [ To display Area ] and computeArea()[ To Compute Area ] are the private and getData() as public member method to read Radius or Length and Breadth based on the choice('C' or 'R'). As a programmer, Write a java program and subsequent pseudocode, to find the area of circle/rectangle using class CIRCLE/RECTANGLE which have following details:getData() to read either radius or lenght and breadth based on the choice 'C' or 'R'Calculate the area using computeArea()Display the Area using putData().Note :Radius is always +Ve, otherwise print "Invalid Radius"Length and Breadth are +Ve, otherwise print "Invalid Length(if Length is -VE) or Invalid Breadth(if Breadth is -VE) or "Invalid Length and Breadth"Input : First line Read the type ('C' or 'R')Second line onwards read Radius(if Type = 'C') or Length and Breadth (if Type = 'R')Output : print Area
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.