Python Beginner0912
Python Beginner0912

Reputation: 13

How to find the area and volume of Sphere and cylinder in same program in python

Task is to find the area and volume of the shapes. Consider the following shapes while designing the code: Sphere and Cylinder. The class should have only one constructor. Also we should create a class called as Shape Below is code i wrote and returning outputs, but how to use new class for shapes and inherit into test or the other way.

import math
#class Shapes
class Test:
    def __init__(self, *args):
        self.radius = args[0]
        if (len(args) == 2):
            self.height = args[1]
        self.area = 0
        self.volume = 0
        return super().__init__()

    def Area(self,val):
        radius = self.radius
        if (val == "Sphere"):
            area = 4 * 3.14  * radius ** 2
        elif (val =="Cylinder"):
            height = self.height
            area =   ((2 * 3.14  * radius) * (radius + height))
        else:
            area = 0
        return area


    def Volume(self,val):
        radius = self.radius
        if (val == "Sphere"):
            volume = (4/3) * (3.14  * radius ** 2)
        elif (val == "Cylinder"):
            height = self.height
            volume =   3.14  * radius * radius * height
        else:
            volume = 0
        return volume


def main():
    cylinder=Test(2,4) 
    print('Cylinder area:',cylinder.Area(enter code here'Cylinder'))
    print('Cylinder volume:',cylinder.Volume('Cylinder'))
    sphere=Test(3) 
    print('Sphere area:',sphere.Area('Sphere'))
    print('Sphere volume:',sphere.Volume('Sphere'))


if __name__=='__main__':
    main()

Upvotes: 1

Views: 1674

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36732

as specified in the question:

  • Create a class Shape.
  • Derive a class Sphere and a class Cylinder from this class Shape.
  • Implement the calculation of volume and area for each shape.

Then, in order to use the shapes,

  • You need to first create a shape.
  • Then call the methods get_surface, or get_volume, on the shapes created. You do this with "dot" notation

The example here under does that.

import math

class Shape:
    """abstract class for shapes"""
    def __init__(self, radius):
        self.radius = radius

    def get_surface(self):
        raise NotImplementedError

    def get_volume(self):
        raise NotImplementedError


class Sphere(Shape):    # <-- inherits from Shape
    def __init__(self, radius):
        Shape.__init__(self, radius)    # <-- can also use super().__init__(radius)

    def get_surface(self):
        return 4 * math.pi * self.radius**2   # <-- use math.pi constant i/o hard coding the value of pi

    def get_volume(self):
        return math.pi * self.radius**3 * 4/3  


class Cylinder(Shape):    # <-- inherits from Shape
    def __init__(self, radius, height):
        Shape.__init__(self, radius)    # <-- can also use super().__init__(radius)
        self.height = height

    def get_surface(self):
        return 2 * math.pi * self.radius * self.height + 2 * math.pi * self.radius**2

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


def main():
    cyl = Cylinder(2, 4) 
    print('Cylinder area:', cyl.get_surface())   # <-- call the methods on the object
    print('Cylinder volume:', cyl.get_volume())
    sph = Sphere(5) 
    print('Sphere area:',sph.get_surface())
    print('Sphere volume:',sph.get_volume())


if __name__=='__main__':
    main()

output:

Cylinder area: 75.3982236862
Cylinder volume: 50.2654824574
Sphere area: 314.159265359
Sphere volume: 523.598775598

Upvotes: 2

Related Questions