[ Pobierz całość w formacie PDF ]

5640_ch10.fm Page 575 Wednesday, February 11, 2004 2:33 PM
Programming Projects 575
generate the temporary file is to start with an unlikely name, such as "TempX", and to
append a character, such as 'X', until a name is found that does not name an existing file.
5. Write a program that gives and takes advice on program writing. The program starts by
writing a piece of advice to the screen and asking the user to type in a different piece of
advice. The program then ends. The next person to run the program receives the advice
given by the person who last ran the program. The advice is kept in a text file and the con-
tent of the file changes after each run of the program. You can use your editor to enter the
initial piece of advice in the file so that the first person who runs the program receives some
advice. Allow the user to type in advice of any length so that it can be any number of lines
long. The user is told to end his or her advice by pressing the Return key two times. Your
program can then test to see that it has reached the end of the input by checking to see
when it reads two consecutive occurrences of the character '\n'. Alternatively, your pro-
gram can simply test for an empty line marking the end of the file.
PROJECTS INVOLVING BINARY FILES
6. Write a program that will search a binary file of numbers of type int and write the largest
and the smallest numbers to the screen. The file contains nothing but numbers of type int
written to the file with writeInt.
7. Write a program that takes its input from a binary file of numbers of type double and out-
puts the average of the numbers in the file to the screen. The file contains nothing but
numbers of type double written to the file with writeDouble.
8. Write a program that takes its input from a binary file of numbers of type double. The file
contains nothing but numbers of type double written to the file with writeDouble. The
program outputs to the screen the average and standard deviation of the numbers in the
file. The standard deviation of a list of numbers n1, n2, n3, and so forth is defined as the
square root of the average of the following numbers:
(n1 - a)2, (n2 - a)2, (n3 - a)2, and so forth.
The number a is the average of the numbers n1, n2, n3, and so forth. Hint: Write your pro-
gram so that it first reads the entire file and computes the average of all the numbers, then
closes the file, then reopens the file and computes the standard deviation. You will find it
helpful to first do Programming Project 7 and then modify that program to obtain the pro-
gram for this project.
9. Change the definition of the class Person in Display 5.11 to be serializable. Note that this
requires that you also change the class Date. Then, write a program to maintain a binary
file of records of people (records of type Person). Allow commands to delete a record spec-
ified by the person s name, to add a record, to retrieve and display a record, and to obtain
all records of people within a specified age range. To obtain the age of a person, you need
the current date. Your program will ask the user for the current date when the program
begins. You can do this with random access files, but for this exercise do not use random
access files. Use a file or files that record records with the method writeObject of the class
ObjectOutputStream. [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • elpos.htw.pl