Java Reference
In-Depth Information
**14.20
( Remove package statement ) Suppose you have Java source files under the direc-
tories chapter1 , chapter2 ,..., chapter34 . Write a program to remove the
statement package chapteri; in the first line for each Java source file under
the directory chapteri . Suppose chapter1 , chapter2 ,..., chapter34 are
under the root directory srcRootDirectory . The root directory and chapteri
directory may contain other folders and files. Use the following command to run
the program:
java Exercise14_20 srcRootDirectory
**14.21
( Display a graph ) A graph consists of vertices and edges that connect vertices.
Write a program that reads a graph from a file and displays it on a panel. The
first line in the file contains a number that indicates the number of vertices ( n ).
The vertices are labeled as 0 , 1 ,..., n-1 . Each subsequent line, with the format
u x y v1 v2 ... , describes that the vertex u is located at position ( x , y ) with
edges ( u , v1 ), ( u , v2 ), and so on. Figure 14.14a gives an example of the file for
a graph. Your program prompts the user to enter the name of the file, reads data
from the file, and displays the graph on a panel, as shown in Figure 14.14b.
Write another program that reads data from a Web URL such as
http://cs.armstrong.edu/liang/data/graph.txt . This program should prompt the user to
enter the URL for the file.
File
6
0303012
1903003
23090034
390901245
4 30 150 2 3 5
5 90 150 3 4
0
1
2
3
4
5
(a)
(b)
F IGURE 14.14
Exercise 14.21 reads the information about the graph and displays it visually.
**14.22
( Replace text ) Revise Exercise 14.16 to replace a string in a file with a new
string for all files in the specified directory using the command:
java Exercise14_22 dir oldString newString
**14.23
( Process scores in a text file on the Web ) Suppose that the text file on the Web
http://cs.armstrong.edu/liang/data/Scores.txt contains an unspecified number of
scores. Write a program that reads the scores from the file and displays their
total and average. Scores are separated by blanks.
*14.24
( Create large dataset ) Create a data file with 1,000 lines. Each line in the file
consists of a faculty member's first name, last name, rank, and salary. The fac-
ulty member's first name and last name for the i th line are FirstName i and Last-
Name i . The rank is randomly generated as assistant, associate, and full. The
salary is randomly generated as a number with two digits after the decimal
point. The salary for an assistant professor should be in the range from 50,000 to
 
Search WWH ::




Custom Search