Wrap-Up

Answers to Self Review Exercises

11.1

a) abstract. b) concrete. c) Polymorphism. d) abstract. e) downcasting.

11.2

a) True. b) False. An abstract class can include methods with implementations and abstract methods. c) True. d) False. Only a concrete derived class must implement the method. e) True.

Exercises

11.3

How does polymorphism enable you to program "in the general" rather than "in the specific"? Discuss the key advantages of programming "in the general."

11.4

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?

11.5

What are abstract methods? Describe the circumstances in which an abstract method would be appropriate.

11.6

How does polymorphism promote extensibility?

11.7

Discuss four ways in which you can assign base class and derived class references to variables of base class and derived class types.

11.8

Compare and contrast abstract classes and interfaces. Why would you use an abstract class? Why would you use an interface?

11.9

(Payroll System Modification) Modify the payroll system of Figs. 11.411.9 to include private instance variable birthDate in class Employee. Use class Date of Fig. 9.9 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.

11.10

(Shape Hierarchy) Implement the Shape hierarchy shown in Fig. 10.3. You may 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 application that uses an array of Shape references to objects of each concrete class in the hierarchy. The application should print 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.

11.11

(Payroll System Modification) Modify the payroll system of Figs. 11.411.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.

11.12

(Accounts Payable System Modification) In this exercise, we modify the accounts payable application of Figs. 11.1111.15 to include the complete functionality of the payroll application of Figs. 11.411.9. The application 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 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:

  1. Modify classes HourlyEmployee (Fig. 11.6) and CommissionEmployee (Fig. 11.7) to place them in the IPayable hierarchy as derived classes of the version of Employee (Fig. 11.13) that implements IPayable. [Hint: Change the name of method Earnings to GetPaymentAmount in each derived class.]
     
  2. Modify class BasePlusCommissionEmployee (Fig. 11.8) such that it extends the version of class CommissionEmployee created in Part a.
     
  3. Modify PayableInterfaceTest (Fig. 11.15) 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.13

(Package Inheritance Hierarchy) Use the Package inheritance hierarchy created in Exercise 10.9 to create an application that displays the address information and calculates the shipping costs for several Packages. The application should contain an array of Package objects of classes TwoDayPackage and OvernightPackage. Loop through the array to process the Packages polymorphically. For each Package, use properties to obtain the address information of the sender and the recipient, then print the two addresses as they would appear on mailing labels. Also, call each Package's CalculateCost method and print the result. Keep track of the total shipping cost for all Packages in the array, and display this total when the loop terminates.

11.14

(Polymorphic Banking Program Using Account Hierarchy) Develop a polymorphic banking application using the Account hierarchy created in Exercise 10.10. 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, print the updated account balance obtained by using base class property Balance.

Exception Handling

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net