Wrap-Up

Answers to Self Review Exercises

27.1

a) enumerator (or foreach statement). b) ArrayList. c) indexer. d) double. e) Synchronized. f) MoveNext. g) InvalidOperationException.

27.2

a) True. b) False. The class must also implement property Current. c) True. d) False. An ArrayList stores only objects. Autoboxing occurs when adding a value type to the ArrayList. You can prevent boxing by instead using generic class List with a value type. e) True. f) False. A Hashtable cannot contain duplicate keys. g) True. h) False. Dictionary is a class; IDictionary is an interface. i) False. An enumerator cannot be used to change the values of elements. j) False. With hashing, as the load factor increases, there are fewer available slots relative to the total number of slots, so the chance of selecting an occupied slot (a collision) with a hashing operation increases.

Exercises

27.3

Define each of the following terms:

  1. ICollection
  2. Array
  3. IList
  4. load factor
  5. collision
  6. spacetime trade-off in hashing
  7. Hashtable
27.4

Explain briefly the operation of each of the following methods of class ArrayList:

  1. Add
  2. Insert
  3. Remove
  4. Clear
  5. RemoveAt
  6. Contains
  7. IndexOf
  8. Count
  9. Capacity
27.5

Explain why inserting additional elements into an ArrayList object whose current size is less than its capacity is a relatively fast operation and why inserting additional elements into an ArrayList object whose current size is at capacity is a relatively slow operation.

27.6

In our implementation of a stack in Fig. 25.13, we were able to quickly extend a linked list to create class StackInheritance. The .NET Framework designers chose not to use inheritance to create their Stack class. What are the negative aspects of inheritance, particularly for class Stack?

27.7

Briefly answer the following questions:

  1. What happens when you add a simple type (e.g., double) value to a non-generic collection?
  2. Can you print all the elements in an IEnumerable object without explicitly using an enumerator? If yes, how?
27.8

Explain briefly the operation of each of the following enumerator-related methods:

  1. GetEnumerator
  2. Current
  3. MoveNext
27.9

Explain briefly the operation of each of the following methods and properties of class Hashtable:

  1. Add
  2. Keys
  3. Values
  4. ContainsKey
27.10

Determine whether each of the following statements is true or false. If false, explain why.

  1. Elements in an array must be sorted in ascending order before a BinarySearch may be performed.
  2. Method First gets the first node in a LinkedList.
  3. Class Array provides static method Sort for sorting array elements.
27.11

Write an application that reads in a series of first names and stores them in a LinkedList. Do not store duplicate names. Allow the user to search for a first name.

27.12

Modify the application in Fig. 27.8 to count the number of occurrences of each letter rather than of each word. For example, the string "HELLO THERE" contains two Hs, three Es, two Ls, one O, one T and one R. Display the results.

27.13

Use a SortedDictionary to create a reusable class for choosing from some of the predefined colors in class Color (in the System.Drawing namespace). The names of the colors should be used as keys, and the predefined Color objects should be used as values. Place this class in a class library that can be referenced from any C# application. Use your new class in a Windows application that allows the user to select a color and then changes the background color of the Form.

27.14

Write an application that determines and prints the number of duplicate words in a sentence. Treat uppercase and lowercase letters the same. Ignore punctuation.

27.15

Recall from Fig. 27.2 that class List is the generic equivalent of class ArrayList. Write an application that inserts 25 random integers from 0 to 100 in order into an object of class List. The application should calculate the sum of the elements and the floating-point average of the elements.

27.16

Write an application that creates a LinkedList object of 10 characters, then creates a second list object containing a copy of the first list, but in reverse order.

27.17

Write an application that takes a whole-number input from a user and determines whether it is prime. If the number is not prime, display the unique prime factors of the number. Remember that a prime number's factors are only 1 and the prime number itself. Every number that is not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied together, the result is 54. For the number 54, the prime factors output should be 2 and 3. Use generic SortedDictionarys as part of your solution by recording the factors as the keys and using the Keys property to enumerate the factors.

27.18

In Exercise 24.7, you performed a bucket sort of ints by using a two-dimensional array, where each row of the array represented a bucket. By instead using a dynamically expanding data structure to represent each bucket, you do not have to write code that keeps track of the number of ints in each bucket. Rewrite your solution to use a one-dimensional array of LinkedList< int > buckets.

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