Вы находитесь на странице: 1из 2

Lab 10: Polymorphism (cont.

)
JH2P, Ch.10 Exercises

10.11

(Accounts Payable System Modification) In this exercise, we modify the accounts payable
application of Fig. 10.11, Fig. 10.15 to include the complete functionality of the payroll
application of Fig. 10.4,Fig. 10.9 (in Lab 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. Complete 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 as subclasses of the version of Employee (Fig. 10.13)
that implements Payable. [Hint: Change the name of method earnings to
getPaymentAmount in each subclass so that the class satisfies its inherited contract with
interface Payable.]
b. Modify class BasePlusCommissionEmployee (Fig. 10.8) such that it extends the version
of class 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.

// Fig. 10.11: Payable.java


// Payable interface declaration.

public interface Payable


{
double getPaymentAmount(); // calculate payment; no implementation
} // end interface Payable

/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/

// Fig. 10.15: PayableInterfaceTest.java


// Tests interface Payable.

public class PayableInterfaceTest


{
public static void main( String args[] )
{
// create four-element Payable array
Payable payableObjects[] = new Payable[ 4 ];

// populate array with objects that implement Payable


payableObjects[ 0 ] = new Invoice( "01234", "seat", 2, 375.00 );
payableObjects[ 1 ] = new Invoice( "56789", "tire", 4, 79.95 );
payableObjects[ 2 ] =
new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
payableObjects[ 3 ] =
new SalariedEmployee( "Lisa", "Barnes", "888-88-8888", 1200.00 );

System.out.println(
"Invoices and Employees processed polymorphically:\n" );

// generically process each element in array payableObjects


for ( Payable currentPayable : payableObjects )
{
// output currentPayable and its appropriate payment amount
System.out.printf( "%s \n%s: $%,.2f\n\n",
currentPayable.toString(),
"payment due", currentPayable.getPaymentAmount() );
} // end for
} // end main
} // end class PayableInterfaceTest

/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/

Вам также может понравиться