25.2. Client-Server Computing

 
[Page 742 ( continued )]

Programming Exercises

Section 22.3 Sets

22.1 ( Performing set operations on hash sets ) Create two hash sets { "George" , "Jim" , "John" , "Blake" , "Kevin" , "Michael" } and { "George" , "Katie" , "Kevin" , "Michelle" , "Ryan" }, and find their union, difference, and intersection. (You may clone the sets to preserve the original sets from being changed by these set methods .)
22.2 ( Displaying nonduplicate words in ascending order ) Write a program that reads words from a text file and displays all the nonduplicate words in ascending order. The text file is passed as a command-line argument.
22.3** ( Counting the keywords in Java source code ) Write a program that reads a Java source code file and reports the number of keywords in the file. Pass the Java file name from the command line.

Hint

Create a set to store all the Java keywords.


Section 22.4 Lists

22.4 ( Performing set operations on array lists ) Create two array lists { "George" , "Jim" , "John" , "Blake" , "Kevin" , "Michael" } and { "George" , "Katie" , "Kevin" , "Michelle" , "Ryan" }, and find their union, difference, and intersection. (You may clone the lists to preserve the original lists from being changed by these methods.)
22.5* ( Displaying words in ascending alphabetical order ) Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The text file is passed as a command-line argument.

Section 22.6 Static Methods for Lists and Collections

22.6* ( Storing numbers in a linked list ) Write a program that lets the user enter numbers from a graphical user interface and display them in a text area, as shown in Figure 22.26. Use a linked list to store the numbers. Do not store duplicate numbers. Add the buttons Sort , Shuffle , and Reverse to sort, shuffle, and reverse the list.
[Page 743]
Figure 22.26. The program stores numbers in a list.


Section 22.9 Maps

22.7* ( Counting the occurrences of numbers entered ) Write a program that reads an unspecified number of integers and finds the one that has the most occurrences. Your input ends when the input is . For example, if you entered 2 3 40 3 5 4 “3 3 3 2 0 , the number 3 occurred most often. Please enter one number at a time. If not one but several numbers have the most occurrences, all of them should be reported . For example, since 9 and 3 appear twice in the list 9 30 3 9 3 2 4 , both should be reported.
22.8** ( Revising Listing 22.10, CountOccurrenceOfWords.java ) Rewrite Listing 22.10 to display the words in ascending order of occurrence counts.

Hint

Create a class named WordOccurrence that implements the Comparable interface. The class contains two fields, word and count . The compareTo method compares the counts. For each pair in the hash set in Listing 22.10, create an instance of WordOccurrence and store it in an array list. Sort the array list using the Collections.sort method. What would be wrong if you stored the instances of WordOccurrence in a tree set?


22.9** ( Counting the occurrences of words in a text file ) Rewrite Listing 22.10 to read the text from a text file. The text file is passed as a command-line argument.
22.10*** ( Syntax highlighting ) Write a program that converts a Java file into an HTML file. In the HTML file, the keywords, comments, and literals are displayed in bold navy, green, and blue, respectively. Use the command line to pass a Java file and an HTML file. For example, the following command

java Exercise22_10 ComputeArea.java ComputeArea.HTML

converts Test.java into Test.HTML. Figure 22.27(a) shows a Java file. The corresponding HTML file is shown in Figure 22.27(b).

Figure 22.27. The Java code in plain text in (a) is displayed in HTML with syntax-highlighted in (b).

 


[Page 744]
 


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