Lab 10 97
Short Description
Download Lab 10 97...
Description
Lab 10: Polymorphism (cont.) JH2P, Ch.10 Exercises
10.11 Accounts Payable System Modification) Modification) In this exercise, we modify the accounts payable ( Accounts application of Fig. of Fig. 10.11, 10.11, Fig. 10.15 to include the complete functionality of the payroll application of Fig. of Fig. 10.4, 10.4,Fig. 10.9 (in Lab 9). 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.
b. c.
Modify classes HourlyEmployee (Fig. (Fig. 10.6) 10.6) and CommissionEmployee (Fig. (Fig. 10.7) 10.7) to place them in the Payable hierarchy as subclasses of the version of Employee (Fig. (Fig. 10.13) 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.] Modify class BasePlusCommissionEmployee (Fig. (Fig. 10.8) 10.8) such that it extends the version of class CommissionEmployee created in Part a. Modify PayableInterfaceTest (Fig. (Fig. 10.15) 10.15) to polymorphically process two Invoices, one SalariedEmployee, one HourlyEmployee, one CommissionEmployee and one BasePlusCommissionEmployee. First output a string representation of eac h 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. * *************************************************************************/
View more...
Comments