21.4. Generic Methods

 
[Page 633 ( continued )]

Programming Exercises

Section 18.3 Text I/O vs. Binary I/O

18.1* ( Creating a text file ) Write a program to create a file named Exercise18_1.txt if it does not exist. Append new data to it if the file already exists. Write one hundred integers created randomly into the file using text I/O. Integers are separated by a space.

Section 18.4 Binary I/O Classes

18.2* ( Creating a binary data file ) Write a program to create a file named Exercise18_2.dat if it does not exist. Append new data to it if the file already exists. Write one hundred integers created randomly into the file using binary I/O.
18.3* ( Summing all the integers in a binary data file ) Suppose a binary data file named Exercise18_3.dat has been created using writeInt(int) in DataOutputStream . The file contains an unspecified number of integers. Write a program to find the total of integers.
18.4* ( Converting a text file into UTF ) Write a program that reads lines of characters from a text and writes each line as a UTF-8 string into a binary file. Display the sizes of the text file and the binary file. Use the following command to run the program:
   java Exercise18_4 Welcome.java Welcome.utf   

Section 18.6 Object I/O

18.5* ( Storing objects and arrays into a file ) Write a program that stores an array of five int values 1 , 2 , 3 , 4 and 5 , a Date object for current time, and a double value 5.5 into the file named Exercise18_5.dat .
18.6* ( Storing Loan objects ) The Loan class, introduced in §7.15, "Case Study: The Loan Class," does not implement Serializable . Rewrite the Loan class to implement Serializable . Write a program that creates five Loan objects and stores them in a file named Exercise18_6.dat .
18.7* ( Restoring objects from a file ) Suppose a file named Exercise18_7.dat has been created using the ObjectOutputStream . Write a program that reads the Loan objects from the file and computes the total loan amount. Suppose you don't know how many Loan objects are in the file. Use EOFException to end the loop.

[Page 634]

Section 18.7 (Optional) Random Access Files

18.8* ( Updating count ) Suppose you want to track how many times a program has been executed. You may store an int to count the file. Increase the count by 1 each time this program is executed. Let the program be Exercise18_8 , and store the count in Exercise18_8.dat .
18.9** ( Updating address ) Modify AddressBook in Listing 18.9, AddressBook.java, to add an Update button, as shown in Figure 18.21, to enable the user to modify an address that is being displayed.
Figure 18.21. You can update the address record that is currently displayed.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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