Java Reference
In-Depth Information
4. Write a program that takes its input from a text file of strings representing numbers
of type double . The program outputs to the screen the average and standard devia-
tion of the numbers in the file. The file contains nothing but strings representing
numbers of type double , one per line. The standard deviation of a list of numbers
n 1 , n 2 , n 3 , and so forth is defined as the square root of the average of the following
numbers:
( n 1 - a ) 2 , ( n 2 - a ) 2 , ( n 3 - a ) 2 , and so forth.
The number a is the average of the numbers n 1 , n 2 , n 3 , and so forth. Hint: Write
your program so that it first reads the entire file and computes the average of all the
numbers, then closes the file, and then reopens the file and computes the standard
deviation. You will find it helpful to first do Programming Project 10.3 and then
modify that program in order to obtain the program for this project.
5. Write a program to edit text files for extra blanks. The program will replace any
string of two or more blanks with a single blank. Your program should work as
follows: Create a temporary file. Copy from the file to the temporary file but do not
copy extra blanks. Copy the contents of the temporary file back into the original
file. Use a method (or methods) in the class File to remove the temporary file.
You will also want to use the class File for other things in your program. The
temporary file should have a name that is different from all existing files so that
the existing files are not affected (except for the file being edited). Your program
will ask the user for the name of the file to be edited. However, it will not ask the
user for the name of the temporary file but instead will generate the name within
the program. You can generate the name any way that is clear and efficient. One
possible way to 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.
6. 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 dif-
ferent 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 content 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' .
7. Write a class that keeps track of the top five high scores that could be used for a
video game. Internally, the class should store the top scores in a data structure of
your choice (the most straightforward way is to use arrays). Each entry consists of
a name and a score. The data stored in memory should be synchronized with a text
file for persistent storage. For example, here are the contents of a sample file where
Ronaldo has the highest score and Pele has the third highest score:
Search WWH ::




Custom Search