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

Inheritance and Polymorphism A company pays its employees on a weekly basis. The

ID: 3685829 • Letter: I

Question

Inheritance and Polymorphism A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly employees are paid by the hour and receive overtime pay (1.5 * wage) for all hours worked in the excess of 40 hours, commission employees are paid a percentage of their sales and base-commission employees receive a base salary plus a commission based on their gross sales. The company wants to implement a java application that performs its payroll calculations polymorphically. (50 points) Employee hierarchy UML Class diagram 1. Write an Abstract Java class – Employee that encapsulates an employee’s first name, last name, SSN. Implement the appropriate get and set methods along with toString( ) method and equals( ) method. Note: An abstract class is a class that cannot be instantiated. The class is declared as follows: public abstract class Employee Also provide an abstract method called getEarnings( ) with return type of double type. The purpose of the getEarnings( ) method is to return the earnings that each type of employee makes. Note: an abstract method is a method with no body. Below is it’s declaration public abstract double getEarnings( ) ; 2. Create a Java class – SalariedEmployee which is a special type of Employee with the following additional attribute: weeklySalary. Provide appropriate get, set and equals( ), toString( ) methods. Implement a newer version of getEarnings( ) method that returns the earnings of SalariedEmployee: weeklySalary. 3. Create another Java class – HourlyEmployee which is another special type of Employee with the additional attributes: wage and hours. Provide appropriate set and get methods along with equals( ), toString method. Also implement a newer version of getEarnings( ) method that reflects earnings of HourlyEmployee. (Note: take into the account the rules for calculating the earnings for hourly employee as stated earlier in the problem description) 4. Create Java class – CommissionEmployee, another type of employee with attributes: grossSales and commissionRate. Provide set, get and equals( ), toString( ) methods. Provide a newer version of getEarnings( ) method that returns the earnings as (commissionRate * grossSales). 5. Implement a specialized type of CommissionEmployee called BasePlusCommissionEmployee class which has an additional attribute: base

salary. Provide get, set and equals( ), toString( ) methods. Implement the getEarnings( ) method that returns the earnings as (commissionRate * grossSales) + baseSalary.

Note for all above User Defined Classes:

Provide appropriate validation code so the right values get populated in the instance variables. For example, the payrate should not be negative.

Make sure to provide JavaDoc style comments.

Write a Java application (Client) program with a static method called generateEmployees( ) that returns a random list of 10 different types of Employee objects. You could either use an array or an ArrayList to store the employee objects that will be returned. Use a for loop to populate randomly different types of employee objects with some random data. You could possibly think a range of values like 1 – 4. If random value is 1, create a HourlyEmployee object with some randomly generated data, if 2, SalariedEmployee object with some random data and so on. I would leave it to your ingenuity to generate and populate these different Employee objects. As these objects are generated add them to your data structure (array or ArrayList that you are using). Finally the method returns this data structure.

In the same application class, implement the main( ) method. Call the generateEmployees( ) static method and using a for loop to print the details of each of the employee along with their earnings on the output window.

(70 points)

For the below descriptions your task is to draw a UML class diagram showing the various interfaces and classes and next simply state in sentences all the concepts that you learned from this exercise about inheritance, polymorphism and interfaces.

An interface – Monster with a method menace() with return type as void

An interface – DangerousMonster which inherits the Monster interface and has a method called destroy() with void return type.
(Note: Just like a class can inherit from another class, an interface can inherit from another interface using the “extends” keyword.

An interface – Lethal with a method – Kill( ) with void return type.

A class DragonZilla which implements DangerousMonster. This class does not have any methods of it’s own but provides implementation for the methods from DangerousMonster. The implementation of these methods simply prints meaning full statements that includes the name of the class and method using System.out.println() eg. “DragonZilla can be menace to children”

An interface – Vampire that extends DangerousMonster and Lethal interfaces. This method has it’s own method – drinkBlood( ) with return type as void.
(Note: An interface can inherit from multiple interfaces but a class can inherit from at most one Class.)

A class VeryBadVampire that implements Vampire interface. The methods that are inherited would simply provide an implementation that will print the name of the class and name of the method just like in step d.

A class HorrorShow with the following implementation.

public class HorrorShow

{

public static void u(Monster b)

{

b.menace();

}

public static void v(DangerousMonster d)

{

d.menace();

d.destroy();

}

public static void w(Lethal l)

{

l.kill();

}

public static void main(String[] args)

{

DangerousMonster barney = new DragonZilla();

u(barney);

v(barney);

Vampire vlad = new VeryBadVampire();

u(vlad);

v(vlad);

w(vlad);

                        }

}

Explanation / Answer


package payroll;

import java.util.ArrayList;

public class Payroll {

    public static void main(String[] args) {
      ArrayList list=new ArrayList(10);
      salariedEmployee e1=new salariedEmployee("Jhon","Smith","123-345-345",10000);
      list.add(e1);
      HourlyEmployee e2=new HourlyEmployee("Stephen","Jhon","163-312-310",400,20);
      list.add(e2);
      CommissionEmployee e3=new CommissionEmployee("Mary","Karts","812-234-310",50000,30/100);
      list.add(e3);
      BasePlusCommissionEmployee e4=new BasePlusCommissionEmployee("Mary","Karts","812-234-310",50000,30/100,7000);
      list.add(e4);
      System.out.println(" Employee details");
     for(int i=0;i<list.size();i++)
     {
         System.out.println(list.get(i).toString());
     }
    }
   
}


package payroll;

public abstract class Employee{
/*Employee that encapsulates an employee’s first name,
last name, SSN.*/
String firstName;
String lastName;
String SSN;
public Employee(String first, String last, String ssn)
{
    firstName=first;
    lastName=last;
    SSN=ssn;
}
/* abstract method called getEarnings( ) with return type of double type. The purpose
of the getEarnings( ) method is to return the earnings that each type of employee makes.*/

abstract double getEarnings();

/*Implement the appropriate get and set methods along with toString( )
method and equals( ) method.*/
public boolean equals(Employee e)
{
    boolean ans=false;
    if(firstName.equalsIgnoreCase(e.firstName))
        if(lastName.equalsIgnoreCase(e.lastName))
            if(SSN.equals(e.SSN))
                ans=true;
    return ans;
}
public void setFirstName(String f)
{
    firstName=f;
}
public void setLastName(String l)
{
    lastName=l;
}
public String getFirstName()
{
    return firstName;
}
public String getLastName()
{
    return lastName;
}
public void setSSN(String ssn)
{
    SSN=ssn;
}
public String getSSN()
{
    return SSN;
}
@Override
public String toString()
{
    return firstName+" "+lastName+" "+SSN;
}
}


package payroll;
/*SalariedEmployee which is a special type of Employee with the following
Provide appropriate get, set and equals( ), toString( ) methods.
Implement a newer version of getEarnings( ) method that returns the earnings of SalariedEmployee: weeklySalary*/
public class salariedEmployee extends Employee{
    //additional attribute: weeklySalary.
double weeklySalary;
    public salariedEmployee(String first, String last, String ssn,double sal) {
        super(first, last, ssn);
        weeklySalary=sal;
    }
public void setWeeklySalary(double sal)
{
    weeklySalary=sal;
}
    @Override
    double getEarnings() {
        return weeklySalary;
    }
public boolean equals(salariedEmployee S)
{
    boolean ans=false;
    if(this.equals((Employee)S) && this.weeklySalary==S.weeklySalary)
        ans=true;
    return ans;
}
@Override
public String toString()
{
    return ((Employee)this).toString()+" "+weeklySalary;
}
}


package payroll;

public class HourlyEmployee extends Employee{
double wage;
int hours;
    public HourlyEmployee(String first, String last, String ssn,double w, int h) {
        super(first, last, ssn);
        wage=w;
        hours=h;
    }

    @Override
    double getEarnings() {
        return wage*hours;
        }
   
    public boolean equals(HourlyEmployee S)
{
    boolean ans=false;
    if(this.equals((Employee)S) && getEarnings()==S.getEarnings())
        ans=true;
    return ans;
}
@Override
public String toString()
{
    return ((Employee)this).toString()+" "+getEarnings();
}

}


package payroll;

public class CommissionEmployee extends Employee{
double grossSales;
double commissionRate;
    public CommissionEmployee(String first, String last, String ssn, double gsales, double cr) {
        super(first, last, ssn);
        grossSales=gsales;
        commissionRate=cr;
    }

    @Override
    double getEarnings() {
        return grossSales*commissionRate;
    }
       public boolean equals(CommissionEmployee S)
{
    boolean ans=false;
    if(this.equals((Employee)S) && getEarnings()==S.getEarnings())
        ans=true;
    return ans;
}
@Override
public String toString()
{
    return ((Employee)this).toString()+" "+getEarnings();
}

}


package payroll;

public class BasePlusCommissionEmployee extends CommissionEmployee{
double baseSalary;
    public BasePlusCommissionEmployee(String first, String last, String ssn, double gsales, double cr,double base) {
        super(first, last, ssn,gsales,cr);
        baseSalary=base;
    }

    @Override
    double getEarnings() {
        return ((CommissionEmployee)(this)).getEarnings()+baseSalary;
    }

   public boolean equals(BasePlusCommissionEmployee S)
{
    boolean ans=false;
    if(this.equals((CommissionEmployee)S) && getEarnings()==S.getEarnings())
        ans=true;
    return ans;
}
@Override
public String toString()
{
    return ((CommissionEmployee)this).toString()+" "+getEarnings();
}
}