-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathExercise.java
More file actions
117 lines (96 loc) · 4.15 KB
/
Exercise.java
File metadata and controls
117 lines (96 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.booleanuk.core;
import com.booleanuk.helpers.ExerciseBase;
import java.util.ArrayList;
import java.util.HashMap;
public class Exercise extends ExerciseBase {
/*
The final fundamental building block of Java is a Map. There is still much to learn about the language,
but this component will allow you to start building lots of more complex pieces of software.
A map is like a list, except we have control over what we use for an item's index. This allows us to use
types other than an incrementing integer to describe the positions of the data contained in the structure.
Like Lists, Maps have a variety of different implementations. We'll focus on the HashMap in this exercise.
We create a HashMap like this:
HashMap<K, V>
K is where you'd put the data type of the key for an item, V is the data type of the value.
If we wanted to map a persons details (their first name, last name, occupation etc.), we could use
a HashMap using a String key and a String value like so:
HashMap<String, String>
*/
/*
Spend some time understanding the method below
- It creates a HashMap of String, String key value pairs
- It adds some keys with values
- Under the hood, the Map would look like this: { "firstName": "Nathan", "lastName": King, etc. }
- It returns the HashMap
https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Map.html
*/
public HashMap<String, String> createPerson() {
HashMap<String, String> map = new HashMap<>();
map.put("firstName", "Nathan");
map.put("lastName", "King");
map.put("occupation", "Software Developer");
return map;
}
/*
TODO: 1. Create a method named getValue that accepts one parameter:
- a string key
The method must return the value associated to the provided key from the HashMap created
in the createPerson method
*/
public String getValue(String key) {
return createPerson().get(key);
}
/*
TODO: 2. Create a method named hasKey that accepts two parameters:
- A HashMap of String, String key value pairs
- A string
The method must return a boolean that represents whether the string provided exists as a key
in the provided HashMap
*/
public boolean hasKey(HashMap<String, String> map, String key) {
return map.containsKey(key);
}
/*
TODO: 3. Create a method named getValueOrDefault that accepts two parameters:
- A HashMap of String, Integer key value pairs
- A string
The method must use the string provided to return the integer contained in the provided HashMap,
or -1 if the string provided is not a key in the HashMap
*/
public int getValueOrDefault(HashMap<String, Integer> map, String key) {
if (map.containsKey(key) == true) {
return map.get(key);
} else {
return -1;
}
}
/*
TODO: 4. Complete the method below
Example input & output:
.
input output
[42, 6712, 7] | ArrayList<String> ["universe", "bass", "muse"]
[23, 19, 96, 23, 165] | ArrayList<String> ["chicken", "nice", "chicken", "soup"]
[918, 71, 88] | ArrayList<String> []
*/
public ArrayList<String> buildSecretPhrase(ArrayList<Integer> numbers) {
// Do not modify the map
HashMap<Integer, String> map = new HashMap<>();
map.put(23, "chicken");
map.put(42, "universe");
map.put(165, "soup");
map.put(6712, "bass");
map.put(7, "muse");
map.put(96, "nice");
// Write your code below this comment...
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < numbers.size(); i++) {
if(map.get(numbers.get(i)) != null) {
list.add(map.get(numbers.get(i)));
}
}
// ...and above this comment
// Change the return statement below to return your actual ArrayList
return list;
}
}