Assignment #3: Inheritance and Exceptions

This assignment will ask you to write several classes that make use of inheritance.

What to Do

First, go back to the Date class and add robustness by defining an IllegalDateException as a checked exception, and throwing it in routines that attempt to set dates.

Define an abstract base class called Employee that contains a name (String), a social security number (String), a date of hire (Date), and the respective accessor functions. It contains a toString whose task is to output the social security number, name, and date of hire (in that order), and it also contains a compareTo function that orders employees by social security number (returning the same values as the String's compareTo) and an equals method (two employees are equal if they have the same social security number, regardless of type). You should not use protected members. Include a three-parameter constructor. The Employee class should also declare an abstract salary accessor and mutator.

Next, derive a class called HourlyEmployee that adds a new data member to store an hourly wage (double). Its toString function must return a String containing the social security number, name, date of hire, and salary (with phrase "per hour"). It will certainly want to call the base class toString. Provide an accessor and mutator for the salary, and make sure that its constructor initializes a salary (how many parameters will the constructor need?).

Next, derive another class called SalariedEmployee that adds a new data member to store a yearly salary (double). Its toString function must return a String with the social security number, name, date of hire, and salary (with phrase "annual"). Provide an accessor and mutator for the salary, and make sure that its constructor initializes a salary.

In order to hold all employees, create a class called Roster that is able to hold a variable number of Employee references. Provide the capability to add an employee and give a String representing the entire roster of employees. Roster should have an array of Employee as its data member, and keep track of the number of employees. The array should start with an initial size of 5, and can be expanded by doubling the size if calls to add force it to exceed capacity. You may not insert a duplicate social security number; perform this test at the start of add (by calling a private method of Roster). The test should use compareTo to determine if two Employee references are equal; they are equal if compareTo returns 0. You should not use equals to perform this test.

To summarize, Roster has public methods add and toString. The initial version of toString should not use repeated concatenations. You should use a StringBuffer instead, for efficiency.

You should then be able to write a program that includes the following main:

public static void main( String [] args )
{
    Roster r = new Roster( );

    r.add( new HourlyEmployee( "Jim", "400-00-0000", new Date( "Nov", 1, 1992 ), 15.60 ) );
    r.add( new SalariedEmployee( "Alex", "500-00-0000", new Date( "Oct", 1, 1994 ), 81090 ) );
    r.add( new HourlyEmployee( "Mark", "600-00-0000", new Date( "Jul", 1, 1983 ), 21.75 ) );
    r.add( new HourlyEmployee( "Bill", "100-00-0000", new Date( "Jan", 1, 1990 ), 10.90 ) );
    r.add( new SalariedEmployee( "Bob", "200-00-0000", new Date( "Jun", 1, 1993 ), 61090 ) );
    r.add( new SalariedEmployee( "Joe", "300-00-0000", new Date( "May", 1, 1996 ), 31690 ) );
    r.add( new SalariedEmployee( "Tim", "700-00-0000", new Date( "Jun", 1, 1973 ), 111577 ) );
    r.add( new HourlyEmployee( "Jane", "700-00-0000", new Date( "Jun", 1, 1973 ), 111577 ) ); // oops
 
    System.out.println( r );
}

You should also be able to attempt to add an employee with a bad date and catch the exception in main.