In: Computer Science
Design a class named Employee. The class should keep the following information in fields:
· Employee name
· Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M.
· Hire date
Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields to hold the following information:
· Shift (an integer)
· Hourly pay rate (a double)
· Add hours and calculate pay
The workday is divided into two shifts: day and night. The shift field will be an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2. Write one or more constructors and the appropriate accessor and mutator methods for the class. Demonstrate the classes by writing a program that uses a ProductionWorker object.
/*Java program to create a super class Employee and a sub class
ProductionWorker .Then constructors
of super class and class are added.Accessor and mutator methods for
all the fields are added.
In main program create the object of subclass and pass the
employee name,number,hiredate, shift,hourly pay rate,
and hours are passed to the constructor. and the program computes
the pay of an employee and display all the informations*/
//The super class Employee
class Employee
{
//data fields
private String name;
private String hiredate;
private String number;
//constructors to set name,number and hiredate
public Employee(String n,String hd,String num)
{
name=n;
hiredate=hd;
number=num;
}
//accessors methods of Employee class defined
below
public String getName()
{
return name;
}
public String getHireDate()
{
return hiredate;
}
public String getNumber()
{
return number;
}
//mutator methods of Employee class defined
below
public void setName(String n)
{
name=n;
}
public void setHireDate(String hd)
{
hiredate=hd;
}
public void setNumber(String n)
{
number=n;
}
}
class ProductionWorker extends Employee
{
private int shift;
private double hourlyRate;
private int hours;
//constructors to set
name,number and hiredate, shift,hourly rate and hours
public ProductionWorker(String
ename,String enumber,String hd,int sh,double hrate,int hr)
{
super(ename,hd,enumber);
shift=sh;
hourlyRate=hrate;
hours=hr;
}
//accessors methods of
ProductionWorker class defined below
public int getHours()
{
return
hours;
}
public int getShift()
{
return
shift;
}
public double getHourlyRate()
{
return
hourlyRate;
}
//mutator methods of
ProductionWorker class defined below
public void setShift(int sh)
{
shift=sh;
}
public void setHourlyRate(double
hr)
{
hourlyRate=hr;
}
public void setHours(int
hr)
{
hours=hr;
}
public String getName()
{
return
super.getName();
}
public String getHireDate()
{
return
super.getHireDate();
}
public String getNumber()
{
return super.getNumber();
}
//calculate the pay
public double calculatePay()
{
double
pay=hours*hourlyRate;
return pay;
}
}
public class TestEmp
{
public static void main(String args[])
{
//test1
ProductionWorker worker1=new
ProductionWorker("Harry
Biston","222B","12-4-2015",1,100.00,6);
System.out.println("Employee
Name :"+worker1.getName());
System.out.println("Employee
Number :"+worker1.getNumber());
System.out.println("Hire
Date :"+worker1.getHireDate());
String shift=worker1.getShift()==1?
" Day":" Night";
System.out.println("Shift
:"+shift);
System.out.println("Hourly
rate :"+worker1.getHourlyRate());
System.out.println("Hours
worked :"+worker1.getHours());
System.out.println("Pay
= :"+worker1.calculatePay());
System.out.println("\n");
//test2
ProductionWorker worker2=new
ProductionWorker("SSg Mishra","124H","22-1-2010",2,120.00,5);
System.out.println("Employee
Name :"+worker2.getName());
System.out.println("Employee
Number :"+worker2.getNumber());
System.out.println("Hire
Date :"+worker2.getHireDate());
shift=worker2.getShift()==1? "
Day":" Night";
System.out.println("Shift
:"+shift);
System.out.println("Hourly
rate :"+worker2.getHourlyRate());
System.out.println("Hours
worked :"+worker2.getHours());
System.out.println("Pay
= :"+worker2.calculatePay());
}
}
--------------------------------------------------------
output