30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 17 Files and Streams 831<br />

17.2 a) 1s, 0s. b) bit. c) file. d) characters. e) database. f) ReadLine. g) WriteLine.<br />

h) Stream, Object. i) System.IO. j) System.Runtime.Serialization.Formatters.Binary.<br />

EXERCISES<br />

17.3 Create a program that s<strong>to</strong>res student grades in a text file. The file should contain the name,<br />

ID number, class taken and grade of every student. Allow the user <strong>to</strong> load a grade file and display its<br />

contents in a read-only textbox. The entries should be displayed as follows:<br />

LastName, FirstName: ID# Class Grade<br />

We list some sample data below:<br />

Jones, Bob: 1 "Introduction <strong>to</strong> Computer Science" "A-"<br />

Johnson, Sarah: 2 "Data Structures" "B+"<br />

Smith, Sam: 3 "Data Structures" "C"<br />

17.4 Modify the previous program <strong>to</strong> use objects of class that can be serialized <strong>to</strong> and deserialized<br />

from a file. Ensure fixed-length records by fixing the length of fields LastName, FirstName,<br />

Class and Grade.<br />

17.5 Extend classes StreamReader and StreamWriter. Allow the class that derives from<br />

StreamReader <strong>to</strong> have methods ReadInteger, ReadBoolean and ReadString. Allow the<br />

class that derives from StreamWriter <strong>to</strong> have methods WriteInteger, WriteBoolean and<br />

WriteString. Think about how <strong>to</strong> design the writing methods so that the reading methods will be<br />

able <strong>to</strong> read what was written. Design WriteInteger and WriteBoolean <strong>to</strong> write Strings of<br />

uniform size so that ReadInteger and ReadBoolean can read those values accurately. Make<br />

sure ReadString and WriteString use the same character(s) <strong>to</strong> separate Strings.<br />

17.6 Create a program that combines the ideas of Fig. 17.9 and Fig. 17.11 <strong>to</strong> allow the user <strong>to</strong><br />

write records <strong>to</strong> and read records from a file. Add an extra field of type Boolean <strong>to</strong> the record indicating<br />

whether the account has overdraft protection.<br />

17.7 In commercial data processing, it is common <strong>to</strong> have several files in each application system.<br />

In an accounts-receivable system, for example, there is generally a master file containing detailed information<br />

about each cus<strong>to</strong>mer, such as the cus<strong>to</strong>mer’s name, address, telephone number, outstanding<br />

balance, credit limit, discount terms, contract arrangements and possibly a condensed his<strong>to</strong>ry of recent<br />

purchases and cash payments.<br />

As transactions occur (i.e., sales are made and cash payments arrive in the mail), they are entered<br />

in<strong>to</strong> a file. At the end of each business period (i.e., a month for some companies, a week for<br />

others and a day in some cases), the file of transactions (trans.dat) is applied <strong>to</strong> the master file<br />

(oldmast.dat), thus updating each account’s record of purchases and payments. During an updating<br />

run, the master file is rewritten as a new file (newmast.dat), which then is used at the end of<br />

the next business period <strong>to</strong> begin the updating process again.<br />

File-matching programs must deal with certain problems that do not exist in single-file programs.<br />

For example, a match does not always occur. A cus<strong>to</strong>mer on the master file might not have<br />

made any purchases or cash payments in the current business period, and, therefore, no record for this<br />

cus<strong>to</strong>mer will appear on the transaction file. Similarly, a cus<strong>to</strong>mer who did make some purchases or<br />

cash payments might have just moved <strong>to</strong> the community, and the company might not have had a<br />

chance <strong>to</strong> create a master record for this cus<strong>to</strong>mer.<br />

When a match occurs (i.e., records with the same account number appear on both the master<br />

file and the transaction file), add the dollar amount on the transaction file <strong>to</strong> the current balance on<br />

the master file and write the newmast.dat record. (Assume that purchases are indicated by positive

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!