Answer to Question #280942 in Python for Annette

Question #280942

Demonstrate use of Object Oriented Programming (OOP) principles, data structures and file handling operations to carry out the following tasks:

a) Define a class named Person, with child classes named Daughter and Son who also have have derived classes named GrandSon and GrandDaughter respectively. All child classes have derived genetic features and skills from Person. Create 5 methods for getting and setting genetic features, setting and getting skills and demonstrate the concept of polymorphism.

b) Modify the answer in a) and show how the person features are captured in a file called person.txt. Also create another method to read from the file person.csv and store the data in a list called persondata. Clearly show how exceptions are handled in your program


1
Expert's answer
2021-12-20T10:03:31-0500

1. Single inheritance

class Parent:
    def f1(self):
        print("Function of parent class.")

class Child(Parent):
    def f2(self):
        print("Function of child class.")

object1 = Child()
object1.f1()
object1.f2()

2. Multiple Inheritance

class Parent_1:
    def f1(self):
        print("Function of parent_1 class.")
    
class Parent_2:
    def f2(self):
        print("Function of parent_2 class.")
    
class Parent_3:
    def f3(self):
        print("function of parent_3 class.")
    
class Child(Parent_1, Parent_2, Parent_3):
    def f4(self):
        print("Function of child class.")
    
object_1 = Child()
object_1.f1()
object_1.f2()
object_1.f3()
object_1.f4()

3. Multi-level Inheritance

class Parent:
    def f1(self):
        print("Function of parent class.")
    
class Child_1(Parent):
    def f2(self):
        print("Function of child_1 class.")
    
class Child_2(Child_1):
    def f3(self):
        print("Function of child_2 class.")
    
obj_1 = Child_1()
obj_2 = Child_2()
    
obj_1.f1()
obj_1.f2()
    
print("\n")
obj_2.f1()
obj_2.f2()
obj_2.f3()


class Car:          #parent class

    def __init__(self, name, mileage):
        self.name = name 
        self.mileage = mileage 

    def description(self):                
        return f"The {self.name} car gives the mileage of {self.mileage}km/l"

class BMW(Car):     #child class
    pass

class Audi(Car):     #child class
    def audi_desc(self):
        return "This is the description method of class Audi."

obj1 = BMW("BMW 7-series",39.53)
print(obj1.description())

obj2 = Audi("Audi A8 L",14)
print(obj2.description())
print(obj2.audi_desc())

(b)

import ​csv
file = open("Salary_Data.csv")
csvreader = csv.reader(file)
header = next(csvreader)
print(header)
rows = []
for ​row in csvreader:
   ​ rows.append(row)
print(rows)
file.close()


import ​csv
rows = []
with open("Salary_Data.csv", 'r) as file:
   ​csvreader = csv.reader(file)
   ​header = next(csvreader)
   ​for row in csvreader:
       ​rows.append(row)
print(header)
print(rows)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS