Programming Exercises

   


1:

Write a simple Account class containing an instance variable called balance and a property called Balance to provide access to balance. Allow the Account class to be sorted by the generic BubbleSortAscending contained in the Sorter class of Listing 17.13 by letting it implement the IComparable interface. Test your Account class by creating an array of Accounts with different balances and pass this as an argument to the BubbleSortAscending method.

2:

Write three classes called Secretary, Director, and Programmer. Each of the classes contains a CalculateSalary method. For simplicity, let each of these methods write "Now calculating the salary for…" followed by the name of the class in which the method resides.

Suppose there are many objects of these classes in your program, and that they need to be stored in a collection, such as an array. How can you store them all in one array and also call the CalculateSalary of each object simply by iterating through the array and making the same method call for all objects? Write the code that implements this scenario.

3:

Extend the code of question 2 by implementing the following separate hierarchy: A base class named Building containing two instance variables called age of type int and currentValue of type decimal. Derive two classes from Building called House and OfficeBuilding. House contains an instance variable called numberOfBedrooms of type ushort and OfficeBuilding an instance variable called floorSpace of type uint.

You want to write and read objects of type Secretary and House to a file. So both of these classes must contain methods called Read and Write that (for simplicity make the Read methods write "Now reading House" and "Now reading Secretary" on the console and similarly for the Write methods) save these objects to and from a file. You want to construct just one method that accepts any object containing the Read and Write methods and will call these methods polymorphically, regardless of which type the object is. Write the code that implements this functionality.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

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