@@ -48,7 +48,7 @@ public HashMap<String, String> createPerson() {
4848 in the createPerson method
4949 */
5050
51-
51+ public String getValue ( String key ) { return createPerson (). get ( key );}
5252
5353 /*
5454 TODO: 2. Create a method named hasKey that accepts two parameters:
@@ -58,7 +58,7 @@ public HashMap<String, String> createPerson() {
5858 in the provided HashMap
5959 */
6060
61-
61+ public boolean hasKey ( HashMap < String , String > map , String key ) { return map . containsKey ( key );}
6262
6363 /*
6464 TODO: 3. Create a method named getValueOrDefault that accepts two parameters:
@@ -68,7 +68,11 @@ public HashMap<String, String> createPerson() {
6868 or -1 if the string provided is not a key in the HashMap
6969 */
7070
71-
71+ public int getValueOrDefault (HashMap <String , Integer > map , String key ) {
72+ if (map .containsKey (key ))
73+ return map .get (key );
74+ return -1 ;
75+ }
7276
7377 /*
7478 TODO: 4. Complete the method below
@@ -90,12 +94,17 @@ public ArrayList<String> buildSecretPhrase(ArrayList<Integer> numbers) {
9094 map .put (96 , "nice" );
9195 // Write your code below this comment...
9296
97+ ArrayList <String > phrase = new ArrayList <>();
9398
99+ for (Integer num : numbers ){
100+ if (map .containsKey (num ))
101+ phrase .add (map .get (num ));
102+ }
94103
95104
96105 // ...and above this comment
97106
98107 // Change the return statement below to return your actual ArrayList
99- return new ArrayList < String >() ;
108+ return phrase ;
100109 }
101110}
0 commit comments