Wrap-Up

Answers to Self Review Exercises

18.1

a) True. b) True. c) False. Class StreamReader inherits from class TexTReader. d) False. Only classes with the Serializable attribute can be serialized. e) False. It seeks relative to the SeekOrigin enumeration member that is passed as one of the arguments. f) True. g) True.

18.2

a) 0s, 1s. b) bit. c) file. d) characters. e) database. f) ReadLine. g) WriteLine. h) Stream, object. i) System.IO. j) System.Runtime.Serialization.Formatters.Binary.

Exercises

18.3

Create a program that stores student grades in a text file. The file should contain the name, ID number, class taken and grade of every student. Allow the user to load a grade file and display its contents in a read-only TextBox. The entries should be displayed in the following format:

LastName, FirstName: ID# Class Grade  

We list some sample data below:

Jones, Bob: 1 "Introduction to Computer Science" "A-" Johnson, Sarah: 2 "Data Structures" "B+" Smith, Sam: 3 "Data Structures" "C"

18.4

Modify the previous program to use objects of a class that can be serialized to and deserialized from a file.

18.5

Extend classes StreamReader and StreamWriter. Make the class that derives from StreamReader have methods ReadInteger, ReadBoolean and ReadString. Make the class that derives from StreamWriter have methods WriteInteger, WriteBoolean and WriteString. Think about how to design the writing methods so that the reading methods will be able to read what was written. Design WriteInteger and WriteBoolean to write strings of uniform size so that ReadInteger and ReadBoolean can read those values accurately. Make sure ReadString and WriteString use the same character(s) to separate strings.

18.6

Create a program that combines the ideas of Fig. 18.9 and Fig. 18.11 to allow a user to write records to and read records from a file. Add an extra field of type bool to the record to indicate whether the account has overdraft protection.

18.7

(Telephone-Number Word Generator) Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have three letters associated with them (Fig. 18.16). Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indicated in Fig. 18.16 to develop the seven-letter word "NUMBERS." Every seven-letter word corresponds to exactly one seven-digit telephone number. A restaurant wishing to increase its takeout business could surely do so with the number 825-3688 (i.e., "TAKEOUT").

Every seven-letter phone number corresponds to many different seven-letter words. Unfortunately, most of these words represent unrecognizable juxtapositions of letters. It is possible, however, that the owner of a barbershop would be pleased to know that the shop's telephone number, 424-7288, corresponds to "HAIRCUT." The owner of a liquor store would no doubt be delighted to find that the store's number, 233-7226, corresponds to "BEERCAN." A veterinarian with the phone number 738-2273 would be pleased to know that the number corresponds to the letters "PETCARE." An automotive dealership would be pleased to know that its phone number, 639-2277, corresponds to "NEWCARS."

Figure 18.16. Letters that correspond to the numbers on a telephone keypad.

Digit

Letter

2

A B C

3

D E F

4

G H I

5

J K L

6

M N O

7

P R S

8

T U V

9

W X Y

 

Write a GUI program that, given a seven-digit number, uses a StreamWriter object to write to a file every possible seven-letter word combination corresponding to that number. There are 2,187 (37) such combinations. Avoid phone numbers with the digits 0 and 1.

18.8

(Student Poll) Figure 8.8 contains an array of survey responses that is hard-coded into the program. Suppose we wish to process survey results that are stored in a file. First, create a Windows Form that prompts the user for survey responses and outputs each response to a file. Use StreamWriter to create a file called numbers.txt. Each integer should be written using method Write. Then add a TextBox that will output the frequency of survey responses. You should modify the code in Fig. 8.8 to read the survey responses from numbers.txt. The responses should be read from the file by using a StreamReader. Class string's split method should be used to split the input string into separate responses, then each response should be converted to an integer. The program should continue to read responses until it reaches the end of file. The results should be output to the TextBox.

Extensible Markup Language (XML)

Категории