Self-Review Exercises
Java(c) How to Program
Authors: Deitel H. M.
Published year: 2003
Pages: 302/615
Buy this book on amazon.com >>

[Page 737]

Self-Review Exercises

14.1

Fill in the blanks in each of the following statements:

  1. Ultimately, all data items processed by a computer are reduced to combinations of __________ and __________.

  2. The smallest data item a computer can process is called a(n)__________.

  3. A(n) __________ can sometimes be viewed as a group of related records.

  4. Digits, letters and special symbols are referred to as __________.

  5. A group of related files is called a __________.

  6. Object __________ normally enables a program to output error messages to the screen.

  7. RandomAccessFile method __________ reads an integer from the specified stream.

  8. RandomAccessFile method __________ sets the file-position pointer to a specific location in a file for input or output.

14.2

Determine which of the following statements are true and which are false . If false , explain why.

  1. The programmer must explicitly create the stream objects System.in , System.out and System.err .

  2. When reading data from a file using class Scanner , if the programmer wishes to read data in the file multiple times, the file must be closed and reopened to read from the beginning of the file. This moves the file-position pointer back to the beginning of the file.

  3. Method exists of class File returns TRue if the name specified as the argument to the File constructor is a file or directory in the specified path .

  4. It is not necessary to search all the records in a random-access file to find a record.

  5. Binary files are human readable.

  6. An absolute path contains all the directories, starting with the root directory, that lead to a specific file or directory.

  7. Class Formatter contains method printf , which enables formatted data to be output to the screen or to a file.

14.3

Complete the following tasks , assuming that each applies to the same program:

  1. Write a statement that opens file "oldmast.txt" for inputuse Scanner variable inOldMaster .

  2. Write a statement that opens file "trans.txt" for inputuse Scanner variable inTransaction .

  3. Write a statement that opens file "newmast.txt" for output (and creation)use formatter variable outNewMaster .

  4. Write the statements needed to read a record from the file "oldmast.txt" . The data read should be used to create an object of class AccountRecord use Scanner variable inOldMaster . Assume that class AccountRecord is the same as the AccountRecord class in Fig. 14.6.

  5. Write the statements needed to read a record from the file "trans.txt" . The record is an object of class transactionRecord use Scanner variable inTransaction . Assume that class transactionRecord contains method setAccount (which takes an int ) to set the account number and method setAmount (which takes a double ) to set the amount of the transaction.

  6. Write a statement that outputs a record to the file "newmast.txt" . The record is an object of type AccountRecord use Formatter variable outNewMaster .

14.4

Complete the following tasks, assuming that each applies to the same program:

  1. Write a statement that opens file "oldmast.ser" for inputuse ObjectInputStream variable inOldMaster to wrap a FileInputStream object.


    [Page 738]
  2. Write a statement that opens file "trans.ser" for inputuse ObjectInputStream variable inTransaction to wrap a FileInputStream object.

  3. Write a statement that opens file "newmast.ser" for output (and creation)use ObjectOutputStream variable outNewMaster to wrap a FileOutputStream .

  4. Write a statement that reads a record from the file "oldmast.ser" . The record is an object of class AccountRecordSerializable use ObjectInputStream variable inOldMaster . Assume class AccountRecordSerializable is the same as the AccountRecordSerializable class in Fig. 14.17

  5. Write a statement that reads a record from the file "trans.ser" . The record is an object of class transactionRecord use ObjectInputStream variable inTransaction .

  6. Write a statement that outputs a record to the file "newmast.ser" . The record is an object of type AccountRecordSerializable use ObjectOutputStream variable outNewMaster .

14.5

Find the error in each block of code and show how to correct it.

  1. Assume that account , company and amount are declared.

    ObjectOutputStream outputStream;
    
         outputStream.writeInt( account );
         outputStream.writeChars( company );
         outputStream.writeDouble( amount );
    

  2. The following statements should read a record from the file "payables.txt" . The Scanner variable inPayable should be used to refer to this file.

    Scanner inPayable =
    
    new
    
    Scanner(
    
    new
    
    File(
    
    "payables.txt"
    
    ) );
         PayablesRecord record = ( PayablesRecord ) inPayable.readObject();
    


Java(c) How to Program
Authors: Deitel H. M.
Published year: 2003
Pages: 302/615
Buy this book on amazon.com >>

Similar books on Amazon