Should be in Java Interface Worker should extend Serializabl
Should be in Java
Interface Worker should extend Serializable from java.io.*
In addition to shown data fields, each Worker object should have a name field, a workerID field, and a Department field.The classes and/or interfaces where these fields belong are not indicated, so you need to decide where they best belong in order to reduce code redundancy as much as possible; it may not be possible to put them in only one place.
Write get and set methods for every data field. You may use the automatic generator of your IDE, but all data should be validated. I should not be able to set invalid data such a negative hours. The program should not terminate when receiving bad data but should recover, whether input is coming from a file or interactively.
All appropriate classes must override the equals method so that it compares workers by workerID; two workers should be considered equal if they have the same workerID, even if they are not the same kind of Worker; when comparing a WorkerObject to an object of another type, the equals method should return false.
Class HourlyWorker should be an abstract class because it contains an abstract toString method that will be implemented in the subclasses; in the subclasses, it should return the type of subclass along with all data field values in a user-friendly understandable format.
Data field hours in the HourlyWorker class represents the number of hours worked so far during the current month
Decide if any of the methods requires a precondition; if so, they should throw an appropriate exception when the precondition is not met
Supply appropriate constructors for all concrete and abstract classes. Each should have a no-arg constructor and a fully qualified constructor.
Create an application class named WorkerTest to demonstrate the hierarchy.
The application should create Volunteer, HourlyEmployee, and SalariedEmployee objects with initial field values specified interactively. Invalid data should produce a message and a re-prompt.
The application should then create a series of objects by reading initial field values from a file. The file format is up to you. The input should test both the no-arg constructors and the fully qualified constructors. If the file contains invalid data, the program send a message to err.out, ignore the invalid data and proceed to the next valid data. The file input data should test all caught exceptions. I should not be able to crash your program, regardless of the input data I provide.
Check each object against abstract class HourlyWorker and each interface by using the instanceof operator. Output results indicating whether each object is or is not of the checked type.
Create an ArrayList containing the objects and polymorphically traverse the array, writing the output of each objects\' toString() method to a file.
I know I need to do a department class and also an employee class.
Here is the code i have so far.
public abstract class Worker
{
private final String lastName;
private final String workerID;
private final String Department;
//contrructor
public Worker(String lastName, String workerID, String Department)
{
this.lastName = lastName;
this.workerID = workerID;
this.Department = Department;
}
public String getLastName()
{
return lastName;
}
public String getWorkerID()
{
return workerID;
}
public String getDepartment()
{
return Department;
}
//return String of Worker object
@Override
public String toString()
{
return String.format(\"%s %s%nID worker ID: %s\", getLastName(), getWorkerID(), getDepartment());
}
public abstract double earnings();
}
// worker class
public class HourlyEmpoyee extends Worker
{
private double wage;
private double hours;
public HourlyEmpoyee(String lastName, String workerID, String Department, double wage, double hours)
{
super(lastName, workerID, Department);
if (wage < 0.0)
throw new IllegalArgumentException(\"Hours worked must be >= 0.0\");
if ((hours <0.0) || (hours > 168.0))
throw new IllegalArgumentException(\"Hours worked must be >= 0.0 and <=168.0\");
this.wage = wage;
this.hours = hours;
}
public void setWage(double wage)
{
if(wage <0.0)
throw new IllegalArgumentException(\"Hourly wage must be >= 0.0\");
this.wage = wage;
}
public double getWage()
{
return wage;
}
public void setHours(double hours)
{
if((hours <0.0) || (hours > 168.0))
throw new IllegalArgumentException(\"Hours worked must be >= 0.0 and <= 168.0\");
this.hours = hours;
}
public double getHours()
{
return hours;
}
@Override
public double earnings()
{
if(getHours() <=40)
return getWage() * getHours();
else
return 40 * getWage() + (getHours() - 40) * getWage() * 1.5;
}
@Override
public String toString()
{
return String.format(\"hourly employee: %s%n%s: $%,.2f; %s: %,.2f\", super.toString(), \"hourly wage\", getWage(), \"hours worked\", getHours());
}
}
public class SalariedEmployee extends Worker
{
private double weeklySalary;
public SalariedEmployee(String lastName, String workerID, String Department, double weeklySalary)
{
super(workerID, lastName, Department);
if (weeklySalary <0.0)
throw new IllegalArgumentException(\"Weekly salary must be >= 0.0\");
this.weeklySalary = weeklySalary;
}
public void setWeeklySalary(double weeklySalary)
{
if(weeklySalary <0.0)
throw new IllegalArgumentException(\"Weekly salary must be >= 0.0\");
this.weeklySalary = weeklySalary;
}
public double getWeeklySalary()
{
return weeklySalary;
}
@Override
public double earnings()
{
return getWeeklySalary();
}
@Override
public String toString()
{
return String.format(\"salaried employee: %s%n%s: $%, .2f\", super.toString(),\"weekly salary\", getWeeklySalary());
}
}public abstract class Volunteer extends Worker// extends hourly worker due to hours worked
{
public Volunteer(String lastName, String workerID, String Department)
{
super(lastName, workerID, Department);
}
public double pay()
{
return 0;
}
}public class WorkerTest
{
public static void main(String[] args)// add a input scanner and a file reader
{
SalariedEmployee salariedEmployee =
new SalariedEmployee(\"Burger\", \"ID001\", \"Sales\" 777.00);
HourlyEmployee hourlyEmployee =
new HourlyEmployee (\"Smith\", \"ID002\", \"Claims\" 14.44, 40);
Volunteer volunteer =
new Volunteer(\"Johnson\", \"ID003\", \"Help\" );
System.out.println(\"Workers processed individually:\");
System.out.printf(\"%n%s%n%s: $%,.2f%n%n\", salariedEmployee, \"earned\" salariedEmployee.earning());
System.out.printf(\"%s%n%s: $%,.2f%n%n\", hourlyEmployee, \"earned\" hourlyEmployee.earnings());
System.out.println(\"Volunteer earnings are: 0 \");
Worker[] employees = new Worker[3];
employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
employees[2] = volunteer;
System.out.printf(\"Employees processed polymorphically: %n%n\");
for(Worker currentEmployee : employees)
{
System.out.println(currentEmployee);
System.out.printf(\"earned $%,.2f%n%n\", currentEmployee.earnings());
}
for (int j = 0; j <employees.length; j++)
System.out.printf(\"Employee %d is a %s%n\", j, employees[j].getClass().getName());
}
}
Solution
Hi Friend, I have fixed all issue and modified classes according to requirement.
There is no need of Department and Employee class.
Please let me know in case of any issue.
################# Worker.java ##############
public abstract class Worker
{
private final String lastName;
private final String workerID;
private final String Department;
//contrructor
public Worker(String lastName, String workerID, String Department)
{
this.lastName = lastName;
this.workerID = workerID;
this.Department = Department;
}
public String getLastName()
{
return lastName;
}
public String getWorkerID()
{
return workerID;
}
public String getDepartment()
{
return Department;
}
//return String of Worker object
@Override
public String toString()
{
return String.format(\"%s %s%nID worker ID: %s\", getLastName(), getWorkerID(), getDepartment());
}
public abstract double earnings();
}
################## HourlyEmploye.java ##########
public class HourlyEmploye extends Worker
{
private double wage;
private double hours;
public HourlyEmploye(String lastName, String workerID, String Department, double wage, double hours)
{
super(lastName, workerID, Department);
if (wage < 0.0)
throw new IllegalArgumentException(\"Hours worked must be >= 0.0\");
if ((hours <0.0) || (hours > 168.0))
throw new IllegalArgumentException(\"Hours worked must be >= 0.0 and <=168.0\");
this.wage = wage;
this.hours = hours;
}
public void setWage(double wage)
{
if(wage <0.0)
throw new IllegalArgumentException(\"Hourly wage must be >= 0.0\");
this.wage = wage;
}
public double getWage()
{
return wage;
}
public void setHours(double hours)
{
if((hours <0.0) || (hours > 168.0))
throw new IllegalArgumentException(\"Hours worked must be >= 0.0 and <= 168.0\");
this.hours = hours;
}
public double getHours()
{
return hours;
}
@Override
public double earnings()
{
if(getHours() <=40)
return getWage() * getHours();
else
return 40 * getWage() + (getHours() - 40) * getWage() * 1.5;
}
@Override
public String toString()
{
return String.format(\"hourly employee: %s%n%s: $%,.2f; %s: %,.2f\", super.toString(), \"hourly wage\", getWage(), \"hours worked\", getHours());
}
}
###################### SalariedEmployee.java #############
public class SalariedEmployee extends Worker
{
private double weeklySalary;
public SalariedEmployee(String lastName, String workerID, String Department, double weeklySalary)
{
super(workerID, lastName, Department);
if (weeklySalary <0.0)
throw new IllegalArgumentException(\"Weekly salary must be >= 0.0\");
this.weeklySalary = weeklySalary;
}
public void setWeeklySalary(double weeklySalary)
{
if(weeklySalary <0.0)
throw new IllegalArgumentException(\"Weekly salary must be >= 0.0\");
this.weeklySalary = weeklySalary;
}
public double getWeeklySalary()
{
return weeklySalary;
}
@Override
public double earnings()
{
return getWeeklySalary();
}
@Override
public String toString()
{
return String.format(\"salaried employee: %s%n%s: $%, .2f\", super.toString(),\"weekly salary\", getWeeklySalary());
}
}
###################### Volunteer.java ##############
public class Volunteer extends Worker// extends hourly worker due to hours worked
{
public Volunteer(String lastName, String workerID, String Department)
{
super(lastName, workerID, Department);
}
@Override
public double earnings() {
return 0;
}
@Override
public String toString() {
return super.toString();
}
}
################# WorkerTest.java ##############
import java.util.ArrayList;
public class WorkerTest
{
public static void main(String[] args)// add a input scanner and a file reader
{
// creating objects
SalariedEmployee salariedEmployee =
new SalariedEmployee(\"Burger\", \"ID001\", \"Sales\", 777.00);
HourlyEmploye hourlyEmployee =
new HourlyEmploye (\"Smith\", \"ID002\", \"Claims\", 14.44, 40);
Volunteer volunteer =
new Volunteer(\"Johnson\", \"ID003\", \"Help\" );
ArrayList<Worker> employees = new ArrayList<>();
// adding in array list
employees.add(salariedEmployee);
employees.add(hourlyEmployee);
employees.add(volunteer);
System.out.printf(\"Employees processed polymorphically: %n%n\");
for(Worker currentEmployee : employees)
{
System.out.println(currentEmployee);
System.out.printf(\"earned $%,.2f%n%n\", currentEmployee.earnings());
}
}
}
/*
Sample run:
Employees processed polymorphically:
salaried employee: ID001 Burger
ID worker ID: Sales
weekly salary: $ 777.00
earned $777.00
hourly employee: Smith ID002
ID worker ID: Claims
hourly wage: $14.44; hours worked: 40.00
earned $577.60
Johnson ID003
ID worker ID: Help
earned $0.00
*/








