Exercises

  1. 12.3 (Programming in the General) How does polymorphism enable you to program “in the general” rather than “in the specific”? Discuss the key advantages of programming “in the general.”

  2. 12.4 (Inheriting Interface vs. Inheriting Implementation) A derived class can inherit “interface” or “implementation” from a base class. How do inheritance hierarchies designed for inheriting interface differ from those designed for inheriting implementation?

  3. 12.5 (Abstract Methods) What are abstract methods? Describe the circumstances in which an abstract method would be appropriate.

  4. 12.6 (Polymorphism and Extensibility) How does polymorphism promote extensibility?

  5. 12.7 (Assigning Base-Class and Derived-Class References) Discuss four ways in which you can assign base-class and derived-class references to variables of base-class and derived-class types.

  6. 12.8 (Abstract Classes vs. Interfaces) Compare and contrast abstract classes and interfaces. Why would you use an abstract class? Why would you use an interface?

  7. 12.9 (Payroll System Modification) Modify the payroll system of Figs. 12.412.9 to include private instance variable birthDate in class Employee. Use class Date of Fig. 10.7 to represent an employee’s birthday. Assume that payroll is processed once per month. Create an array of Employee variables to store references to the various employee objects. In a loop, calculate the payroll for each Employee (polymorphically), and add a $100.00 bonus to the person’s payroll amount if the current month is the month in which the Employee’s birthday occurs.

  8. 12.10 (Shape Hierarchy) Implement the Shape hierarchy of Fig. 11.3. Omit the Triangle and Tetrahedron classes. Each TwoDimensionalShape should contain read-only abstract property Area to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have read-only abstract properties Area and Volume to calculate the surface area and volume, respectively, of the three-dimensional shape. Create an app that uses an array of Shape references to objects of each concrete class in the hierarchy. Display a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If a shape is a TwoDimensionalShape, display its area. If a shape is a ThreeDimensionalShape, display its area and volume.

  9. 12.11 (Payroll System Modification) Modify the payroll system of Figs. 12.4–12.9 to include an additional Employee derived class, PieceWorker, that represents an employee whose pay is based on the number of pieces of merchandise produced. Class PieceWorker should contain private instance variables wage (to store the employee’s wage per piece) and pieces (to store the number of pieces produced). Provide a concrete implementation of method Earnings in class PieceWorker that calculates the employee’s earnings by multiplying the number of pieces produced by the wage per piece. Create an array of Employee variables to store references to objects of each concrete class in the new Employee hierarchy. Display each Employee’s string representation and earnings.

  10. 12.12 (Accounts Payable System Modification) Modify the accounts payable app of Figs. 12.11–12.14 to include the complete functionality of the payroll app of Figs. 12.4–12.9. The app should still process two Invoice objects, but now should process one object of each of the four Employee derived classes. If the object currently being processed is a BasePlusCommissionEmployee, the app should increase the BasePlusCommissionEmployee’s base salary by 10%. Finally, the app should output the payment amount for each object. Modify PayableInterfaceTest (Fig. 12.14) to polymorphically process two Invoices, one SalariedEmployee, one HourlyEmployee, one CommissionEmployee and one BasePlusCommissionEmployee. First, output a string representation of each IPayable object. Next, if an object is a BasePlusCommissionEmployee, increase its base salary by 10%. Finally, output the payment amount for each IPayable object.

  11. 12.13 (Polymorphic Banking Program Using Account Hierarchy) Develop a polymorphic banking app using the Account hierarchy created in Exercise 11.9. Create an array of Account references to SavingsAccount and CheckingAccount objects. For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit. As you process each Account, determine its type. If an Account is a SavingsAccount, calculate the amount of interest owed to the Account using method CalculateInterest, then add the interest to the account balance using method Credit. After processing an Account, display the updated account balance obtained by using base-class property Balance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset