Solution for Programming Exercise 10.1
This page contains a sample solution to one of the exercises from Introduction to Programming Using Java.
Exercise 10.1:
Rewrite the PhoneDirectory class from Subsection 7.4.2 so that it uses a TreeMap to store directory entries, instead of an array. (Doing this was suggested in Subsection 10.3.1.) You should also write a short program to test the class.
The class that you are asked to write for this exercise really is very trivial. Everything that the class needs to do is already done by the TreeMap class, and TreeMap does much more besides. In fact the only reason to write the PhoneDirectory class is to hide a lot of the capabilities of TreeMaps. A PhoneDirectory object is a "thin wrapper" around a TreeMap object. It allows only the operations getNumber(name) and putNumber(name,number). Each of these can be implemented in a single line, as a call to one of the methods in the TreeMap. The PhoneDirectory does provide nice, meaningful names for the methods, and it adds a check to the implementation of putNumber(name,number) to ensure that name and number are not null.
It would be nice for phone directories to have a few more capabilities. A method for listing the contents of the directory would be especially nice, so I have added one to my class even though there was no such method in the original PhoneDirectory class. The print method uses a for-each loop to iterate through the entry set of the phone directory. It is very similar to examples in Subsection 10.3.2 Even with this addition, though, the PhoneDirectory class is still very far from being useful in real applications.
import java.util.Map; import java.util.TreeMap; /** * A phone directory holds a list of names with a phone number for * each name. It is possible to find the number associated with * a given name, and to specify the phone number for a given name. * (This version of the class uses a TreeMap to store the data.) */ public class PhoneDirectoryWithTreeMap { /** * The TreeMap that will store the data. Both key and value are * of type String. The key represents a name and the value represents * the associated phone number. */ private TreeMap<String,String> data; /** * Constructor creates an initially empty directory. */ public PhoneDirectoryWithTreeMap() { data = new TreeMap<String,String>(); } /** * Finds the phone number, if any, for a given name. * @return The phone number associated with the name; if the name does * not occur in the phone directory, then the return value is null. */ public String getNumber( String name ) { return data.get(name); } /** * Associates a given name with a given phone number. If the name * already exists in the phone directory, then the new number replaces * the old one. Otherwise, a new name/number pair is added. The * name and number should both be non-null. An IllegalArgumentException * is thrown if this is not the case. */ public void putNumber( String name, String number ) { if (name == null || number == null) throw new IllegalArgumentException("name and number cannot be null"); data.put(name,number); } /** * Write the contents of the phone directory to System.out. */ public void print() { for ( Map.Entry<String,String> entry : data.entrySet() ) System.out.println( entry.getKey() + ": " + entry.getValue() ); } } // end class PhoneDirectoryWithTreeMap
Here is a very small program to test the class:
public class TestPhoneDirectoryWithTreeMap { public static void main(String[] args) { PhoneDirectoryWithTreeMap directory = new PhoneDirectoryWithTreeMap(); System.out.println("This program creates a PhoneDirectoryWithTreeMap and"); System.out.println("adds several entries. It then prints the contents of"); System.out.println("directory and does a few lookups."); System.out.println(); directory.putNumber("Fred","555-1234"); directory.putNumber("Barney","555-2345"); directory.putNumber("Wilma","555-3456"); System.out.println("Contents are:"); System.out.println(); directory.print(); System.out.println(); System.out.println("Number for Fred is " + directory.getNumber("Fred")); System.out.println("Number for Wilma is " + directory.getNumber("Wilma")); System.out.println("Number for Tom is " + directory.getNumber("Tom")); // The output from the last line should be null. } }