This week’s tutorial is taken from the textbook, exercises 5.25 – 32 on page 174-175. They are reproduced below.
The task is to implement a simple phone book where numbers are associated with names, both represented as strings. Your phone book should be able to add a new name/number pair and you should be able to lookup a number, given a name.
HashMap<String, String> phoneBook= new HashMap<String, String>();
initialises the phone book. The following statements add new entries:
phoneBook.put("Charles Nguyen", "(02) 9392 4587"); phoneBook.put("Lisa Jones", "(03) 4536 4674"); phoneBook.put("William H. Smith", "(07) 5488 0123");
Next, we look up an entry:
String number = phoneBook.get("Lisa Jones"); System.out.println(number);
We refer to the name as the key because it is used for the lookup. Given the above, answer the following questions. You will need to use the Java API documentation.
public void enterNumber(String name, String number)
and
public String lookupNumber(String name)
The methods should use put and get methods of the HashMap class to implement their functionality.
There are no comments yet...Kick things off by filling out the form below.
You must to post a comment.