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:

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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:

 

Test
Human h = new Employee ("John", 30, "UCD", 30000);
System.out.println(h);
Human h = new Employee("John", 30, "UCD", 30000);
Human i = new Person ("John", 30);
System.out.println(h.equals(i));
System.out.println(i.equals(h));
Human h new Employee("John", 31, "UCD", 30000);
Human i = new Person ("John", 30);
System.out.println(h.equals(i));
Human h new Employee ("John", 30, "UCD", 30000);
Human i = new Employee("John", 31, "UCD", 30000);
Human j = new Employee ("John", 31, "UCD", 30001);
Human k = new Employee("John", 31, "TCD", 30000);
System.out.println(i.equals(h));
System.out.println(h.equals(h));
System.out.println(j.equals(h));
System.out.println(k.equals(h));
ArrayList<Human> p = new ArrayList<Human>();
p.add(new Person ("A", 1));
p.add(new Employee ("B", 31, "E1", 45000));
p.add(new Person ("C", 122));
p.add(new Employee ("D", 3, "E2", 54321));
p.add(new Person ("E", 21));
p.add(new Employee ("F", 31, "E1", 21000));
p.add(new Employee("G", 31, "E1", 38000));
System.out.println(p);
Collections.sort (p);
System.out.println(p);
Human h = new Employee ("John", 30, "TCD", 30000);
Human i = new Employee("Jane", 31, "UCD", 40000);
Human j = new Employee ("James", 30, "TCD", 32000);
Human k = new Employee("Joanna", 31, "UCD", 35000);
Human = new Person ("Julie", 29);
System.out.println(i.compareTo (h));
System.out.println(h.compareTo(h));
System.out.println(j.compareTo(h));
System.out.println(k.compareTo (h));
System.out.println(h.compareTo(1));
System.out.println(l.compareTo (h));
Employee h = new Employee ("Jamie", 22, "EY", 80000);
Employee i = new Employee ("Joyce", 23, "KPMG", 90000);
System.out.println(h.getSalary() + + h.getEmployer());
System.out.println(i.getSalary() + " " + i.getEmployer());
Result
Employee: [John, 30] [UCD, 30000]
true
true
false
false
true
false
false
[Person: [A, 1], Employee: [B, 31] [E1, 45000], Person: [C, 122], Employee: [D, 3] [E2, 54321], Person: [E, 211, Employee
[Person: [A, 1], Employee: [D, 3] [E2, 54321], Person: [E, 21], Employee: [F, 31] [E1, 21000], Employee: [G, 31] [E1, 3800
HOTLI
0
1
1
1
-1
80000 EY
90000 KPMG
Transcribed Image Text:Test Human h = new Employee ("John", 30, "UCD", 30000); System.out.println(h); Human h = new Employee("John", 30, "UCD", 30000); Human i = new Person ("John", 30); System.out.println(h.equals(i)); System.out.println(i.equals(h)); Human h new Employee("John", 31, "UCD", 30000); Human i = new Person ("John", 30); System.out.println(h.equals(i)); Human h new Employee ("John", 30, "UCD", 30000); Human i = new Employee("John", 31, "UCD", 30000); Human j = new Employee ("John", 31, "UCD", 30001); Human k = new Employee("John", 31, "TCD", 30000); System.out.println(i.equals(h)); System.out.println(h.equals(h)); System.out.println(j.equals(h)); System.out.println(k.equals(h)); ArrayList<Human> p = new ArrayList<Human>(); p.add(new Person ("A", 1)); p.add(new Employee ("B", 31, "E1", 45000)); p.add(new Person ("C", 122)); p.add(new Employee ("D", 3, "E2", 54321)); p.add(new Person ("E", 21)); p.add(new Employee ("F", 31, "E1", 21000)); p.add(new Employee("G", 31, "E1", 38000)); System.out.println(p); Collections.sort (p); System.out.println(p); Human h = new Employee ("John", 30, "TCD", 30000); Human i = new Employee("Jane", 31, "UCD", 40000); Human j = new Employee ("James", 30, "TCD", 32000); Human k = new Employee("Joanna", 31, "UCD", 35000); Human = new Person ("Julie", 29); System.out.println(i.compareTo (h)); System.out.println(h.compareTo(h)); System.out.println(j.compareTo(h)); System.out.println(k.compareTo (h)); System.out.println(h.compareTo(1)); System.out.println(l.compareTo (h)); Employee h = new Employee ("Jamie", 22, "EY", 80000); Employee i = new Employee ("Joyce", 23, "KPMG", 90000); System.out.println(h.getSalary() + + h.getEmployer()); System.out.println(i.getSalary() + " " + i.getEmployer()); Result Employee: [John, 30] [UCD, 30000] true true false false true false false [Person: [A, 1], Employee: [B, 31] [E1, 45000], Person: [C, 122], Employee: [D, 3] [E2, 54321], Person: [E, 211, Employee [Person: [A, 1], Employee: [D, 3] [E2, 54321], Person: [E, 21], Employee: [F, 31] [E1, 21000], Employee: [G, 31] [E1, 3800 HOTLI 0 1 1 1 -1 80000 EY 90000 KPMG
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY