Question

In: Computer Science

Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces...

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.

Solutions

Expert Solution

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!


Related Solutions

C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an...
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an integer à The age of the vehicle       Price, a float à The price of the vehicle       Behaviors: Vehicle() à default constructor sets age=0, and price=0.0 setAge()   à Takes an integer parameter, returns nothing setPrice() à Takes a float parameter, returns nothing getAge()   à Takes no parameters, returns the vehicle’s age getPrice() à Takes no parameters, returns the vehicle’s price End Class Vehicle...
Using Eclipse, create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours...
Using Eclipse, create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type double. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type double. For each class, provide its getter and setter functions, a default constructor, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for the Services class...
Task1: Write a class DynArr that represents an array data structure. Here are the attributes (data)...
Task1: Write a class DynArr that represents an array data structure. Here are the attributes (data) of the class (note - fields of the objects should be marked as private)” INITIAL_SIZE: the initial size of the array = 10; _innerArr: an array of integers _growthFactor: the factor by which to resize the array with to become bigger. _lastIndex: an index on the last element in the array. The class will have the constructor public DynArr() that initialzes an array of...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Here is the assignment description. * Create a class named 'Account' having the following private attributes...
Here is the assignment description. * Create a class named 'Account' having the following private attributes int accountNumber; double balance; * Write a constructor with parameters for each of the attributes. * Write another constructorwith one parameter for the accountNumber. * Write getter and setter methods for each of the private attributes. * Write a method void credit(double amount) which adds the given amount to the balance. * Write a method void debit(double amount) which subtracts the given amount from...
Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
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
Write a class encapsulating the concept of a student, assuming a student has the following attributes:...
Write a class encapsulating the concept of a student, assuming a student has the following attributes: a name, a Social Security number, and a GPA (for instance, 3.5). Include a constructor, the accessors and mutators, and methods toString and equals. Write a client class (test driver with main method) to test all the methods in your class (create 2 student objects, print the name, social security number and GPA of the 2 students, check if the two student’s GPA is...
Given the following list of classes, attributes and methods, - identify which items are classes, which...
Given the following list of classes, attributes and methods, - identify which items are classes, which items are attributes and which items are methods; - identify which class each attribute and method belongs to; and - suggest a class hierarchy given your list of classes. *Note - no particular capitalization scheme is used in the list below to differentiate between classes, methods, and attributes. LandOnStatue, NumberOfLegs, Height, ShoeSize, Eat, Animal, Speak, WingSpan, Age, Peck, Sleep, Horse, LengthOfMane, Move, BeakLength, LengthOfTail,...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT