Lab 7 UNSW Blogs Home UNSW Wikispaces UNSW Website MyUNSW RSS COMP1400 – Programming for Designers Class announcements for Programming for Designers Lab 7 Posted by malcolmr on September 11th, 2011 · Uncategorized This week’s tutorial is taken from the textbook, exercises 5.25 – 30 on page 149. 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 phoneBook= new HashMap(); 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. Create a class MapTester in a new project. In it, use a HashMap to implement a phone book. You will have to import java.util.HashMap. In this class, implement two methods: 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. What happens when you add an entry to a map with a key that already exists in the map? What happens when you add an entry to a map with two different keys? How do you check whether a given key is contained in a map? Try it in Java. What happens when you try to look up a values and the key does not exist in the map? How do you check how many entries are contained in the map? Email to friend Stay updated Tags: No Comments so far ↓ There are no comments yet...Kick things off by filling out the form below. You must log in to post a comment. Pages Course Outline for Semester 2, 2014 For Tutors Lab Times Lecture Notes and Example Files Software Weekly Quizzes Search Categories Announcements Assignments Lab Lectures Uncategorized Links BlueJ web site Submit/Collect Assignments View Marks Staff Claude Sammut (Lecturer) Timothy Wiley (Admin/Tutor) Troy Lenger (Tutor) Sam Ipsen (Tutor) Meta Log in Entries feed Comments feed WordPress.org UNSW Blogs User Tools Username and Password: Lost your password? COMP1400 – Programming for Designers Terms of Use Privacy Policy Copyright and Disclaimer Accessibility Acknowledgements UNSW Sydney NSW 2052 Australia Telephone +61 2 9385 1000 UNSW CRICOS Provider Code 00098G ABN 57 195 873 179 Design by jokay