Can someone please help me modify the Employee class (hierarchy) to include a ne
ID: 3670036 • Letter: C
Question
Can someone please help me modify the Employee class (hierarchy) to include a new subclass named "CommissionEmployee". CommissionEmployee will include a double class variable named 'commission'. Add a non argument constructor and a constructor that receives 3 arguments (the firstName, the lastName and the commission; add the set and get methods;
optionally add the toString method.
Use the EmployeeTest class to create 2 instances of the CommissionEmployee. Print the results. Submit the CommissionEmployee class andEmployeeTest class.
Explanation / Answer
public class Employee
{
private String firstName;
private String lastName;
private double salary;
public Employee()
{
firstName = "";
lastName = "";
salary = 0;
}
public Employee (String fName, String lName)
{
firstName = fName;
lastName = lName;
salary = 0;
}
public Employee( String fName, String lName, double s)
{
firstName = fName;
lastName = lName;
setSalary( s );
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public double getSalary()
{
return salary;
}
public void setFirstName( String fName)
{
firstName = fName;
}
public void setLastName ( String lName)
{
lastName = lName;
}
public void setSalary( double s)
{
if (s < 0 )
salary = 0;
else
salary = s;
}
public String toString()
{
return firstName + " " + lastName + " " + salary;
}
}
class CommissionEmployee extends Employee{
private double commmission;
public CommissionEmployee(){
this.commmission = 0;
}
public CommissionEmployee(String firstName, String lastName, double commmission) {
super(firstName,lastName);
this.commmission = commmission;
}
public double getCommmission() {
return commmission;
}
public void setCommmission(double commmission) {
this.commmission = commmission;
}
public String toString()
{
return "CommisionEmp["+getFirstName() + " " + getLastName() + " " + getSalary()+
" "+getCommmission()+" ]";
}
}
class EmployeeTest{
public static void main(String[] args) {
Employee e1 = new Employee("Alex","Bob");
CommissionEmployee ce = new CommissionEmployee("Mittal", "Sardha", 12);
System.out.println(e1);
System.out.println(ce);
}