|
1 | | -# Phone Book |
2 | | -* **Objective** - To implement a `PhoneBook` class which maps a `name` to many `phoneNumber` |
3 | | -* **Purpose** - To establish familiarity with Java's `Map` interface. |
| 1 | +# PhoneBook |
4 | 2 |
|
5 | | -## Modeling Abstraction in Software |
| 3 | +How does one start to think about `abstraction` in code? |
6 | 4 |
|
7 | | -Software developers use data structures to build abstractions that model real-world objects in their programs. For example, a phonebook can be modeled as a collection of key-value pairs, where the keys are names and the values are phone numbers. In Java, there are several common data structures that can be used to implement a phonebook, including arrays, ArrayLists, and HashMaps. |
| 5 | +## Understanding Abstraction Through the PhoneBook |
8 | 6 |
|
9 | | -Arrays are a simple data structure that can be used to store a fixed number of elements of the same type. However, arrays have several limitations, including a fixed size and the need to manually manage the elements in the array. |
| 7 | +As a beginner programmer, you're about to create your first meaningful abstraction - a PhoneBook. But what does that mean exactly? |
10 | 8 |
|
11 | | -ArrayLists are a more flexible data structure that can be used to store a variable number of elements of the same type. ArrayLists can grow or shrink dynamically as elements are added or removed, and they provide several methods for adding, removing, and accessing elements. |
| 9 | +Think about a real-world phone book: it's a collection of names paired with phone numbers, organized in a way that makes it easy to find someone's contact information. In programming, we can model this same concept using basic data structures like dictionaries (or HashMaps in Java) and lists. The magic happens when we wrap these simple structures inside a class that provides a clean, intuitive interface. |
12 | 10 |
|
13 | | -HashMaps are a key-value data structure that can be used to store a collection of key-value pairs. Each key in a HashMap must be unique, and it is used to retrieve the corresponding value. HashMaps provide several methods for adding, removing, and accessing elements, and they can be used to implement a phonebook by storing names as keys and phone numbers as values. |
| 11 | +When you implement the PhoneBook class, you're essentially building a specialized tool. Other programmers (including your future self) won't need to worry about whether you used a dictionary, how you store multiple phone numbers per person, or how you handle edge cases. They'll simply call methods like `add()`, `lookup()`, or `reverseLookup()` and trust that your implementation handles all the details correctly. This is the power of abstraction - hiding complexity behind a simple, understandable interface. |
14 | 12 |
|
15 | | -By using an object of type `Map` to implement the phonebook, you can easily add, remove, and retrieve entries in an efficient and flexible way. |
16 | | - |
17 | | -## Map Interface in Java |
18 | | - |
19 | | -In Java, the `Map` interface is used to represent a collection of key-value pairs. Each key in a Map must be unique, and it is used to retrieve the corresponding value. The Map interface provides several methods for adding, removing, and retrieving elements from the collection. |
20 | | - |
21 | | -Here are some of the key features of the Map interface: |
22 | | - |
23 | | -- Keys and values: A Map consists of a set of key-value pairs. The keys are used to retrieve the corresponding values. |
24 | | -- Unique keys: Each key in a Map must be unique. If you try to add a key that already exists in the Map, the existing value will be overwritten. |
25 | | -- No duplicate keys: Although values can be duplicated, keys cannot. Each key in a Map must be unique. |
26 | | -- Null keys and values: A Map can contain null keys and values. |
27 | | -- Iteration: You can iterate over the keys or values in a Map using the `keySet()`, `values()`, or `entrySet()` methods. |
28 | | - |
29 | | -Here is an example of how to use the Map interface in Java: |
30 | | - |
31 | | -```java |
32 | | -import java.util.HashMap; |
33 | | -import java.util.Map; |
34 | | - |
35 | | -public class MapExample { |
36 | | - public static void main(String[] args) { |
37 | | - // Create a new HashMap |
38 | | - Map<String, Integer> map = new HashMap<>(); |
39 | | - |
40 | | - // Add some key-value pairs to the map |
41 | | - map.put("Alice", 25); |
42 | | - map.put("Bob", 30); |
43 | | - map.put("Charlie", 35); |
44 | | - |
45 | | - // Retrieve a value from the map using a key |
46 | | - int age = map.get("Bob"); |
47 | | - System.out.println("Bob's age is " + age); |
48 | | - |
49 | | - // Iterate over the keys in the map |
50 | | - for (String name : map.keySet()) { |
51 | | - System.out.println(name + " is " + map.get(name) + " years old"); |
52 | | - } |
53 | | - } |
54 | | -} |
55 | | -``` |
56 | | - |
57 | | -In this example, a `HashMap` is used to store a collection of key-value pairs. |
58 | | -The `put()` method is used to add elements to the Map, and the `get()` method is used to retrieve a value from the Map using a key. |
59 | | -The `keySet()` method is used to iterate over the keys in the Map. |
| 13 | +By creating this PhoneBook module, you're not just solving a coding exercise. You're learning to think like a software architect, taking real-world concepts and translating them into reusable code components. Every major software system is built from layers of such abstractions, each one solving a specific problem so that higher-level code can focus on bigger challenges. |
60 | 14 |
|
61 | 15 | ## Getting Started |
62 | | -* Fork and clone this repository to complete your lab. Submit each part with a Pull Request for a separate branch. |
63 | 16 |
|
64 | | -## Instructions: |
| 17 | +This is a dual lab, one leg for java, one leg for python. |
65 | 18 |
|
66 | | -### Building a basic `PhoneBook` class |
67 | | -* Create a `PhoneBook` class that holds names and phone numbers. |
68 | | -* You can use an [associative data type](https://en.wikipedia.org/wiki/Associative_array) (one which stores items as keys paired with values). |
| 19 | +Check out the folder which applies to you. |
69 | 20 |
|
70 | | -* Your PhoneBook class should have the following method |
71 | | - * `void add(String name, String phoneNumber)` |
72 | | - * adds an entry to the composite associate data type |
73 | | - * `void addAll(String name, String... phoneNumbers)` |
74 | | - * adds many phone numbers to a single name entry |
75 | | - * `void remove(String name)` |
76 | | - * removes an entry to the composite associate data type |
77 | | - * `Boolean hasEntry(String name)` |
78 | | - * removes an entry to the composite associate data type |
79 | | - * `List<String> lookup(String name)` |
80 | | - * returns a phone number for the respective input `name` |
81 | | - * `String reverseLookup(String phoneNumber)` |
82 | | - * returns a name for the respective input `phoneNumber` |
83 | | - * `String getAllContactNames()` |
84 | | - * returns a list of all names in this `PhoneBook` |
| 21 | +Mkae your pull requests as you make progress and finish. |
0 commit comments