CSc 221 Spring 2006 Homework 4

Due before the start of class Tuesday, March 28.

You are to do Exercise 10.12  in the sixth edition of the text. For your convenience, here it  is:

10.12 (Accounts Payable System Modification) In this exercise we modify the accounts payable application of Fig 10.12-Fig 10.15 to include the complete functionality of the payroll application of Fig 10.4-Fig 10.9. The application should still process two Invoice objects, but now should process one object of each of the four Employee subclasses. If the object currently being processed is a BasePlusCommissionEmployee, the application should increase the BasePlusCommissionEmployee’s base salary by 10%. Finally, the application should output the payment amount for each object. Complee the following steps to create the new application:

a. Modify classes HourlyEmployee (Fig 10.6) and CommissionEmployee (Fig 10.7) to place them in the Payable hierarchy  [[[SEE NOTE AT END]]] as subclasses of the version of Employee (Fig 10.13) that implements Payable. [Hint: Change the name of the method earnings to getPaymentAmount in each subclass so that the class satisfies the inherited contract with the interface Payable.]

b. Modify class BasePlusCommissionEmployee (Fig 10.8) so that it extends the version of CommissionEmployee created in Part a.

c. Modify PayableInterfaceTest (Fig 10.15) to polymorphically process two Invoices, one SalariedEmployee, one HourlyEmployee, one CommissionEmployee, and one BasePlusCommissionEmployee. First output a string representation of each Payable object. Next, if an object is a BasePlusCommissionEmployee, increase its base salary by 10%. Finally, output the payment amount for each Payable object.

Once you fully understand how the programs in the text work, this homework can be done in an hour, maybe. But that begs the question: how do you get to understand the programs in the text? Answer, by doing the homework! Seriously: copy all the files into a package called hw4 or whatever, and start making the modifications. You will get a lot of error messages, but for the most part the error messages are helpful. By fighting through the problems you will come to understand how inheritance, interfaces, and polymorphism work--at least in this example. And you will get the homework done.

One hint: you will have to modify the overrides of toString in several cases, to account for additional information. That's one of several goals in having you do this homework: to understand toString, and to understanding overriding a method. The main goals, of course, are to introduce you to polymorphism and interfaces, and to give you a deeper understanding of inheritance.

On polymorphism: Do whatever it  takes, to convince yourself that the compiler does not know, at compile time, what kind of object each element of the array payableObjects is.

Don't start of this the night before it is due. You won't stand a chance. Get going on it soon, so you can ask questions in class.

 

NOTE: The talk above about a "Payable hierarchy" is at least misleading. Payable is an interface, and we implement interfaces, we don't inherit from them. This assignment involves both extending the Employee class, and implementing the Payable interface. The payable interface will contain just one abstract method, . All classes that implement the Payable interface are required to implement the abstract method  getPaymentAmount, each in the way that is appropriate for that class of employee.

This will make sense soon enough, but until it does, just memorize: "We extend a superclass; we implement an interface."

Back to 221 home page.

Back to Dan McCracken's  Home Page