Question

In: Computer Science

Design and develop a class named Person in Python that contains two data attributes that stores...

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.

Solutions

Expert Solution

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


Related Solutions

In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
Design a class named Account that contains: ■ A private int data field named id for...
Design a class named Account that contains: ■ A private int data field named id for the account (default 0). ■ A private double data field named balance for the account (default 0). ■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. ■ A private Date data field named dateCreated that stores the date when the account was created. ■ A no-arg constructor that creates...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
python3 Design a class named Histogram to display a histogram of data. The Histogram class contains...
python3 Design a class named Histogram to display a histogram of data. The Histogram class contains the following A number of instance variables/fields to store a histogram of data. You can design them by your own. A constructor that creates a histogram with the following a list of data. It may have more than 2 columns. x_width: the width of the 1st column in the horizontal histogram. The default value is 5. y_width: the width of the 2nd column in...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
In c++ Design a class named Account that contains: a.An int data field named id for...
In c++ Design a class named Account that contains: a.An int data field named id for the account. b.A double data field named balancefor the account. c.A double data field named annualInterestRate that stores the current interestrate. d.A no-arg constructor that creates a default account with id 0, balance 0, andannualInterestRate 0. e.The set and get functions for id,balance, and annualInterestRate. f.A functionearnedAmount()that calculates and returns the amount of dollars earned after one year. g.A function named printAccountInfo() that print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT