Exercises

  1. 20.3 (Generic Notation) Explain the use of the following notation in a C# program:

    public class Array<T>
  2. 20.4 (Overloading Generic Methods) How can generic methods be overloaded?

  3. 20.5 (Determining Which Method to Call) The compiler performs a matching process to determine which method to call when a method is invoked. Under what circumstances does an attempt to make a match result in a compile-time error?

  4. 20.6 (What Does This Statement Do?) Explain why a C# program might use the statement

    var workerlist = new Array<Employee>();
  5. 20.7 (Generic Linear Search Method) Write a generic method, Search, that searches an array using the linear-search algorithm. Method Search should compare the search key with each element in its array parameter until the search key is found or until the end of the array is reached. If the search key is found, return its location in the array; otherwise, return -1. Write a test app that inputs and searches an int array and a double array. Provide buttons that the user can click to randomly generate int and double values. Display the generated values in a TextBox, so the user knows what values they can search for [Hint: Use (T: IComparable<T>) in the where clause for method Search so that you can use method CompareTo to compare the search key to the elements in the array.]

  6. 20.8 (Overloading a Generic Method) Overload generic method DisplayArray of Fig. 20.3 so that it takes two additional int arguments: lowIndex and highIndex. A call to this method displays only the designated portion of the array. Validate lowIndex and highIndex. If either is out of range, or if highIndex is less than or equal to lowIndex, the overloaded DisplayArray method should throw an ArgumentException; otherwise, DisplayArray should return the number of elements displayed. Then modify Main to exercise both versions of DisplayArray on arrays intArray, doubleArray and charArray. Test all capabilities of both versions of DisplayArray.

  7. 20.9 (Overloading a Generic Method with a Non-Generic Method) Overload generic method DisplayArray of Fig. 20.3 with a nongeneric version that displays an array of strings in neat, tabular format, as shown in the sample output that follows:

    Array stringArray contains:
    one      two      three    four
    five     six      seven    eight
  8. 20.10 (Generic Method IsEqualTo) Write a simple generic version of method IsEqualTo that compares its two arguments with the Equals method, and returns true if they’re equal and false otherwise. Use this generic method in a program that calls IsEqualTo with a variety of simple types, such as object or int. What result do you get when you attempt to run this program?

  9. 20.11 (Generic Class Pair) Write a generic class Pair which has two type parameters, F and S, representing the type of the first and second element of the pair, respectively. Add properties for the first and second elements of the pair. [Hint: The class header should be public class Pair<F, S>.]

  10. 20.12 (Generic Classes TreeNode and Tree) Convert classes TreeNode and Tree from Fig. 19.20 into generic classes. To insert an object in a Tree, the object must be compared to the objects in existing TreeNodes. For this reason, classes TreeNode and Tree should specify IComparable<T> as the interface constraint of each class’s type parameter. After modifying classes TreeNode and Tree, write a test app that creates three Tree objects—one that stores ints, one that stores doubles and one that stores strings. Insert 10 values into each tree. Then output the preorder, inorder and postorder traversals for each Tree.

  11. 20.13 (Generic Method TestTree) Modify your test program from Exercise 20.12 to use generic method TestTree to test the three Tree objects. The method should be called three times—once for each Tree object.

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

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