In: Computer Science
Enlistee and Private Classes
Write an Enlistee class that keeps data attributes for the following pieces of information:
Enlistee name
Enlistee number
Next, write a class named Private that is a subclass of the Enlistee class. The Private class should keep data attributes for the following information:
Platoon number (an integer, such as 1, 2, or 3)
Years of service (also an integer)
Write the appropriate accessor and mutator methods for each class.
Once you have written the classes, write a program that creates an object of the Private class and prompts the user to enter data for each of the object’s data attributes. Store the data in the object and then use the object’s accessor methods to retrieve it and display it on the screen.
Using python please.
Here is the solution if you have any doubt then please write in the comment section.
Please give feedback.
Solution: I have written comments for each line for better understanding.
# class Enlistee
class Enlistee:
# init method or constructor
def __init__(self, name,number):
self.name = name
self.number = number
# Retrieves instance variable
def getName(self):
return self.name
# Retrieves instance variable
def getNumber(self):
return self.number
# Mutator method
def setName(self,name):
self.name = name
# Mutator method
def setNumber(self,number):
self.number = number
# Private class inherits Enlistee
class Private(Enlistee):
# init method or constructor
def __init__(self, name, number,platoon_number,service_year):
self.platoon_number = platoon_number
self.service_year = service_year
# invoking the __init__ of the parent class
Enlistee.__init__(self, name, number)
# Retrieves instance variable
def getPlatoon_number(self):
return self.platoon_number
# Retrieves instance variable
def getService_year(self):
return self.service_year
# Mutator method
def setPlatoon_number(self,platoon_number):
self.platoon_number = platoon_number
# Mutator method
def setService_year(self,service_year):
self.service_year = service_year
# Taking iinput from user
name = input("Enter name of Enlistee: ")
number = int(input("Enter number of Enlistee: "))
platoon_number = int(input("Enter platoon number of Enlistee: "))
service_year = int(input("Enter service year of Enlistee: "))
# Creating object with the help of given values
obj = Private(name,number,platoon_number,service_year)
# Printing value using accessor methods
print("\n\nName of Enlistee: "+obj.getName())
print("Number of Enlistee: "+str(obj.getNumber()))
print("Platoon Number of Enlistee: "+str(obj.getPlatoon_number()))
print("Service Year of Enlistee: "+str(obj.getService_year()))
Output:
If you have any type of doubts then please write in the comment section, I will feel happy to help you.
Please give feedback.
Thank You!