I am to create a program that works as follows:
1. Write a java class (Employee.java) that encapsulates an employees first name, last name, SSN. Implement the apporpriate get and set methods along with toString() methods. Implement a method called getEarnings() which simply returns the value 0.0
Employee.java
public class Employee { // instance variables protected String fName; protected String lName; protected double ssn; protected double earnings; public Employee() { earnings = 0.0; } public Employee(String first, String last, double socialSec) { fName = first; lName = last; ssn = socialSec; } public void setFirstName(String f_Name) { fName = f_Name; } public String getFirstName() { return fName; } public void setLastName(String l_Name) { lName = l_Name; } public String getLastName() { return lName; } public void setSsn(double social) { ssn = social; } public double getSsn() { return ssn; } public double getEarnings() { return earnings; } public String toString() { return ("The employees name is " + getFirstName() + " " + getLastName() + ". The employees social security number is " + getSsn()); } }
2. Create a java class (SalariedEmployee.java) which is a special type of Employee with the following additional attribute: weeklySalary. Provide appropriate get, set, and toString() methods. Implement a new version of getEarnings() mehtod that returns the earnings of the salaried Employee.
SalariedEmployee.java
public class SalariedEmployee extends Employee { private double weeklySalary; public SalariedEmployee() { super(); earnings = 0.0; } public SalariedEmployee(double weeklySal) { super(); if(weeklySal > 0) weeklySalary = weeklySal; else weeklySalary = 0; } public void setWeeklySalary(double salary) { if(salary > 0) weeklySalary = salary; else weeklySalary = 0; } public double getWeeklySalary() { return weeklySalary; } public double getEarnings(double numberWeeks) { if(numberWeeks>0) earnings = weeklySalary*numberWeeks; else earnings = 0; return earnings; } public String toString() { return super.toString() + ". The weekly salary is " + getWeeklySalary(); } }
3. Create another java class (hiredEmployee.java) which is another special type of Employee with the additional attributes: wage and hours. Provide appropriate set and get methods along with toString() method. Also implement a getEarnings() mehod that reflects earning of HiredEmployee.
HiredEmployee.java:
public class HiredEmployee extends Employee { private double wage; private double hours; public HiredEmployee() { super(); wage = 0.0; hours = 0; } public HiredEmployee(double w, double h) { super(); if(w > 0 && h > 0) { wage = w; hours = h; } else { wage = 0; hours = 0; } } public void setWage(double nWage) { if(nWage > 0) wage = nWage; else wage = 0; } public double getWage() { return wage; } public void setHours(double nHours) { if(nHours > 0) hours = nHours; else hours = 0; } public double getHours() { return hours; } public double getEarnings() { earnings = wage * hours; return earnings; } public String toString() { return super.toString() + ". The employees wage is " + getWage() + ". The employees hours are " + getHours(); } }
4. Create a java class (CommissionEmployee.java) with additional attributes: grossSales and commissionRate. Provide get, set, and toString() mehtods. Provide a newer version of getEarnings() method that returns the earnings.
CommissionEmployee.java
public class CommissionEmployee extends Employee { protected double grossSales; protected double commissionRate; public CommissionEmployee() { super(); grossSales = 0.0; commissionRate = 0.0; } public CommissionEmployee(double gS, double cR) { super(); if(gS > 0 && cR > 0) { grossSales = gS; commissionRate = cR; } else { grossSales = 0; commissionRate = 0; } } public void setGrossSales(double nGS) { if(nGS > 0) grossSales = nGS; else grossSales = 0; } public double getGrossSales() { return grossSales; } public void setCommissionRate(double nCS) { if(nCS > 0) commissionRate = nCS; else commissionRate = 0; } public double getCommissionRate() { return commissionRate; } public String toString() { return super.toString() + ". The employees gross sales are " + getGrossSales() + ". The employees commission rate is " + getCommissionRate(); } public double getEarnings() { earnings = commissionRate * grossSales; return earnings; } }
5. Implement a specialized type of CommissionEmployee (BasePlusCommissionEmployee) which has an additional attribute: base salary. Proved get, set, and toString() methods. Implement the getEarnings() mehtod that returns the earnings.
BasePlusCommissionEmployee.java
public class BasePlusCommissionEmployee extends CommissionEmployee { private double baseSalary; public BasePlusCommissionEmployee() { super(); baseSalary = 0.0; } public BasePlusCommissionEmployee(double bS) { super(); if(bS > 0) baseSalary = bS; else baseSalary = 0; } public void setBaseSalary(double base) { if(base>0) baseSalary = base; else baseSalary = 0; } public double getBaseSalary() { return baseSalary; } public double getEarnings() { earnings = commissionRate * grossSales + baseSalary; return earnings; } public String toString() { return super.toString() + ". The employees base salary is " + getBaseSalary(); } }
Here's the part I am stuck on:
Write a client program with a static method called generateEmployees() that returns a random list of 10 different types of Employee objects. You could use an array or 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. I want to populate the each element of the array with an integer 1-4 each representing an Employee class. Then after determining which is which, the method will generate random data to put into the employee object that is created. As these items are generated, add them to the data structure(array that I'm using). The method should return this data.
In the same application class, I am to implement the main() method. Then call the generateEmployees() static method and using a for loop print the details of each of the employees along with their earning on the terminal window.
Here's what I have so far for my client program:
public class EmployeeTest { public static void generateEmployees() { int[] employed = new int[10]; for(int i=0; i<employed.length; i++) { employed[i] = (int)Math.random(); if(employed[i] < 1 || employed[i] > 4) employed[i] = (int)Math.random(); } System.out.println(employed[0]); System.out.println(employed[1]); System.out.println(employed[2]); System.out.println(employed[3]); System.out.println(employed[4]); System.out.println(employed[5]); System.out.println(employed[6]); System.out.println(employed[7]); System.out.println(employed[8]); System.out.println(employed[9]); } public static void main(String[] args) { } }