Write an abstract class Worker , two concrete subclasses HourlyWorker and SalariedWorker , an interface HaveFun , and a test program TestWorker . Each of these classes is described below.
The abstract Worker class has a name field and a salary rate (in dollars per hour) field. The Worker class has an abstract instance method computeWeeklyPay(int hours) (each subclass of Worker will have its own way of computing weekly pay). The Worker class should have appropriate constructors, appropriate (concrete) set and get methods for the name and salary fields, and it should also have a (concrete) toString method that returns the name and salary rate in an appropriate string.
The HourlyWorker class has a boolean instance field called isUnionMemeber and a String instance field called unionName (which should be set to null if isUnionMemeber is false ). The HourlyWorker class has a computeWeeklyPay(int hours) method that implements the same method from the super class (an hourly worker gets paid for the actual number of hours worked, with overtime over 40 hours paid at time and a half). The HourlyWorker class should have appropriate constructors (that make use of the super class constructors), appropriate set and get methods, and it should also have a toString method that both overrides and calls the toString method in the super class, and that returns the name, salary rate, and union status in an appropriate string.
The SalariedWorker class has an int instance field called subordinates that records the number of workers that the salaried worker manages. The SalariedWorker class has a computeWeeklyPay(int hours) method that implements the same method from the super class (a salaried worker always gets paid for 40 hours, no matter what the actual hours worked is). The SalariedWorker class should have appropriate constructors (that make use of the super class constructors), appropriate set and get methods, and it should also have a toString method that both overrides and calls the toString method in the super class, and that returns the name, salary rate, and number of subordinates in an appropriate string.
The SalariedWorker and HourlyWorker classes should both implement the HaveFun interface. The HaveFun interface should declare two methods, String haveFunInTheMorning() and String haveFunInTheEvening() . You can decide for yourself how hourly and salaried workers have fun (but notice that they do it by returning a string). (The reason that having fun is not in the public interface of the Worker class or its subclasses is that other classes besides workers should know how to have fun. For example a Student or a RetiredPerson object should know how to have fun, but they need not be Worker objects. By having a HaveFun interface, we have a uniform way of handling all objects that should know how to have fun.)
Write a test class called TestWorker . It should create an array of type Worker and fill it with some HourlyWorker and some SalariedWorker objects. Then the program should loop through the array and use polymorphism to compute the pay and print out all of the information for each object in the array. The test class should also show that your constructor, get, and set methods all work. The test class should also create an array of fun loving workers (of various kinds) and test the HaveFun interface.
When you are done you will have five program files, Worker.java , HourlyWorker.java , SalariedWorker.java , HaveFun , and TestWorker.java . Put these four files together into one zip file.
This assignment is due Wednesday, December 1.
|