Skip to main content

Classes and objects 2|task1 & 2| inheritance and polymorphism

 #!/bin/python3

task 1 Inheritance: parent and children share


import math
import os
import random
import re
import sys

class parent:
  def __init__(self,total_asset):
    self.total_asset = total_asset

  def display(self):
    print("Total Asset Worth is "+str(self.total_asset)+" Million.")
    print("Share of Parents is "+str(round(self.total_asset/2,2))+" Million.")


class son(parent):
    def __init__(self, total_asset,Percentage_for_son):
        self.Percentage_for_son=Percentage_for_son
        parent.__init__(self,total_asset)
        
    def son_display(self):
        s = round((self.total_asset * self.Percentage_for_son)/100 ,2)
        print("Share of Son is "+str(s)+" Million.")
class daughter(parent):
    def __init__(self, total_asset,Percentage_for_daughter):
        self.Percentage_for_daughter=Percentage_for_daughter
        parent.__init__(self,total_asset)
        
    def daughter_display(self):
        d=round((self.total_asset*self.Percentage_for_daughter)/100,2)
        print("Share of Daughter is "+str(d)+" Million.")
            
            
# It is expected to create two child classes 'son' & 'daughter' for the above class 'parent'
#
#Write your code here
if __name__ == '__main__':
    
    t = int(input())
    sp = int(input())
    dp = int(input())


    obj1 = parent(t)
    

    obj2 = son(t,sp)
    obj2.son_display()

    obj2.display()


    obj3 = daughter(t,dp)
    obj3.daughter_display()
    obj3.display()
    
    print(isinstance(obj2,parent))
    print(isinstance(obj3,parent))

    print(isinstance(obj3,son))
    print(isinstance(obj2,daughter))



task 2 : polymosphism
#!/bin/python3

import math
import os
import random
import re
import sys



class rectangle:
    def display(self):
        print("This is a Rectangle")
    def area(self,Length,Breadth):
        self.Length=Length
        self.Breadth=Breadth
        arear=self.Length*self.Breadth
        print("Area of Rectangle is  "+str(arear))
class square:
    def display(self):
        print("This is a Square")
    def area(self,Side):
        self.Side=Side
        areas=self.Side**2
        print("Area of square is  "+str(areas))
        
        
# Write your code here
if __name__ == '__main__':
    
    l = int(input())
    b = int(input())
    s = int(input())

    obj1 = rectangle()
    obj1.display()
    obj1.area(l,b)

    obj2 = square()
    obj2.display()
    obj2.area(s)

Comments

Popular posts from this blog

Class and object Task 1| cinema tickets | hacker rank|tcs fresco play

  class   Movie :      def   __init__ ( self , nameofmovie , nooftickets , totalcost ):          self . nameofmovie = nameofmovie          self . nooftickets = nooftickets          self . totalcost = totalcost      def   __str__ ( self ):          return   "Movie : " + str ( self . nameofmovie ) + "\n" + "Number of Tickets : " + str ( self . nooftickets ) + "\n" + "Total Cost : " + str ( self . totalcost )                   # Write your code here if   __name__  ==  '__main__' :

Magic constant generator -python3/hacker rank solution / tcs fresco play

  def   generator_Magic ( n1 ):      # the value starts from 3 and m is formula for constant,      #for generator  yield should use      for   a   in   range ( 3 , n1 + 1 ):          m = a * ( a ** 2 + 1 ) / 2          yield   m               # Write your code here if   __name__  ==  '__main__' :

Handling Exceptions 1 | hacker rank solution

  #!/bin/python3 import   math import   os import   random import   re import   sys # # Complete the 'Handle_Exc1' function below. # # def   Handle_Exc1 ():      a = int ( input ())      b = int ( input ())      if   a > 150   or   b < 100 :          raise   ValueError   ( "Input integers value out of range." )        if   a + b > 400 :          raise   ValueError ( "Their sum is out of range" )      print ( "All in range" )      # Write your code here if   __name__  ==  '__main__' :      try :          Handle_Exc1 ()   ...