1.

Consider the following line of code:

     Set names = new HashSet();

Why is the variable names of type Set rather than type HashSet?


Answer:


2.

Unlike the ListIterator interface, the Iterator interface for a set cannot add an item to a specified position. Is there a reason for this? If so, what is it?


Answer:


3.

The Iterator interface contains a next method. Why does the interface not contain a previous method?


Answer:


4.

For each of the following problems, indicate whether it would be more appropriate to use a HashSet or a HashMap, and explain your reasoning:

   1. A program that keeps track of bank account information (name, account number, account type, balance)
   2. A phonebook program that stores your friends' names and phone numbers
   3. A program to inventory the contents of your refrigerator
   4. A program that stores a list of your favorite fruits
   5. A program that stores a list of the favorite fruits of all the students in your class

Write two programs. Choose one of the above programs and implement it using a HashSet. Implement another of the above programs using a HashMap.


Answer:


5.

Write a program to keep track of the hometowns of all the students in your class using a HashMap. What values would you use for the key set? What values would you use for the value set?


Answer:


6.

Using a HashMap, write a program to store the favorite music style (classical, bluegrass, alternative rock, elevator music . . .) of your friends. Use an editable JComboBox to allow a user to choose a style from a list, or add her own style.

Below is sample code demonstrating the use of a JComboBox.

      String styles[] = {"Alternative", "Bluegrass", "Blues",
                         "Classic Rock", "Classical", "Country",
                         "Hip-hop", "Jazz", "Latin Music",
                         "Metal", "New Age", "Top 40s",
                         "Rap", "World Music"};
      styleComboBox = new JComboBox(styles);
      styleComboBox.setMaximumRowCount(styles.length);
      styleComboBox.setEditable(true);

Use JComboBox.getSelectedItem(Object obj) to access the selection in the JComboBox.

There should be at least two buttons, "Add Person" and "Print." The "Print" button should print out a list of styles in the HashMap, with a list of persons that prefer that style below it. If a person did not select a style from your combo box, do not print it. Make sure you add user-defined styles not in your combo box drop-down list. A sample output is given below.

Classic Rock
------------------
Robby
Chris  

Bluegrass
------------------
Tom
Lara  

Folk
------------------
Rob  

Alternative
------------------
Mike  

World Music
------------------
Forrest

. . .


Answer:


7.

Write a simulation program to gauge the relative efficiency of different sizes of hash tables.

Create or choose a file with a least 5,000 words. A good source would be a paper you have written for another class or check out www.gutenberg.net for free text files of books (whose copyrights have expired). If the file is a word processor file, convert the file to a text only file.

Your program should open a text file and use a StringTokenizer to add words from the file into a HashSet; this set will be used throughout the simulation.

Implement your own hash table with a counter that keeps track of collisions. You should be able to choose different sizes for the hash table.

Write a simulation program that iterates through the words in the HashSet and adds the words to your hash table. Choose five to ten different sizes for your hash table, from very small to very large. Use the count of the words in your HashSet to help choose the sizes. Display the number of collisions and the time it takes to add items to your hash table for each size hash table.


Answer:


8.

Implement an object that computes its hash code based on digit selection.

Create an Employee class. The class contains the name, address, phone number, job title, and Social Security number (SSN) of the employee. You must implement the methods

   public boolean equals(Object o)
   public int hashCode()

To compute the hash code, select the fifth, seventh, and eighth digits of the SSNs, and convert them to an integer. For example, the hash code for the SSN, 222-56-7890 would be 680.

Store the employee information in a HashSet.

Write a program to add employees to your set and display employee information by looking up employees using their SSNs. When employee information is displayed, allow the user the option to edit the information or delete the employee. When the program ends, write employee information to a file. When it reopens, load the file back into the hash set.


Answer:


9.

Create a PhoneBook class that stores entries in a binary search tree.

Create an Entry object. An Entry contains a person's first and last names and phone number. The Entry object must implement the Comparable interface. The entries are indexed on the last name. If the last names are equal, then the first names are indexed. If the first names are equal the index is based on the phone number.

Write a program that allows a user to add and delete entries, and lists the entries in alphabetical order.


Answer:


10.

Create a PhoneBook class that stores entries in a binary search tree and indexes the entries with a Comparator object.

Create an Entry object. An Entry contains a person's first and last names and phone number. Do not implement the Comparable interface, rather, create a Comparator object to index the entries. The entries are indexed on the last name. If the last names are equal, then the first names are indexed. If the first names are equal the index is based on the phone number.

Write a program that allows a user to add and delete entries, and lists the entries in alphabetical order.


Answer:


11.

Create a program to count and list alphabetically the distinct words in a text file.

Allow a user to choose a text file with a JFileChooser. Remove punctuation and convert all words to lowercase. Display a list of all words (do not display duplicates) in the file in alphabetical order using a JScrollPane. Display a total count of all the words (do not count duplicate words) in a JTextField.

Hints:

   Finding long text files

   A good source for text files is www.gutenberg.net. The site contains free text files of hundreds of books.

   Using the StringTokenizer to remove punctuation.

   By default, the StringTokenizer class removes whitespace. To specify other delimiters, pass the StringTokenizer a list of delimiters.

      final String delimiters = "-:`(),.;?!\\\\t \\\\n\\\\r\\\\"";
      String line;      
         while ((line = in.readLine()) != null)
         {          
            StringTokenizer wordFinder = new StringTokenizer(line, delimiters);                      String word = wordFinder.nextToken().toLowerCase();
               . . .

   (Optional) Using a FileFilter

   To specify types of files to display in a JFileChooser, use a javax.swing.filechooser.FileFilter. A FileFilter is an    abstract class with two methods,

      public boolean accept(File f)public String getDescription()

   Below is a filter for *.txt files.
         
      JFileChooser chooser = new JFileChooser();
      class TextFilter extends FileFilter    
         {
         public boolean accept(File f)
            {
            if (f.isDirectory())              
               { return true;}
            String name = f.getName().toLowerCase();             
            if (name.endsWith("txt"))          
            {            
               return true;          
            } else          
            {            
               return false;          
            }      
         }      
         public String getDescription()        
         {
            return "Text Files (*.txt)";        
         }
      }      
      chooser.setFileFilter(new TextFilter());


Answer:


12.

Modify your existing program or create a new program to list alphabetically the distinct words in a text file and the number of times each word appears in the text file.

Allow a user to choose a text file with a JFileChooser. Remove punctuation and convert all words to lowercase. Display a list of all words (do not display duplicates) in the file in alphabetical order using a JScrollPane, followed by the number of times each word appears in the text. Display a total count of all the words (do not count duplicate words) in a JTextField.

Hints:

   Finding long text files
   
   A good source for text files is www.gutenberg.net. The site contains free text files of hundreds of books.

   Using the StringTokenizer to remove punctuation.

   By default, the StringTokenizer class removes whitespace. To specify other delimiters, pass the StringTokenizer a list of delimiters.

      final String delimiters = "-:`(),.;?!\\\\t \\\\n\\\\r\\\\"";
      String line;      
         while ((line = in.readLine()) != null)
         {          
            StringTokenizer wordFinder = new StringTokenizer(line, delimiters);                      String word = wordFinder.nextToken().toLowerCase();
               . . .

   (Optional) Using a FileFilter

   To specify types of files to display in a JFileChooser, use a javax.swing.filechooser.FileFilter. A FileFilter is an    abstract class with two methods,

      public boolean accept(File f)public String getDescription()

   Below is a filter for *.txt files.
         
      JFileChooser chooser = new JFileChooser();
      class TextFilter extends FileFilter    
         {
         public boolean accept(File f)
            {
            if (f.isDirectory())              
               { return true;}
            String name = f.getName().toLowerCase();             
            if (name.endsWith("txt"))          
            {            
               return true;          
            } else          
            {            
               return false;          
            }      
         }      
         public String getDescription()        
         {
            return "Text Files (*.txt)";        
         }
      }      
      chooser.setFileFilter(new TextFilter());


Answer:


13.

Modify your existing program or create a program to count the distinct words in a text file, and display the top ten most frequently used words in the file.

Allow a user to choose a text file with a JFileChooser. Remove punctuation and convert all words to lowercase. Display a total count of all the words (do not count duplicate words) in a JTextField. In another JTextArea list the ten most frequently used words, with the number of times each word is used.

Hints:

   Finding long text files

   A good source for text files is www.gutenberg.net. The site contains free text files of hundreds of books.

   Using the StringTokenizer to remove punctuation.

   By default, the StringTokenizer class removes whitespace. To specify other delimiters, pass the StringTokenizer a list of delimiters.

      final String delimiters = "-:`(),.;?!\\\\t \\\\n\\\\r\\\\"";
      String line;      
         while ((line = in.readLine()) != null)
         {          
            StringTokenizer wordFinder = new StringTokenizer(line, delimiters);                      String word = wordFinder.nextToken().toLowerCase();
               . . .

   (Optional) Using a FileFilter

   To specify types of files to display in a JFileChooser, use a javax.swing.filechooser.FileFilter. A FileFilter is an    abstract class with two methods,

      public boolean accept(File f)public String getDescription()

   Below is a filter for *.txt files.
         
      JFileChooser chooser = new JFileChooser();
      class TextFilter extends FileFilter    
         {
         public boolean accept(File f)
            {
            if (f.isDirectory())              
               { return true;}
            String name = f.getName().toLowerCase();             
            if (name.endsWith("txt"))          
            {            
               return true;          
            } else          
            {            
               return false;          
            }      
         }      
         public String getDescription()        
         {
            return "Text Files (*.txt)";        
         }
      }      
      chooser.setFileFilter(new TextFilter());


Answer: