Knowee
Questions
Features
Study Tools

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

Question

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python implementation of the problem:

from abc import ABC, abstractmethod
import math

class Shape(ABC):
    @abstractmethod
    def calculateArea(self):
        pass

    @abstractmethod
    def calculateCircumference(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def calculateArea(self):
        return math.pi * (self.radius ** 2)

    def calculateCircumference(self):
        return 2 * math.pi * self.radius

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def calculateArea(self):
        return self.width * self.height

    def calculateCircumference(self):
        return 2 * (self.width + self.height)

def main():
    shapes = []
    n = int(input())
    for _ in range(n):
        shape_data = input().split()
        shape_type = shape_data[0]
        if shape_type == 'C':
            radius = float(shape_data[1])
            shapes.append(Circle(radius))
        elif shape_type == 'R':
            width = float(shape_data[1])
            height = float(shape_data[2])
            shapes.append(Rectangle(width, height))
    for shape in shapes:
        print(shape.calculateArea(), shape.calculateCircumference())

if __name__ == "__main__":
    main()

This program first defines an abstract class Shape with two abstract methods calculateArea and calculateCircumference. Then it defines two classes Circle and Rectangle that inherit from Shape and implement the abstract methods. The main function reads the inputs, creates the appropriate shape objects, and prints their areas and circumferences.

This problem has been solved

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)

Write a Java program using " static" that utilizes your ShapeUtility class to demonstrate the calculation of area and perimeter for a circle and a rectangle. Display the total number of shapes processed at the end.The ShapeUtility class to calculate the area and perimeter of a circle and a rectangle. The totalShapes variable (static)  in the ShapeUtility class should be used to keep track of the total number of shapes processed, then display the results and the total number of shapes processed at the end ( as per the Output Format).Input Format:The first line contains the radius of a circle (a double variable)The second line contains the length of a rectangle (a double variable)The third line contains the breadth of a rectangle (a double variable)  Output Format:First line contains the area of a circle (a float : restrict to two precisions)Next line contains the perimeter of a circle (a float : restrict to two precisions)Next line contains the area of a rectangle (a float : restrict to two precisions)Next line contains the perimeter of a rectangle (a float : restrict to two precisions)Last line contains the total number of shapesprocesed.  Testcase-1:Input:1.12.23.3Output:3.806.917.2611.004 Testcase-2:Input:1.23.54.2Output:4.527.5414.7015.404

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 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

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.

1/3

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.