QuesAns Print Page – CSKC
Sign In

Search

siteicon
Q&A CBSE12A-2018-B02C #6120

Question:

Define a class CONTAINER in Python with following specifications

Instance Attributes
- Radius,Height   # Radius and Height of Container
- Type            # Type of Container
- Volume          # Volume of Container

Methods
- CalVolume()     # To calculate volume
                  # as per the Type of container
                  # With the formula as given below:
Type Formula to calculate Volume
1 3.14 * Radius * Height
3 3.14 * Radius * Height/3
- GetValue()       # To allow user to enter values of
                   # Radius, Height and Type.
                   # Also, this method should call
                   # CalVolume() to calculate Volume
- ShowContainer()  # To display Radius, Height, Type
                   # Volume of the Container

Answer:

class CONTAINER: # class CONTAINER():/class CONTAINER(Object):
    def __init__(self):       # def __init__(self,R,H,T,V):
        self.Radius=0         # self.Radius=R
        self.Height=0         # self.Height=H
        self.Type =0          # self.Type=T
        self.Volume=0         # self.Volume=V
    def CalVolume(self):
        if self.Type == 1:
            self.Volume = 3.14 * self.Radius * self.Height
        elif self.Type ==3:
            self.Volume = 3.14 * self.Radius * self.Height /3
    def GetValue(self):
        self.Radius = input("Enter Radius")
        self.Height = input("Enter Height")
        self.Type = input("Enter type")
        self.CalVolume()           # OR CalVolume(self)
    def ShowContainer(self):
        print self.Radius
        print self.Height
        print self.Type
        print self.Volume