Exercises


[Page 553 (continued)]

Note

For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.


Exercise 11.1

Explain the difference between each of the following pairs of terms:

  1. System.in and System.out.

  2. File and directory.

  3. Buffering and filtering.

  4. Absolute and relative path name.

  5. Input stream and output stream.

  6. File and database.

  7. Record and field.

  8. Binary file and text file.

  9. Directory and database.

Exercise 11.2

Fill in the blanks.

  1. Unlike text files, binary files do not have a special ___________ character.

  2. In Java, the String array parameter in the main() method is used for ___________.

  3. ___________ files are portable and platform independent.

  4. A ___________ file created on one computer can't be read by another computer.

Exercise 11.3

Arrange the following kinds of data into their correct hierarchical relationships: bit, field, byte, record, database, file, String, char.

Exercise 11.4

In what different ways can the following string of 32 bits be interpreted?

00010101111000110100000110011110



[Page 554]
Exercise 11.5

When reading a binary file, why is it necessary to use an infinite loop that is exited only when an exception occurs?

Exercise 11.6

Is it possible to have a text file with 10 characters and 0 lines? Explain.

Exercise 11.7

In reading a file, why is it necessary to attempt to read from the file before entering the read loop?

Exercise 11.8

When designing binary I/O, why is it especially important to design the input and output routines together?

Exercise 11.9

What is the difference between ASCII code and UTF code?

Exercise 11.10

Could the following string of bits possibly be a Java object? Explain.

00010111000111101010101010000111001000100 11010010010101010010101001000001000000111 


Exercise 11.11

Write a method that could be added to the TextIO program to read a text file and print all lines containing a certain word. This should be a void method that takes two parameters: the name of the file and the word to search for. Lines not containing the word should not be printed.

Exercise 11.12

Write a program that reads a text file and reports the number of characters and lines contained in the file.

Exercise 11.13

Modify the program in the preceding exercise so that it also counts the number of words in the file. (Hint: The StringTokenizer class might be useful for this task.)

Exercise 11.14

Modify the ObjectIO program so that it allows the user to designate a file and then input Student data with the help of a GUI. As the user inputs data, each record should be written to the file.

Exercise 11.15

Write a program that will read a file of ints into memory, sort them in ascending order, and output the sorted data to a second file.

Exercise 11.16

Write a program that will read two files of ints, which are already sorted into ascending order, and merge their data. For example, if one file contains 1, 3, 5, 7, 9, and the other contains 2, 4, 6, 8, 10, then the merged file should contain 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

Exercise 11.17

Suppose you have a file of data for a geological survey. Each record consists of a longitude, a latitude, and an amount of rainfall, all represented by doubles. Write a method to read this file's data and print them on the screen, one record per line. The method should be void and should take the name of the file as its only parameter.

Exercise 11.18

Suppose you have the same data as in the preceding exercise. Write a method that will generate 1,000 records of random data and write them to a file. The method should be void and should take the file's name as its parameter. Assume that longitudes have values in the range +/- 0 to 180 degrees, latitudes have values in the range +/- 0 to 90 degrees, and rainfalls have values in the range 0 to 20 inches.

Exercise 11.19

Design and write a file copy program that will work for either text files or binary files. The program should prompt the user for the names of each file and copy the data from the source file into the destination file. It should not overwrite an existing file, however. (Hint: Read and write the file as a file of byte.)


[Page 555]
Exercise 11.20

Design a class, similar to Student, to represent an Address. It should consist of street, city, state, and zip code and should contain its own readFromFile() and writeToFile() methods.

Exercise 11.21

Using the class designed in the preceding exercise, modify the Student class so that it contains an Address field. Modify the ObjectIO program to accommodate the new definition of Student and test your program.

Exercise 11.22

Write a program called Directory, which provides a listing of any directory contained in the current directory. This program should prompt the user for the name of the directory. It should then print a listing of the directory. The listing should contain the following information: the full path name of the directory, and the file name, length, last modified date, and a read/write code for each file. The read/write code should be an r if the file is readable and a w if the file is writeable, in that order. Use a "-" to indicate not readable or not writeable. For example, a file that is readable but not writeable will have the code r-. Here is an example listing:

Listing for directory: myfiles   name          length modified   code   index.html    548    129098     rw   index.gif     78     129190     rw   me.html       682    128001     r-   private.txt   1001   129000     -- 


Note that the File.lastModified() returns a long, which gives the modification time of the file. This number cannot easily be converted into a date, so just report its value.

Exercise 11.23

Challenge: Modify the OneRowNimGUI class listed in Figure 4.25 so that the user can save the position of the game to a file or open and read a game position from a file. Add two new JButtons to the GUI interface. Use the object serialization example as a model for your input and output streams.

Exercise 11.24

Challenge: In Unix systems, there is a program named grep that can list the lines in a text file containing a certain string. Write a Java version of this program that prompts the user for the name of the file and the string to search for.

Exercise 11.25

Challenge: Write a program in Java named Copy to copy one file into another. The program will prompt the user for two file names, filename1 and filename2. Both filename1 and filename2 must exist or the program should throw a FileNotFoundException. Although filename1 must be the name of a file (not a directory), filename2 may be either a file or a directory. If filename2 is a file, then the program should copy filename1 to filename2. If filename2 is a directory, then the program should simply copy filename1 into filename2. That is, it should create a new file with the name filename1 inside the filename2 directory, copy the old file to the new file, and then delete the old file.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net