Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA REWRITE so that all employee information is in the Employee Class or Faculty Class, CAN not be in the main method anymore!, Output should

JAVA

REWRITE so that all employee information is in the Employee Class or Faculty Class, CAN not be in the main method anymore!, Output should look exactly the same!!! Please Comment the changes you made.

So basically, a line like this should not be in the main method: a1[0] = Factory.getStaff("Allen", "Paita", "123", "M", 1959, 2, 23, 50);

The information has to be moved to the Employee/Faculty class according to its parameters. But when the main method executes, it should still print like it did before the changes. Thanks!

public interface EmployeeInfo { final double faculty_monthly_salary = 6000; final int staff_monthly_hours_worked = 160;

}

import java.util.*;

public abstract class Employee implements Cloneable, Comparator, Comparable

{

/** * * @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 birthDay = new GregorianCalendar();

// constructor

/** * * constructor for Employee 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;

birthDay.set(Calendar.YEAR, year);

birthDay.set(Calendar.MONTH, month);

birthDay.set(Calendar.DAY_OF_MONTH, day);

}

/** * * Compares objects by ID number * */

public int compareTo(Object obj)

{

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 * * Compares objects by last name * * Parameters are the last names * */

public int compare(Object o1, Object o2)

{

Employee e1 = (Employee) o1;

Employee e2 = (Employee) o2;

return e1.lastName.compareTo(e2.lastName);

}

/** * * get Last Name of Employee * * @return last name * */

public String getLastName()

{

return lastName;

}

/** * get the first name of Employee * * @return first name * */

public String getFirstName()

{

return firstName;

}

/** * * get ID number of Employee * * @return ID number * */

public String getIDNum()

{

return idNum;

}

/** * * get the sex of Employee * * @return Male or Female * */

public String getSex()

{

return sex;

}

/** * * get the Birthday of Employee using Calendar functions * * @return mm/dd/yyyy * */

public String getBday()

{

int year = birthDay.get(Calendar.YEAR);

int month = birthDay.get(Calendar.MONTH);

int day = birthDay.get(Calendar.DAY_OF_MONTH);

return String.valueOf(month) + "/" + String.valueOf(day) + "/" + String.valueOf(year);

}

/** * * mutator to change the last name of Employee * * @param change * Last name desired * */

public void setLastName(String change)

{

lastName = change;

}

/** * * mutator to change the first name of Employee * * @param change * first name desired * */

public void setFirstName(String change)

{

firstName = change;

}

/** * * mutator to change the ID number of Employee * * @param change * ID number desired * */

public void setID(String change)

{

idNum = change;

}

/** * * mutator to change the sex of Employee * * @param change * sex desired(male or female) * */

public void setSex(String change)

{

sex = change;

}

/** * * mutator to change the Birthday of Employee * * @param month * the month to change to * * @param day * the day to change to * * @param year * the year to change to * */

public void setBday(int month, int day, int year)

{

birthDay.set(Calendar.MONTH, month);

birthDay.set(Calendar.DAY_OF_MONTH, day);

birthDay.set(Calendar.YEAR, year);

}

/** * * toString formatted as specified by Assignment * */

public String toString()

{

return "ID Employee number: " + idNum + " " + "Employee Name: " + lastName + " " + firstName + " " + "Birth Date: " + birthDay.get(Calendar.MONTH) + " / " + birthDay.get(Calendar.DAY_OF_MONTH) + " / " + birthDay.get(Calendar.YEAR) + " " + "Sex: " + sex + " ";

}

/** * * abstract method to be used later * * @return monthly earning of employee * */

public abstract double monthlyEarning();

}

public class Staff extends Employee

{

/** * * @hourlyRate hourly rate for Employee * */

int hourlyRate;

/** * * constructor for Staff * * @param last * last name * * @param first * first name * * @param id * ID number * * @param s * sex * * @param year * Birth Year * * @param month * Birth month * * @param day * Birth day * * @param hr * hourly rate * */

public Staff(String last, String first, String id, String s, int year, int month, int day, int hr)

{

// super used to call parent constructor

super(last, first, id, s, year, month, day);

hourlyRate = hr;

}

/** * * gets the hourly Rate of Employee * * @return hourlyRate * */

public int getHourlyRate()

{

return hourlyRate;

}

/** * * Mutator for 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()

{

// staff_monthly_hours_worked from EmployeeInfo

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 " + "Monthly Salary: " + monthlyEarning() + " ";

}

}

public class Education implements Cloneable

{

/** * * @degree degree they received from college * */

String degree;

/** * * @major 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();

/* * * Education b = (Education) super.clone(); * * * * and * * * * b.setMajor(major); * * b.setDegree(degree); * * b.setResearch(research); * * are the same * */

return b;

}

/** * * Changes the equal function to compare Employee's degree, major, and research. * Checks if clone() has worked * * and cloned two objects successfully and individually. * * @param o1 * Object to compare * */

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;

}

}

public class Faculty extends Employee

{

public enum Level

{

AS, // Assistant Professor

AO, // Associate Professor

FU; // Full Time Professor

}

/** * * @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

/** * * Employee earings based on position * * @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;

}

}

/** * * toString formatted as specified by the assignment. uses the Super class * */

public String toString()

{

return super.toString() + "Level: " + position + " " + "Degree: " + e.getDegree() + " " + "Major: " + e.getMajor() + " " + "Research: " + e.getReserch() + " " + "Monthly Salary: " + monthlyEarning() + " ";

}

/** * * 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;

}

/** * * * * 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;

}

}

}

public class Partime extends Staff

{

/** * * @hoursWorkedPerWeek this will be the amount of hours the employee works each * week * */

int hoursWorkedPerWeek;

/** * * 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() + " " + "Hours worked per Week: " + hoursWorkedPerWeek + " " + "Monthly Salary: " + monthlyEarning() + " ";

}

}

public class Test

{

public static void main(String[] args) throws CloneNotSupportedException

{

//Changed lines below old have been commented Employee[] a1 = Factory.getEmployees(9); a1[0] = Factory.getStaff("Allen", "Paita", "123", "M", 1959, 2, 23, 50);

a1[1] = Factory.getStaff("Zapata", "Steven", "456", "F", 1964, 7, 12, 35);

a1[2] = Factory.getStaff("Rios", "Enrique", "789", "M", 1970, 6, 2, 40);

a1[3] = Factory.getFaculty("Johnson", "Anne", "243", "F", 1962, 4, 27, Faculty.Level.FU, "Ph.D", "Engineering", 3);

a1[4] = Factory.getFaculty("Bouris", "William", "791", "F", 1975, 3, 14, Faculty.Level.AO, "Ph.D", "English", 1);

a1[5] = Factory.getFaculty("Andrade", "Christopher", "623", "F", 1980, 5, 2, Faculty.Level.AS, "MS", "Physical Education", 0);

a1[6] = Factory.getPartime("Guzman", "Augusto", "455", "F", 1977, 8, 10, 35, 30);

a1[7] = Factory.getPartime("Depirro", "Martin", "678", "F", 1987, 9, 15, 30, 15);

a1[8] = Factory.getPartime("Aldaco", "Marque", "945", "M", 1988, 24, 11, 20, 35);

System.out.println("A:");

for (int i = 0; i < 9; i++)

{

System.out.println(a1[i].toString());

}

System.out.println("B: Total Monthly Salary for Partime");

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("C: Total Monthly Salary for All Employees");

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("D: Sorted by ID Number ");

System.out.println(" ");

bubbleSort(a1);

for (int i = 0; i < 9; i++)

{

System.out.println(a1[i].toString());

}

System.out.println("E: Sorted by Last Name ");

bubbleSortbyName(a1);

for (int i = 0; i < 9; i++)

{

System.out.println(a1[i].toString());

}

System.out.println("F: Cloning");

//Changed line below Faculty abc = Factory.getFaculty("Johnson", "Anne", "243", "F", 1962, 4, 27, Faculty.Level.FU, "Ph.D", "Engineering", 3);

Faculty xyz = (Faculty) abc.clone();

//Changed line below Education e = Factory.getEducation("MS", "Engineering", 3);

xyz.setE(e);

System.out.println("Clone: " + xyz.toString());

System.out.println("Original: " + abc.toString());

}

/** * * 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 by using compare or Comparer and to sort by Last Name * * @param a1 * array with all original names * */

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;

}

}

}

}

}

public class Factory {

public static Staff getStaff(String last, String first, String id, String s, int year, int month, int day, int hr) { return new Staff(last, first, id, s, year, month, day, hr); }

public static Faculty getFaculty(String last, String first, String id, String s, int year, int month, int day, Faculty.Level fu, String d, String m, int r) { return new Faculty(last, first, id, s, year, month, day, fu, d, m, r); }

public static Partime getPartime(String last, String first, String id, String s, int year, int month, int day, int hourRate, int hpw) { return new Partime(last, first, id, s, year, month, day, hourRate, hpw); }

public static Employee[] getEmployees(int i) { // TODO Auto-generated method stub return new Employee[i]; }

public static Education getEducation(String d, String m, int r) { // TODO Auto-generated method stub return new Education(d, m, r); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions