Exercises

Included below are short-answer and programming exercises. Answers are provided for those exercises whose exercise number is a hyperlink. Because college faculty use these exercises in their exams, we have provided answers to roughly half of the exercises included here.


17.5 Fill in the blanks in each of the following:
  1. Computers store large amounts of data on secondary storage devices as ________.
  2. A ________ is composed of several fields.
  3. A field that may contain only digits, letters, and blanks is called an ________ field.
  4. To facilitate the retrieval of specific records from a file, one field in each record is chosen as a ________.
  5. The vast majority of information stored in computer systems is stored in ________ files.
  6. The standard stream objects are ________, ________ and ________.

17.6 State which of the following are true and which are false. If false, explain why.

  1. The impressive functions performed by computers essentially involve the manipulation of zeros and ones.
  2. People specify programs and data items as characters; computers then manipulate and process these characters as groups of zeros and ones.
  3. A person's 5-digit zip code is an example of a numeric field.
  4. A person's street address is generally considered to be an alphabetic field.
  5. Data items represented in computers form a data hierarchy in which data items become larger and more complex as we progress from fields to characters to bits, etc.
  6. A record key identifies a record as belonging to a particular field.
  7. Companies store all their information in a single file to facilitate computer processing.
  8. When a program creates a file, the file is automatically retained by the computer for future reference.

17.7 Exercise 17.3 asked the reader to write a series of single statements. Actually, these statements form the core of an important type of file-processing program, namely, a file-matching program. In commercial data processing, it is common to have several files in each application system. In an accounts receivable system, for example, there is generally a master file containing detailed information about each customer such as the customer's name, address, telephone number, outstanding balance, credit limit, discount terms, contract arrangements, and possibly a condensed history of recent purchases and cash payments.

As transactions occur (i.e., sales are made and cash payments arrive in the mail), they are entered into a file. At the end of each business period (i.e., a month for some companies, a week for others, and a day in some cases) the file of transactions (called "trans.dat" in Exercise 17.3) is applied to the master file (called "oldmast.dat" in Exercise 17.3), thus updating each account's record of purchases and payments. During an updating run, the master file is rewritten as a new file ("newmast.dat"), which is then used at the end of the next business period to begin the updating process again.

File-matching programs must deal with certain problems that do not exist in single-file programs. For example, a match does not always occur. A customer on the master file may not have made any purchases or cash payments in the current business period, and therefore no record for this customer will appear on the transaction file. Similarly, a customer who did make some purchases or cash payments may have just moved to this community, and the company may not have had a chance to create a master record for this customer.

Use the statements in Exercise 17.3 as a basis for writing a complete file-matching accounts receivable program. Use the account number on each file as the record key for matching purposes. Assume that each file is a sequential file with records stored in increasing account number order.

When a match occurs (i.e., records with the same account number appear on both the master file and the transaction file), add the dollar amount on the transaction file to the current balance on the master file, and write the "newmast.dat" record. (Assume that purchases are indicated by positive amounts on the transaction file, and that payments are indicated by negative amounts.) When there is a master record for a particular account but no corresponding transaction record, merely write the master record to "newmast.dat". When there is a transaction record but no corresponding master record, print the message "Unmatched transaction record for account number …" (fill in the account number from the transaction record).

17.8 After writing the program of Exercise 17.7, write a simple program to create some test data for checking out the program. Use the following sample account data:

Master file
Account number
Name Balance
100 Alan Jones 348.17
300 Mary Smith 27.19
500 Sam Sharp 0.00
700 Suzy Green -14.22

Transaction file
Account number
Transaction amount
100 27.14
300 62.11
400 100.56
900 82.17

 

Run the program of Exercise 17.7 using the files of test data created in Exercise 17.8. Print the new master file. Check that the accounts have been updated correctly.

17.9 It is possible (actually common) to have several transaction records with the same record key. This occurs because a particular customer might make several purchases and cash payments during a business period. Rewrite your accounts receivable file-matching program of Exercise 17.7 to provide for the possibility of handling several transaction records with the same record key. Modify the test data of Exercise 17.8 to include the following additional transaction records:

Account number Dollar amount
300 83.89
700 80.78
700 1.53

 

17.10 You are the owner of a hardware store and need to keep an inventory that can tell you what different tools you have, how many of each you have on hand, and the cost of each one. Write a program that initializes the random-access file "hardware.dat" to one hundred empty records, lets you input the data concerning each tool, enables you to list all your tools, lets you delete a record for a tool that you no longer have, and lets you update any information in the file. The tool identification number should be the record number. Use the following information to start your file:

Record # Tool name Quantity Cost
3 Electric sander 18 35.99
19 Hammer 128 10.00
26 Jig saw 16 14.25
39 Lawn mower 10 79.50
56 Power saw 8 89.99
76 Screwdriver 236 4.99
81 Sledge hammer 32 19.75
88 Wrench 65 6.48

 

17.11 (Telephone Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as indicated in the Fig. 17.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. 17.16 to develop the seven-letter word "NUMBERS."

Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its takeout business could surely do so with the number 825-3688 (i.e., "TAKEOUT").

Each seven-letter phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtapositions of letters. It is possible, however, that the owner of a barber shop 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 the dealership number, 639-2277, corresponds to "NEWCARS."

Write a program that, given a seven-digit number, writes to a file every possible seven-letter word combination corresponding to that number. There are 2187 (37) such words. Avoid phone numbers with the digits 0 and 1.

Fig. 17.17

Telephone keypad digits and letters.

 
Digit Letters
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

 


Selected Answers

Included below are answers to approximately half the of the exercises in the Cyber Classroom. We are not able to include answers to every exercise because college faculty use these exercises in their classroom exams.


17.5 Fill in the blanks in each of the following:

  1. Computers store large amounts of data on secondary storage devices as .
    ANS: files
  2. A is composed of several fields.
    ANS: record
  3. A field that may contain only digits, letters, and blanks is called an field.
    ANS: alphanumeric.
  4. To facilitate the retrieval of specific records from a file, one field in each record is chosen as a .
    ANS: key
  5. The vast majority of information stored in computer systems is stored in files.
    ANS: sequential
  6. The standard stream objects are , and .
    ANS: System.in, System.out, System.err

17.6 State which of the following are true and which are false. If false, explain why.

  1. The impressive functions performed by computers essentially involve the manipulation of zeros and ones.
    ANS: True.
  2. People specify programs and data items as characters; computers then manipulate and process these characters as groups of zeros and ones.
    ANS: True.
  3. A person's 5-digit zip code is an example of a numeric field.
    ANS: True.
  4. A person's street address is generally considered to be an alphabetic field.
    ANS: True.
  5. Data items represented in computers form a data hierarchy in which data items become larger and more complex as we progress from fields to characters to bits, etc.
    ANS: False. Data becomes more complex as we progress from bits to characters to fields, etc.
  6. A record key identifies a record as belonging to a particular field.
    ANS: False. A record key identifies a record as belonging to a particular person or entity.
  7. Companies store all their information in a single file to facilitate computer processing.
    ANS: False. Companies typically store information in multiple files.
  8. When a program creates a file, the file is automatically retained by the computer for future reference.
    ANS: False. The file must be saved to disk (or other storage medium).

17.11

// Exercise 17.11 Solution// Phone.java// Note: phone number must be input in the form 2345678// Only the digits 2 thru 9 are recognized.import javax.swing.*;import java.io.*;import java.awt.event.*;import java.awt.*;public class Phone extends JFrame          implements ActionListener {   private int phoneNumber[];   private JTextField input;   private JLabel prompt;   public Phone()   {      super( "Phone" );      phoneNumber = new int[ 7 ];      input = new JTextField( 15 );      input.addActionListener( this );      prompt = new JLabel( "Enter phone number (digits only):" );      Container c = getContentPane();      c.setLayout( new FlowLayout() );      c.add( prompt );      c.add( input );      setSize( 200, 200 );      show();   }   public void actionPerformed( ActionEvent e )    {            String number = input.getText();      String letters[] = { "", "", "ABC", "DEF", "GHI", "JKL",                           "MNO", "PRS", "TUV", "WXY" };      DataOutputStream dos = null;        // Output stream      try {         dos = new DataOutputStream( new FileOutputStream( "phone.dat" ) );      } catch( IOException ex ) {         JOptionPane.showMessageDialog(                null, ex.toString(), "Exception",                JOptionPane.ERROR_MESSAGE );         System.exit( 1 );      }      // output all possible combinations      for (int loop1 = 0; loop1 <= 2; loop1++)       for (int loop2 = 0; loop2 <= 2; loop2++)        for (int loop3 = 0; loop3 <= 2; loop3++)         for (int loop4 = 0; loop4 <= 2; loop4++)          for (int loop5 = 0; loop5 <= 2; loop5++)           for (int loop6 = 0; loop6 <= 2; loop6++)            for (int loop7 = 0; loop7 <= 2; loop7++) {             try {                dos.writeByte( ( byte ) letters[ ( number.charAt( 0 ) - 48 ) ].charAt( loop1 ) );                 dos.writeByte( ( byte ) letters[ ( number.charAt( 1 ) - 48 ) ].charAt( loop2 ) );                dos.writeByte( ( byte ) letters[ ( number.charAt( 2 ) - 48 ) ].charAt( loop3 ) );                dos.writeByte( ( byte ) letters[ ( number.charAt( 3 ) - 48 ) ].charAt( loop4 ) );                dos.writeByte( ( byte ) letters[ ( number.charAt( 4 ) - 48 ) ].charAt( loop5 ) );                dos.writeByte( ( byte ) letters[ ( number.charAt( 5 ) - 48 ) ].charAt( loop6 ) );                dos.writeByte( ( byte ) letters[ ( number.charAt( 6 ) - 48 ) ].charAt( loop7 ) );                dos.writeByte( ( byte ) ' ' );             } catch( IOException io ) {                JOptionPane.showMessageDialog(                   null, e.toString(), "Exception",                   JOptionPane.ERROR_MESSAGE );                System.exit( 1 );             }             input.setText( "Please wait..." );            }      input.setText( "Done" );   }   public static void main( String args[] )   {      Phone p = new Phone();      p.addWindowListener(          new WindowAdapter() {            public void windowClosing( WindowEvent e )            {               System.exit( 0 );            }            }      );   }}