Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define the Employee abstract superclass from which specific types of Employee\'s

ID: 3846221 • Letter: D

Question

Define the Employee abstract superclass from which specific types of Employee's can be derived. This abstract class contains one instance variable; name.

This class should contain a default constructor.

This class should contain a constructor which can be used to pass in the name of the Employee.

This class should contain the getName() method to retrieve the name instance variable

This class should contain the setName() method to set the name instance variable.

This class should contain an abstract method, getSalary(), which is not defined in this class because determining the salary of an Employee is dependent on the Employee type.

Define two derived classes from the Employee superclass:

The Commission class should be derived from the Employee class The salary for this type of employee is based on a sales commission percentage.

The Hourly class should be derived from the Employee class. The salary for this type of employee is based on a base pay plus tips collected.  

Each derived class should contain constructors, which call the constructor of the superclass.

Each class should implement a toString() method to provide a formatted output of the name, as well as the salary based on the Employee type.

Examples:

Explanation / Answer

abstract class Employee
{
private String name;
  
public Employee() //default constructor
{
name = " ";
}
  
public Employee(String name)//argument constructor
{
this.name = name;
}
//set and get methods
public void setName(String name)
{
this.name = name;
}
  
public String getName()
{
return name;
}
  
public abstract double getSalary(); //abstract method having no definition

  
}

class Commission extends Employee
{
private double commissionPer;
private double sales;
  
public Commission(String name,double sales,double commissionPer)
{
super(name);//calling base class Employee's constructor to initialize name
this.sales = sales;
this.commissionPer = commissionPer;
}
public double getSalary()
{
return sales *commissionPer/100;//compute salary from sales and commissionPercentage
}
public String toString()
{
return " "+ getName() +" salary is $"+getSalary();
}
}
class Hourly extends Employee
{
private double basePay;
private double tips;
  
  
public Hourly(String name,double basePay,double tips)
{
super(name);//calling base class Employee's constructor to initialize name
this.basePay = basePay;
this.tips = tips;
}
public double getSalary()
{
return basePay+tips;//compute salary from base salary and tips
}
public String toString()
{
return " "+ getName() +" salary is $"+getSalary();
}
}

class TestEmployee
{
   public static void main (String[] args)
   {
   Commission employee1 = new Commission( "Wendy Smith", 40000, 4 );
Hourly employee2 = new Hourly( "Bruno Hildago", 800, 325 );

System.out.println( employee1 );
  

System.out.println( employee2 );
     
     
   }
}

Output:

Wendy Smith salary is $1600.0

Bruno Hildago salary is $1125.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote