In: Computer Science
This is for a java program.
Payroll System Using Inheritance and
Polymorphism
1. Implement an interface called EmployeeInfo with the following
constant variables:
FACULTY_MONTHLY_SALARY = 6000.00
STAFF_MONTHLY_HOURS_WORKED = 160
2. Implement an abstract class Employee with the following requirements:
Attributes
last name (String)
first name (String)
ID number (String)
Sex - M or F
Birth date - Use the Calendar Java class to create a date object
Default argument constructor and argument constructors.
Public methods
toString - returning a string with the following format:
ID Employee number :_________
Employee name: __________
Birth date: _______
mutators and accessors
abstract method monthlyEarning that returns the monthly earning.
3. Implement a class called Staff extending from the class Employee with the following requirements:
Attribute
Hourly rate
Default argument and argument contructors
Public methods
get and set
The method monthlyEarning returns monthly salary (hourly rate times 160)
toString - returning a string with the following format:
ID Employee number :_________
Employee name: __________
Birth date: _______
Full Time
Monthly Salary: _________
Implelment a class Education with the following requirements:
Attributes
Degree (MS or PhD )
Major (Engineering, Chemistry, English, etc ... )
Research (number of researches)
Default argument and argument constructors.
Public methods
get and set
Implement a class Faculty extending from the class Employee with the following requirements:
Attributes
Level (Use enum Java)
"AS": assistant professor
"AO": associate professor
"FU": professor
Education object
Default argument and argument constructor
Public methods
mutators and accessors
The method monthlyEarning returns monthly salary based on the
faculty's level.
AS - faculty monthly salary
AO - 1.5 times faculty monthly salary
FU - 2.0 times faculty monthly salary
toString - returning a string with the following format:
ID Employee number :_________
Employee name: __________
Birth date: _______
XXXXX Professor where XXXXX can be Assistant, Associate or
Full
Monthly Salary: _________
Implement a class called Partime extending from the class Staff with the following requirements:
Attributes
Hours worked per week
Default argument and argument constructors
Public methods
mutators and accessors
The method monthlyEarning returns monthly salary . The monthly salary is equal to hourly rate times the hours worked in four weeks.
toString - returning a string with the following format:
ID Employee number :_________
Employee name: __________
Birth date: _______
Hours works per month: ______
Monthly Salary: _________
mplement a test driver program that creates a one-dimensional
array of class Employee to store the objects Staff, Faculty and
Partime.
Using polymorphism, display the following outputs:
a. Employee information using the method toString
Staff
Faculty
Part-time
b. Total monthly salary for all the part-time staff .
c. Total monthly salary for all employees.
d. Display all employee information descending by employee id using
interface Comparable
e. Display all employee information ascending by last name using
interface Comparer
f. Duplicate a faculty object using clone. Verify the
duplication.
Test Data
Staff
Last name: Allen
First name: Paita
ID: 123
Sex: M
Birth date: 2/23/59
Hourly rate: $50.00
Last name: Zapata
First Name: Steven
ID: 456
Sex: F
Birth date: 7/12/64
Hourly rate: $35.00
Last name:Rios
First name:Enrique
ID: 789
Sex: M
Birth date: 6/2/70
Hourly rate: $40.00
Faculty
Last name: Johnson
First name: Anne
ID: 243
Sex: F
Birth date: 4/27/62
Level: Full
Degree: Ph.D
Major: Engineering
Reseach: 3
Last name: Bouris
First name: William
ID: 791
Sex: F
Birth date: 3/14/75
Level: Associate
Degree: Ph.D
Major: English
Reseach: 1
Last name: Andrade
First name: Christopher
ID: 623
Sex: F
Birth date: 5/22/80
Level: Assistant
Degree: MS
Major: Physical Education
Research: 0
Part-time
Last name: Guzman
First name: Augusto
ID: 455
Sex: F
Birth date: 8/10/77
Hourly rate: $35.00
Hours worked per week: 30
Last name: Depirro
First name: Martin
ID: 678
Sex: F
Birth date: 9/15/87
Hourly rate: $30.00
Hours worked per week:15
Last name: Aldaco
First name: Marque
ID: 945
Sex: M
Birth date: 11/24/88
Hourly rate: $20.00
Hours worked per week: 35
Test.java
import java.util.*;
public class Test
{
public static void main(String[] args) throws
CloneNotSupportedException
{
//test information on website
Employee [] a1 = new Employee[9];
a1[0] = new
Staff("Allen","Paita","123","M",1959,2,23,50);
a1[1] = new
Staff("Zapata","Steven","456","F",1964,7,12,35);
a1[2] = new
Staff("Rios","Enrique","789","M",1970,6,2,40);
a1[3] = new
Faculty("Johnson","Anne","243","F",1962,4,27,Level.FU,"Ph.D","Engineering",3);
a1[4] = new
Faculty("Bouris","William","791","F",1975,3,14,Level.AO,"Ph.D","English",1);
a1[5] = new
Faculty("Andrade","Christopher","623","F",1980,5,2,Level.AS,"MS","Physical
Education",0);
a1[6] = new
Partime("Guzman","Augusto","455","F",1977,8,10,35,30);
a1[7] = new
Partime("Depirro","Martin","678","F",1987,9,15,30,15);
a1[8] = new
Partime("Aldaco","Marque","945","M",1988,24,11,20,35);
System.out.println("A:");
System.out.println("---------------------------------------------------------------");
for(int i = 0; i<9;i++)
{
System.out.println(a1[i].toString());
}
System.out.println("----------------------------------------------------------------");
System.out.println("B: Total Monthly Salary for
Partime");
System.out.println("----------------------------------------------------------------");
double partimeTotal=0;
for(int i = 0; i<9;i++)
{
if(a1[i] instanceof Partime)
{
partimeTotal=partimeTotal + a1[i].monthlyEarning();
}
}
System.out.println("The total Monthly Salary for
Partime is: $" + partimeTotal);
System.out.println("----------------------------------------------------------------");
System.out.println("C: Total Monthly Salary for
All Employees");
System.out.println("----------------------------------------------------------------");
double total = 0;
for(int i = 0; i<9;i++)
{
total = total +
a1[i].monthlyEarning();
}
System.out.println("The total monthly salary for
all employees is: $" + total);
System.out.println("----------------------------------------------------------------");
System.out.println("D: Sorted by ID Number
");
System.out.println("----------------------------------------------------------------");
System.out.println(" " );
bubbleSort(a1);
for(int i = 0; i<9;i++)
{
System.out.println(a1[i].toString());
}
System.out.println("----------------------------------------------------------------");
System.out.println("E: Sorted by Last Name "
);
System.out.println("---------------------------------------------------------------");
bubbleSortbyName(a1);
for(int i = 0; i<9;i++)
{
System.out.println(a1[i].toString());
}
System.out.println("----------------------------------------------------------------");
System.out.println("F: Cloning");
System.out.println("----------------------------------------------------------------");
Faculty abc = new
Faculty("Johnson","Anne","243","F",1962,4,27,Level.FU,"Ph.D","Engineering",3);
Faculty xyz = (Faculty)abc.clone();
Education e = new
Education("MS","Engineering",3);
xyz.setE(e);
System.out.println("Clone:
"+xyz.toString());
System.out.println("Original: " +
abc.toString());
System.out.println("----------------------------------------------------------------");
}
/**
* bubble sort will call compareTo or Comparable to compare by ID
number
* @param a1 will be the array with all your Employees
*/
public static void bubbleSort(Employee[] a1) {
boolean swapped = true;
int j = 0;
Employee tmp;
while (swapped) {
swapped =
false;
j++;
for (int i =
0; i < a1.length - j; i++)
{
if (a1[i].compareTo( a1[i + 1])==1)
{
tmp = a1[i];
a1[i] = a1[i + 1];
a1[i + 1] = tmp;
swapped = true;
}
}
}
}
/**
* Bubble Sort will sort by using compare or Comparer and will sort
by Last Name
* @param a1 will be the array with all your Employees
*/
public static void bubbleSortbyName(Employee[] a1) {
boolean swapped = true;
int j = 0;
Employee tmp;
while (swapped) {
swapped =
false;
j++;
for (int i =
0; i < a1.length - j; i++) {
if (a1[i].compare(a1[i], a1[i + 1])>0)
{
tmp = a1[i];
a1[i] = a1[i + 1];
a1[i + 1] = tmp;
swapped = true;
}
}
}
}
}
Employee.java
import java.util.*;
public abstract class Employee implements
Cloneable,Comparator<Object>,Comparable<Object>
{
/**
* @lastName last name of Employee
*/
String lastName;
/**
* @firstName first name
*/
String firstName;
/**
* @idNum ID number
*/
String idNum;
/**
* @sex sex(male or female)
*/
String sex;
/**
* @bDay will be object for date of birth
of Employees
*/
Calendar bDay = new GregorianCalendar();
//constructor
/**
* this will be your constructor for
class
* @param last last name
* @param first first name
* @param id id number
* @param s sex(male or female)
* @param year year born in
* @param month month born in
* @param day day born on
*/
public Employee(String last,String first,String id,String s,int
year, int month,int day)
{
lastName=last;
firstName = first;
idNum = id;
sex = s;
bDay.set(Calendar.YEAR,year);
bDay.set(Calendar.MONTH,month);
bDay.set(Calendar.DAY_OF_MONTH,day);
}
//methods
//comparable
/**
* @Override
* This will be your comparable and will no compare by object ID
Number
*/
public int compareTo(Object obj)//ID
{
Employee e = (Employee)obj;
int e2 = Integer.parseInt(e.getIDNum());
int e1 = Integer.parseInt(this.idNum);
if(e1<e2)
return 1;
else
return -1;
}
//comparator
/**
* @Override
* This will be your comparator and it will compare by object Last
Name
*/
public int compare(Object o1,Object o2)//Last Name
{
Employee e1 =(Employee)o1;
Employee e2 =(Employee)o2;
return e1.lastName.compareTo(e2.lastName);
}
/**
* This will get Last Name of Employee
* @return last name
*/
public String getLastName()
{
return lastName;
}
/** this will get the first name of Employee
*
* @return first name
*/
public String getFirstName()
{
return firstName;
}
/**
* This will get ID number of Employee
* @return ID number
*/
public String getIDNum()
{
return idNum;
}
/**
* This will get the sex of Employee
* @return Male or Female
*/
public String getSex()
{
return sex;
}
/**
* This will get the Birthday of Employee
* @return mm/dd/yyyy
*/
public String getBday()
{
int year=bDay.get(Calendar.YEAR);
int month=bDay.get(Calendar.MONTH);
int day=bDay.get(Calendar.DAY_OF_MONTH);
return String.valueOf(month) + "/" +
String.valueOf(day) + "/" + String.valueOf(year);
}
/**
* will change the last name of Employee
* @param change Last name desired
*/
public void setLastName(String change)
{
lastName=change;
}
/**
* will change the first name of Employee
* @param change first name desired
*/
public void setFirstName(String change)
{
firstName=change;
}
/**
* will change the ID number of Employee
*
* @param change ID number desired
*/
public void setID(String change)
{
idNum = change;
}
/**
* will change the sex of Employee
* @param change sex desired(male or female)
*/
public void setSex(String change)
{
sex = change;
}
/**
* will change the Birthday of Employee
* @param month the month desired
* @param day day desired
* @param year year desired
*/
public void setBday(int month,int day,int year)
{
bDay.set(Calendar.MONTH,month);
bDay.set(Calendar.DAY_OF_MONTH,day);
bDay.set(Calendar.YEAR,year);
}
/**
* This will display all important attributes of this class.
*/
public String toString()
{
return "ID Employee number: " + idNum + "\n" +
"Employee Name: " + lastName+" "+firstName + "\n" + "Birth Date: "
+ bDay.get(Calendar.MONTH)+" / "+bDay.get(Calendar.DAY_OF_MONTH)+"
/ "+bDay.get(Calendar.YEAR) + "\n";
}
/**
* This is will be your abstract method that will be used in child
classes
* @return monthly earning of employee
*/
public abstract double monthlyEarning();
}
Staff.java
import java.util.*;
public class Staff extends Employee
{
/**
* @hourlyRate this will be hourly rate for
Employee
*/
int hourlyRate;
/**
* This is your constructor to initialize
the following
* @param last last name
* @param first first name
* @param id ID number
* @param s sex(male or female)
* @param year Year born in
* @param month month born in
* @param day day born in
* @param hr hourly rate
*/
public Staff(String last,String first,String
id,String s,int year,int month,int day,int hr)
{
super(last,first,id,s,year,month,day );
hourlyRate=hr;
}
/**
* gets the Hourly Rate of Employee
* @return Hourly rate
*/
public int getHourlyRate()
{
return hourlyRate;
}
/**
* Changes the Hourly Rate of
Employee
* @param change new Hourly Rate
*/
public void setHourlyRate(int change)
{
hourlyRate=change;
}
/**
* Monthly Earning calculates the
Employee's monthly earning
*/
public double monthlyEarning()
{
double
monthlySalary=hourlyRate*EmployeeInfo.staff_monthly_hours_worked;
return
monthlySalary;
}
/**
* Displays attributes of class and
abstract class Employee
*/
public String toString()
{
return super.toString()
+ "Full Time\n"+"Monthly Salary: " + monthlyEarning() + "\n";
}
}
Partime.java
public class Partime extends Staff
{
/**
* @hoursWorkedPerWeek this will be the
amount of hours the employee works each week
*/
int hoursWorkedPerWeek;
/**
* This will be your constructor for
Partime.
* @param last This will be their last
name
* @param first This will be their first
name
* @param id this will be id number
* @param s sex(male or female)
* @param year year they were born in
* @param month month they were born
in
* @param day day they were born in
* @param hourRate how much they earn each
hour
* @param hpw how much they worked each
week
*/
public Partime(String last,String first,String
id,String s,int year,int month,int day,int hourRate,int hpw)
{
super(last, first, id,
s,year,month,day, hourRate);
hoursWorkedPerWeek =
hpw;
}
/**
* This will give the integer value of how
many hours the Employee worked that week
* @return integer value of hours worked
that week
*/
public int getHoursWorkedPerWeek()
{
return
hoursWorkedPerWeek;
}
/**
* This will change the integer value of
hours the employee works in a week
* @param change the new amount of hours
per week
*/
public void settHoursWorkedPerWeek(int
change)
{
hoursWorkedPerWeek =
change;
}
/**
* Monthly Earning will calculate how much
the employee earns each month
*/
public double monthlyEarning()
{
return
hoursWorkedPerWeek *super.hourlyRate* 4;
}
/**
* Displays last name, first name, ID
number, sex(male or female), Birthday, hour per week, and monthly
salary for that Employee
*/
public String toString()
{
return super.toString()
+ "\n" + "Hours worked per Week: " + hoursWorkedPerWeek + "\n" +
"Monthly Salary: " + monthlyEarning() + "\n";
}
}
Education.java
public class Education implements Cloneable
{
/**
* @degree This is the degree they received
from college
*/
String degree;
/**
* @major This is what they majored in
college
*/
String major;
/**
* @research How many research projects
they have done
*/
int research;
/**
* constructor for Education initializes
degree,major,and research
* @param d Degree
* @param m Major
* @param r Research
*/
public Education(String d, String m, int
r)
{
degree = d;
major = m;
research = r;
}
/**
* Gets the Degree the Employee
received
* @return Degree(Bachelors,PH.D etc)
*/
public String getDegree()
{
return degree;
}
/**
* Gets Major of Employee
* @return Major(Computer Science,Biology,
English, etc)
*/
public String getMajor()
{
return major;
}
/**
* Gets the number of Research Projects the
person has done
* @return Number of Research Projects
(1,2,3, etc)
*/
public int getReserch()
{
return research;
}
/**
* Changes and Employee's Degree
* @param change Degree you want to replace
the current one with
*/
public void setDegree(String change)
{
degree=change;
}
/**
* Changes the Major of an Employee
* @param change Major you want to replace
the current one with
*/
public void setMajor(String change)
{
major = change;
}
/**
* Changes the number of Research
Projects
*
* @param change number you want to replace
the current one with
*/
public void setResearch(int change)
{
research = change;
}
/**
* clone is here to just clone b. Also if
equals doesn't work then use this one instead of the one in
Faculty
*
*/
public Object clone() throws
CloneNotSupportedException
{
Education b = (Education) super.clone();
/*
b.setMajor(major);
b.setDegree(degree);
b.setResearch(research);
*/
//if equals doesnt work
out comment
return b;
}
/**
* @Override
* Changes the equal fucntion to compare
Employee's degree, major, and research. This to make sure clone()
has worked and cloned two objects
* succesfully.
* @param o1 Object to compare with
implicit.
*/
public boolean equals(Object o1)
{
Education e =
(Education)o1;
if(this.degree.equals(e.degree) &&
this.major.equals(e.major) &&
this.research==e.research)
{
return true;
}
else
return false;
}
}
EmployeeInfo.java
public interface EmployeeInfo
{
final double
faculty_monthly_salary=6000;
final int
staff_monthly_hours_worked=160;
}
Faculty.java
public class Faculty extends Employee
{
/**
* @position This will be the Employee's
position in the company
*/
Level position;
/**
* @e highest education received
*/
Education e;
/**
* @monthlySalary employee's monthly
salary
*/
int monthlySalary;
/**
* Constructor for Faculty will initialize
values for class
* @param last last name
* @param first first name
* @param id ID number
* @param s sex(Male or Female)
* @param year year they were born in
* @param month month they were born
in
* @param day day they were born
* @param fu position in company
* @param d degree they received from
college
* @param m what they majored in
* @param r research projects they have
done
*/
public Faculty(String last,String first,String
id,String s,int year,int month,int day,Level fu,String d,String
m,int r)
{
super(last,first,id,s,year,month,day);
this.position=fu;
e= new
Education(d,m,r);
}
//methods
/**
* Finds the how much the employee earns
each month. Dependent on position on company as well.
* @return returns Employees Monthly
earnings
*/
public double monthlyEarning()
{
switch(position)
{
case AS:
return EmployeeInfo.faculty_monthly_salary;
case AO:
return EmployeeInfo.faculty_monthly_salary*1.5;
case FU:
return EmployeeInfo.faculty_monthly_salary*2.0;
default:
System.out.println("There's a PROBLEM OVER HERE!(Facutly
Class->monthlyEarning!");
return 0;
}
}
/**
* Displays everything important for this
class in particular. Uses super to display everything important
from Employee
* In particulart it displays
position,degree,major,and research
*/
public String toString()
{
return super.toString()
+ "Level: " + position + "\n" + "Degree: " + e.getDegree() + "\n" +
"Major: " + e.getMajor() + "\n" + "Research: " + e.getReserch() +
"\n" + "Monthly Salary: "+ monthlyEarning() + "\n";
}
/**
* clones objects b and e.
* @return returns object from
Faculty
*/
public Object clone() throws
CloneNotSupportedException
{
Faculty b = (Faculty)
super.clone();
e = (Education)
e.clone();
b.setE(e);
return b;
}
/**
* get position of Employee
* @return AS,AO, FU
*/
public Level getPosition() {
return position;
}
/**
* set position changes the position of
Employee
* @param Position you want it changed
too
*/
public void setPosition(Level position) {
this.position =
position;
}
/**
* Gets Education of Employee
* @return Education could be PH.D or
Bachelors
*/
public Education getE() {
return e;
}
/**
* Changes Education of Employee
* @param Education you want it changed
too
*/
public void setE(Education e) {
this.e = e;
}
/**
* gets the Monthly Salary
* @return Monthly Salary of Employee
*/
public int getMonthlySalary() {
return
monthlySalary;
}
/**
* Changes the monthly salary of
Employee
* @param monthlySalary salary you want it
changed too
*/
public void setMonthlySalary(int monthlySalary)
{
this.monthlySalary =
monthlySalary;
}
/**
* @Override
* Changes the equals function to compare 2
objects. Tests Clone method to make sure it works
* @return return true or false.
*/
public boolean equals(Object o1)
{
Faculty f =
(Faculty)o1;
if(this.e.equals(f.e))
{
return true;
}
else
{
return false;
}
}
}
Level.java
public enum Level
{
AS, //Assistant Professor
AO, //Associate Professor
FU; //Full Time Professor
}