In: Computer Science
Design and develop a class named Person in Python that contains two data attributes that stores the first name and last name of a person and appropriate accessor and mutator methods. Implement a method named __repr__ that outputs the details of a person.
Then Design and develop a class named Student that is derived from
Person, the __init__ for which should receive first name and last
name from the class Person and also assigns values to student id,
course, and teacher name. This class needs to redefine the __repr__
method to person details as well as details of a student. Include
appropriate __init__.
Design and develop a class named Teacher that is derived from Person. This class should contain data attributes for the course name and salary. Include appropriate __init__. Finally, redefine the __repr__ method to include all teacher information in the printout.
Implement an application/driver that creates at least two student
objects and two teacher objects with different values and calls
__repr__ for each.
Code is as follows:
class Person : #base class
firstName =""
lastName =""
#constructor for base class
def __init__(self,firstName,lastName):
self.firstName = firstName
self.lastName = lastName
#__repr__ method
def __repr__(self):
print("firstname \t lastname")
print (self.firstName + '\t' + self.lastName)
#method to get the first name
def get_firstName(self):
return self.firstName
#method to get the last name
def get_lastName(self):
return self.lastName
class Student(Person): #child class
studentId =0
courseName = ""
teacherName =""
#constructor for child class
def __init__(self,firstName,lastName,studentId,courseName,teacherName):
self.studentId = studentId
self.courseName =courseName
self.teacherName = teacherName
#call base class constructor
super(Student,self).__init__(firstName,lastName)
#override the __repr__ method of base class
def __repr__(self):
print (self.get_firstName() + '\t\t' + self.get_lastName() + '\t\t' + str(self.studentId) + '\t\t' + self.courseName + '\t\t' + self.teacherName)
class Teacher(Person):#child class
courseName = ""
salary = 0.0
#constructor for child class
def __init__(self,firstName,lastName,courseName,salary):
self.courseName =courseName
self.salary = salary
#call base class constructor
super(Teacher,self).__init__(firstName,lastName)
#override the __repr__ method of base class
def __repr__(self):
print (self.get_firstName() + '\t\t' + self.get_lastName() + '\t\t' + self.courseName + '\t\t' + str(self.salary))
#main class, create two instances of Student and Teacher and call __repr__ method for them
student1 = Student("Alice", "cook",1,"CS","Will Jones")
student2 = Student("Bob", "White",2,"IT","Tim Jones")
teacher1 = Teacher("Will","Jones","CS",23400.0)
teacher2 = Teacher("Tim","Jones","IT",35000.0)
print("============== Student Information =====================================")
print("firstname\tlastname\tStudentId\tCourse\tTeacherName")
student1.__repr__()
student2.__repr__()
print("============== END ====================================================")
print("============== Teacher Information =====================================")
print("firstname\tlastname\tCourse Name\tSalary")
teacher1.__repr__()
teacher2.__repr__()
print("============== END ====================================================")
Screen shot of the code for your reference and indentation:
Output:
In case of any query, kindly comment.
Please rate your answer. Thanks