69.4k views
5 votes
Exercise 2. Employee, Manager, and Executive:

For the first time, we'll be asking you to design your own classes from scratch.
Make a class Employee with a name and salary (with appropriate types and access modifiers). Give it a constructor with these values. Name doesn't need to be able to change, but a salary should be changeable in the case of a raise. Give it an overridden toString method that prints the employee details in a clean way.
Make a subclass Manager that inherits from Employee. It will have the additional instance variable called department, which is a String. It should also have an appropriate restricted access modifier but with getters and setters such that it can be changed and read. Manager should have the same constructor as Employee. This class's toString should also include this new department in addition to everything from Employee's details. Remember you can do this using super.toString() and then adding to that.
Lastly, make a subclass of Manager called Executive. We can interpret the department variable from its superclass as the executive suite position, e.g. "Executive" for a CEO or "Financial" for CFO. Give this class a new instance variable numberOfShares, which keeps track of how many shares (as an integer) they own in the company's stock. Executives can buy or sell more shares, so getters and setters for this variable are needed.

User ZenoArrow
by
8.3k points

1 Answer

7 votes

Final answer:

To design the classes as described, you will start by creating a class called Employee. This class will have instance variables for name and salary with appropriate types and access modifiers. The class will also have a constructor that takes the values for name and salary. The name variable doesn't need to be changeable, but the salary should have a setter method to allow for changes.

Step-by-step explanation:

To design the classes as described, you will start by creating a class called Employee. This class will have instance variables for name and salary with appropriate types and access modifiers. The class will also have a constructor that takes the values for name and salary. The name variable doesn't need to be changeable, but the salary should have a setter method to allow for changes. Finally, override the toString method to print the employee details.

To create the Manager subclass, inherit from the Employee class. Add an additional instance variable called department with the appropriate access modifier. Use getters and setters to allow for changes to the department variable. The Manager class should have the same constructor as the Employee class. Override the toString method to include the department in addition to the employee details.

To create the Executive subclass, inherit from the Manager class. Add a new instance variable called numberOfShares to track the amount of shares the executive owns in the company's stock. Again, use getters and setters to allow for changes to this variable.

User Abuder
by
9.2k points

No related questions found