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

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 ()   ...

Getting Started with NumPy: A Comprehensive Guide

  NumPy is a fundamental library for scientific computing with Python. It provides support for large, multi-dimensional arrays and matrices, along with mathematical functions to operate on these arrays. This tutorial will introduce you to the basics of NumPy and its core functionalities. Installing NumPy Before diving into NumPy, make sure you have it installed. You can install NumPy using the following command: pip install numpy pip install numpy Importing NumPy Once installed, you can import NumPy in your Python script or Jupyter notebook as follows: import numpy as np Now, let's explore some of the essential features of NumPy. NumPy Arrays NumPy's primary data structure is the array. An array is a grid of values, and it can be one-dimensional or multi-dimensional. Here's how you can create a simple one-dimensional array: import numpy as np # Creating a one-dimensional array arr1 = np.array([1, 2, 3, 4, 5]) print(arr1) For multi-dimensional arrays, you can use nested ...