Write the Java code for an abstract class named Employee which has two data memb
ID: 3835429 • Letter: W
Question
Write the Java code for an abstract class named Employee which has two data members; one for the employee number and one for employee name (use information hiding). The Employee class should include get and set methods for both data members and a constructor that initializes the two data members from the values received in the parameter list. Create (override) the toString() method that returns a string indicating the employee number and the employee name. In addition to these methods, the Employee class will have one abstract method called calculatePay. The calculatePay method will return a floating point amount representing the weekly pay. In addition to the Employee superclass, write the code for two subclasses: HourlyEmpl and SalaryEmpl. The HourlyEmpl weekly pay is calculated by multiplying its two data members together ( hourlyRate and no_of_hrs_worked). The SalaryEmpl pay is calculated by dividing its yearlySalary data member by 52 ( weeks in a year). The constructors of both subclasses should initialize all data members with the values passed in the constructor’s parameter list. Write the code for a class named TestEmployee that will create an array of 3 Employee references, instantiates two HourlyEmpl objects and one SalaryEmpl object, and then assigns the HourlyEmpl objects to the first two array elements in the array mentioned above, and the SalaryEmpl object to the third. Create a loop that calls the calculatePay method and then displays the employee number, employee name and weekly pay for all three employees using the array references.
Explanation / Answer
Please find my implementation:
########
public abstract class Employee {
private int employeeID;
private String name;
public Employee(int id, String n){
employeeID = id;
name = n;
}
public int getID(){
return employeeID;
}
public String getName(){
return name;
}
public abstract float calculatePay();
@Override
public String toString() {
return "Employee Id: "+employeeID+" Name: "+name;
}
}
// HourlyEmpl Class
class HourlyEmpl extends Employee{
private float hourlyRate;
private int no_of_hrs_worked;
public HourlyEmpl(int id, String n, int hrs, float hourlyRate) {
super(id, n);
this.hourlyRate = hourlyRate;
no_of_hrs_worked = hrs;
}
public double getHourlyRate(){
return hourlyRate;
}
@Override
public float calculatePay() {
return no_of_hrs_worked*hourlyRate;
}
@Override
public String toString() {
return super.toString()+" Hourly Rate: "+hourlyRate+" "+
"Number of Hours Worked: "+no_of_hrs_worked;
}
}
//SalaryEmpl Class
class SalaryEmpl extends Employee{
private float yearlySalary;
public SalaryEmpl(int id, String n, float yearlySal) {
super(id, n);
this.yearlySalary = yearlySal;
}
public float getYearlySalary(){
return yearlySalary;
}
@Override
public float calculatePay() {
return yearlySalary/52;
}
@Override
public String toString() {
return super.toString()+" Yearly Salary: "+yearlySalary;
}
}
###########
public class TestEmployee {
public static void main(String[] args) {
HourlyEmpl h1 = new HourlyEmpl(1, "Pravesh Kumar", 45, 65.34f);
HourlyEmpl h2 = new HourlyEmpl(2, "Alex Bob", 30, 60.50f);
SalaryEmpl s1 = new SalaryEmpl(3, "Raja Babu", 543234.65f);
Employee[] employees = {h1, h2, s1};
for(Employee e : employees){
System.out.println("Weekly Pay: "+e.calculatePay());
System.out.println("DETAILS: ");
System.out.println(e);
System.out.println();
}
}
}
/*
Sample run:
Weekly Pay: 2940.2998
DETAILS:
Employee Id: 1
Name: Pravesh Kumar
Hourly Rate: 65.34
Number of Hours Worked: 45
Weekly Pay: 1815.0
DETAILS:
Employee Id: 2
Name: Alex Bob
Hourly Rate: 60.5
Number of Hours Worked: 30
Weekly Pay: 10446.819
DETAILS:
Employee Id: 3
Name: Raja Babu
Yearly Salary: 543234.6
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.