Define a class named Employee. This class should extend the Person class from the previous question. The class should have a constructor, which takes the name, age, employer (String) and salary (int) as parameters (in that order). The first two parameters should be passed to the constructor of the superclass and the value of the last two parameters should be stored in instance variables. The class should define the methods, getSalary which returns the salary of the employee and getEmployer which returns the name of the employer. Any methods from the previous question that need to be overridden should be overridden! Use super and instanceof as appropriate. I.e., for the following methods you should be able to call the method in the superclass for specific situations (e.g. the compareTo method in the superclass can be called if you receive anything other than an Employee, and this also holds for the equals method). The toString method only needs to append details and as such can also call the method in the superclass. equals(Object o) toString(): Employee:[name, age][employer, salary] compareTo(Human h) -- in cases of a tie break (i.e. if the ages are the same), salary should be used. All instance variables should be private. The model answer to the previous question is preloaded. It uses getClass() in the toString method. For example:
Define a class named Employee. This class should extend the Person class from the previous question. The class should have a constructor, which takes the name, age, employer (String) and salary (int) as parameters (in that order). The first two parameters should be passed to the constructor of the superclass and the value of the last two parameters should be stored in instance variables.
The class should define the methods, getSalary which returns the salary of the employee and getEmployer which returns the name of the employer.
Any methods from the previous question that need to be overridden should be overridden! Use super and instanceof as appropriate. I.e., for the following methods you should be able to call the method in the superclass for specific situations (e.g. the compareTo method in the superclass can be called if you receive anything other than an Employee, and this also holds for the equals method). The toString method only needs to append details and as such can also call the method in the superclass.
- equals(Object o)
- toString(): Employee:[name, age][employer, salary]
- compareTo(Human h) -- in cases of a tie break (i.e. if the ages are the same), salary should be used.
All instance variables should be private. The model answer to the previous question is preloaded. It uses getClass() in the toString method.
For example:
Step by step
Solved in 4 steps with 2 images