71.7k views
3 votes
Java Programming: Write a class named Employee which is derived from Person and test it in EmployeeTest. The class Employee should have at least:

1. three additional features: EmployeeID (int), salary (as double) and hireYear (as int).
2. two constructors (default and one with 4 parameters).
3. four mutator methods (rest, setEmployeeID, setSalary, and setHireYear).
4. three accessor methods.
5. equals method.
6. toString method
7. EmployeeID and salary must be positive and hireYear must be greater than 1970.

1 Answer

5 votes

Final answer:

To create a class named Employee that inherits from the Person class, you can use the 'extends' keyword in Java. The Employee class should have three additional features: EmployeeID (int), salary (as double), and hireYear (as int). It should also have two constructors, four mutator methods, three accessor methods, an equals method, and a toString method. EmployeeID and salary must be positive, and hireYear must be greater than 1970.

Step-by-step explanation:

Java Programming: Employee Class

To create a class named Employee that inherits from the Person class, you can use the 'extends' keyword in Java. Here's an example of how the Employee class can be implemented:

public class Employee extends Person {
private int employeeID;
private double salary;
private int hireYear;

// Default constructor
public Employee() {
// Call the parent class constructor
super();
// Initialize the additional features
employeeID = 0;
salary = 0.0;
hireYear = 0;
}

// Constructor with parameters
public Employee(String name, int age, int employeeID, double salary, int hireYear) {
// Call the parent class constructor
super(name, age);
// Set the additional features
this.employeeID = employeeID;
this.salary = salary;
this.hireYear = hireYear;
}

// Mutator methods
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}

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

public void setHireYear(int hireYear) {
this.hireYear = hireYear;
}

// Accessor methods
public int getEmployeeID() {
return employeeID;
}

public double getSalary() {
return salary;
}

public int getHireYear() {
return hireYear;
}

// Equals method
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (!(other instanceof Employee)) {
return false;
}

Employee otherEmployee = (Employee) other;
return super.equals(other) && this.employeeID == otherEmployee.employeeID && this.salary == otherEmployee.salary && this.hireYear == otherEmployee.hireYear;
}

// toString method
public String toString() {
return super.toString() + "\\Employee ID: " + employeeID + "\\Salary: " + salary + "\\Hire Year: " + hireYear;
}
}

In the EmployeeTest class, you can create instances of the Employee class and test its functionality. For example:

public class EmployeeTest {
public static void main(String[] args) {
Employee employee1 = new Employee();
employee1.setName("John Doe");
employee1.setAge(30);
employee1.setEmployeeID(12345);
employee1.setSalary(50000.0);
employee1.setHireYear(2005);

Employee employee2 = new Employee("Jane Smith", 25, 67890, 60000.0, 2010);

System.out.println(employee1.toString());
System.out.println(employee2.toString());

System.out.println("Are employee1 and employee2 equal?");
System.out.println(employee1.equals(employee2));
}
}

This code demonstrates the implementation of the Employee class with the desired features, constructors, mutator and accessor methods, equals method, and the toString method.

User AmazingDayToday
by
8.4k points