Question

In: Computer Science

#2 Extend Employee and Manager classes created in lab#3. § Replace field year by the field...

#2 Extend Employee and Manager classes created in lab#3. § Replace field year by the field hireDate of type java.util.Date § Your classes should implement Comparable interface. (Employee1 > Employee2 if its salary is more than the salary of Employee2, the same for managers, but if their salaries are equal, compare by bonus). (java oop)-> laboratory work

lab3# Create a class called Employee whose objects are records for an employee. This class will be a derived class of the class Person (MUST contain equals and toString methods). An employee record has an employee's name (inherited from the class Person), an annual salary represented as a single value of type double, a year the employee started work as a single value of type int and a national insuranceNumber, which is a value of type String. Inside this class you need to override toString and equals methods of the Person class. Your class should have a reasonable number of constructors and accessor methods. Then create a class Manager extending Employee, each manager has a team of Employees (Vector) and can get a bonus. You need to override toString and equals methods. Write another class containing a main method to fully test your class definition. *Use super() keyword whenever possible. Otherwise you will lose points. *Check the quality of your equals and hashcode methods by adding several employees to a HashSet and checking whether it allows duplicate items.

Solutions

Expert Solution

Person Class

import java.util.Objects;

public class Person {
    protected String name;  // protected so that Employee child class can inherit it.

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }


    // Equality is based on Name. 2 person with same name is considered same.
    // Question does not mention basis of equality, so this is the default implementation.
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

     // Accessor Methods

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Employee Class

import java.util.Objects;

public class Employee extends Person {  // derived class of the class Person. Employee class has inherited name variable from Person class. See Person class.

    double salary; // an annual salary represented as a single value of type double,
    int yearOfStart; // a year the employee started work as a single value of type int
    String nationalInsuranceNumber; //national insuranceNumber, which is a value of type String.


    public Employee() {
    }



    public Employee(double salary) {
        this.salary = salary;
    }

    public Employee(double salary, int yearOfStart) {
        this.salary = salary;
        this.yearOfStart = yearOfStart;
    }

    public Employee(String name,double salary, int yearOfStart, String nationalInsuranceNumber) {
        super(name);
        this.salary = salary;
        this.yearOfStart = yearOfStart;
        this.nationalInsuranceNumber = nationalInsuranceNumber;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "salary=" + salary +
                ", yearOfStart=" + yearOfStart +
                ", nationalInsuranceNumber='" + nationalInsuranceNumber + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        Employee employee = (Employee) o;
        return Double.compare(employee.salary, salary) == 0 &&
                yearOfStart == employee.yearOfStart &&
                Objects.equals(nationalInsuranceNumber, employee.nationalInsuranceNumber);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), salary, yearOfStart, nationalInsuranceNumber);
    }


    // Accessor Methods

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getYearOfStart() {
        return yearOfStart;
    }

    public void setYearOfStart(int yearOfStart) {
        this.yearOfStart = yearOfStart;
    }

    public String getNationalInsuranceNumber() {
        return nationalInsuranceNumber;
    }

    public void setNationalInsuranceNumber(String nationalInsuranceNumber) {
        this.nationalInsuranceNumber = nationalInsuranceNumber;
    }
}


Related Solutions

0. Introduction. In this lab assignment, you will extend some simple Java classes that represent plane...
0. Introduction. In this lab assignment, you will extend some simple Java classes that represent plane figures from elementary geometry. The object of this assignment is not to do anything useful, but rather to demonstrate how methods can be inherited by extending classes. 1. Theory. A polygon is a closed plane figure with three or more sides, all of which are line segments. The perimeter of a polygon is the sum of the lengths of its sides. A rectangle is...
Give 3 examples each for Field Tests, Field/Lab tests, and Lab tests and explain when and...
Give 3 examples each for Field Tests, Field/Lab tests, and Lab tests and explain when and why you would use them
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience using classes and arrays of classes. Use the following as the beginning of a student abstract data type. public class Student { private String fName ; private String lName ; private double[] grades; } This class should be in the package com.csc241. Write a program that prompts a user for a total number of students and dynamically allocates memory for just that number of...
Match the appropriate field names for an Ethernet frame Field 1 Field 2 Field 3 Field...
Match the appropriate field names for an Ethernet frame Field 1 Field 2 Field 3 Field 4 Field 5 Field 6 Group of answer choices Field 1       [ Choose ]            Source Port            Preamble            Ethernet Type            File Check Sequence            Destination Address            Source Address            Data Length            Destination Port            Data and Padding      ...
CIS247C WEEK 2 LAB The following problem should be created as a project in Visual Studio,...
CIS247C WEEK 2 LAB The following problem should be created as a project in Visual Studio, compiled and debugged. Copy your code into a Word document named with your last name included such as: CIS247_Lab2_Smith. Also include screen prints of your test runs. Be sure to run enough tests to show the full operation of your code. Submit this Word document and the project file (zipped). The Zoo Class This class describes a zoo including some general visitor information. We...
5. A Company is comparing three alternative investments in Field 1, Field 2 and Field 3....
5. A Company is comparing three alternative investments in Field 1, Field 2 and Field 3. This company opts to undertake the analysis using a discount rate of 10%. The estimated cash flows for each investment are as follow: Years Field 1 Cash Flow $ Field 2 Cash Flow $ Field 3 Cash Flow $ 0 -75,000 -175,000 -475,000 1 30,000 25,000 150,000 2 20,000 25,000 150,000 3 20,000 25,000 150,000 4 15,000 25,000 75,000 5 10,000 25,000 75,000 6...
WRITE MODULES TO SORT BY 2 FIELD THEN 3 FIELD, THEN 4 FIELD use data structure...
WRITE MODULES TO SORT BY 2 FIELD THEN 3 FIELD, THEN 4 FIELD use data structure to guide you. please i need help. im so glad if anyone help me. thank you a lot and be safe // Use a custom comparator. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; class Emprec { String name; String address; double hours; double rate; int dependents; char gender; boolean degree; // This is the classes's constructor !!!! Emprec(String name, String address, String hours,String...
You are the field manager on the audit of Hotshot Ltd for the year ended 30...
You are the field manager on the audit of Hotshot Ltd for the year ended 30 September 2015. You have asked Michelle Psi, a new audit analyst to assist you and you are busy ensuring that she has a proper understanding of the work to be done before she starts. Consider the following procedures, which are included in the audit programmes: Vouch a sample of sales transactions recorded in the sales journal to ensure that they have been appropriately authorised...
Assignment 2 - Employee Hierarchy In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model...
Assignment 2 - Employee Hierarchy In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Employees and that each has a different CompensationModel object. A CompensationModel would provide an earnings method. Classes or subclasses of CompensationModel would contain the details of a particular Employee's compensation: CommissionCompensationModel - For Employees who...
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes:...
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes: "Bus", "Car", and "Motorcycle". They should contain a variable storing number of seats (bus) / weight (car) / number of tires (motorcycle), constructor, destructor and a print_info function that will print all the information about the vehicle. PK 3. The constructors should take as parameters: company, year and number of seats / weight / number of tires. Inside it assign the company name and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT