{"id":643,"date":"2013-10-20T03:35:18","date_gmt":"2013-10-20T10:35:18","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=643"},"modified":"2013-11-05T16:43:48","modified_gmt":"2013-11-05T23:43:48","slug":"week-12-lab-hashmaps","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2013\/10\/week-12-lab-hashmaps\/","title":{"rendered":"Week 12 Lab &#8211; HashMaps"},"content":{"rendered":"<p>This week&#8217;s tutorial is taken from the textbook, exercises 5.25 &#8211; 32 on page 174-175. They are reproduced below.<\/p>\n<p>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.<\/p>\n<pre>    HashMap&lt;String, String&gt; phoneBook= new HashMap&lt;String, String&gt;();<\/pre>\n<p>initialises the phone book. The following statements add new entries:<\/p>\n<pre>    phoneBook.put(\"Charles Nguyen\", \"(02) 9392 4587\");\r\n    phoneBook.put(\"Lisa Jones\", \"(03) 4536 4674\");\r\n    phoneBook.put(\"William H. Smith\", \"(07) 5488 0123\");<\/pre>\n<p>Next, we look up an entry:<\/p>\n<pre>    String number = phoneBook.get(\"Lisa Jones\");\r\n    System.out.println(number);<\/pre>\n<p>We refer to the name as the\u00a0<em>key<\/em> because it is used for the lookup. Given the above, answer the following questions. You will need to use the Java API documentation.<\/p>\n<ol>\n<li>Create a class\u00a0<tt>MapTester<\/tt> in a new project. In it, use a\u00a0<tt>HashMap<\/tt> to implement a phone book. You will have to\u00a0<tt>import java.util.HashMap<\/tt>. In this class, implement two methods:\n<pre>public void enterNumber(String name, String number)<\/pre>\n<p>and<\/p>\n<pre>public String lookupNumber(String name)<\/pre>\n<p>The methods should use\u00a0<tt>put<\/tt> and\u00a0<tt>get<\/tt> methods of the\u00a0<tt>HashMap<\/tt> class to implement their functionality.<\/li>\n<li>What happens when you add an entry to a map with a key that already exists in the map?<\/li>\n<li>What happens when you add an entry to a map with two different keys?<\/li>\n<li>How do you check whether a given key is contained in a map? Try it in Java.<\/li>\n<li>What happens when you try to look up a values and the key does not exist in the map?<\/li>\n<li>How do you check how many entries are contained in the map?<\/li>\n<li>How do you print out all keys currently stored in a map?<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This week&#8217;s tutorial is taken from the textbook, exercises 5.25 &#8211; 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 [&hellip;]<\/p>\n","protected":false},"author":328,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[630],"tags":[4597],"class_list":["post-643","post","type-post","status-publish","format-standard","hentry","category-lab","tag-4597"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/users\/328"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/comments?post=643"}],"version-history":[{"count":2,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/643\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}