See the criteria on Gradescope for how grades of E/S/N were assigned for each problem.
Question 1 - Conceptual¶
Part A: Count the objects/references
- Part A-1 1 or 2. The intended answer was 2, but we also accepted 1 since it was ambiguous how you should consider an object that has no references to it (technically it will get “garbage collected” at some indeterminate point in the future)
- Part A-2 5
Part B Assertions
Point A | Point B | Point C | Point D | Point E | |
---|---|---|---|---|---|
list.contains(num) | S | S | A | S | N |
list.size() > 0 | S | A | A | A | A |
count > 0 | N | N | S | S | S |
Part C Explain in Plain English
Option D “Moves all multiword strings from s to a returned list in reverse alphabetical order”
Question 2 - Tracing¶
{1=[9.0, 1.5]}
{0=[0.0, 1.0, 2.0, 0.0], 3=[0.0, 1.0, 2.0, 0.0], 4=[]}
{1=[3.3, 2.5 1.5, 3.0], 2=[3.3, 2.5 1.5, 3.0], 3=[3.3, 2.5 1.5, 3.0]}
{1=[3.3, -0.7, 0.0], 1=[3.1, 3.99, 1.5, 3.0], 2=[3.1, 3.99, 1.5, 3.0], 3=[3.1, 3.99, 1.5, 3.0]}
Question 3 - Debugging¶
- Part A: Line 5 was the line causing the bug (including the wrong indices for the sum).
- Part B: One possible fix to line 5 is to change the statemtent to
if (j <= i) {
although there are other equivalent statements (e.g.,!(j > i)
).
Note that we accepted other possible answers for Part A and B if the explanation for Part B was consistent with the answer in Part A and it fixed the bug. For example, another common answer was to identify that line 4 was the source of the bug for Part A and for Part B to describe removing the if
statement on line 5 and changing the for loop bound to for (int j = 0; j <= i; j++) {
. Technically Line 5 is still the buggy line (since it needs to be removed in this answer), but we accepted this answer for an E since it also fixes the bug.
Question 4 - Collections Programming¶
One possible solution appears below. Note that we accepted List
or Sets
as the value types.
public Map<String, List<String>> commonHobbies(Map<String, List<String>> tas) {
Map<String, List<String>> result = new TreeMap<>();
for (String ta : tas.keySet()) {
for (String hobby : tas.get(ta)) {
if (!result.containsKey(hobby)) {
result.put(hobby, new ArrayList<>());
}
result.get(hobby).add(ta);
}
}
return result;
}
Question 5 - Objects Programming¶
One possible solution appears below.
public class IceCreamCone implements IceCream {
private int totalScoops;
private Map<String, Integer> scoops;
public IceCream() {
totalScoops = 0;
scoops = new HashMap<>();
}
public void add(String flavor, int n) {
if (n < 1) { // OK to be n <= 1 for E/S
throw new IllegalArgumentException();
}
if (scoops.containsKey(flavor)) {
scoops.put(flavor, scoops.get(flavor) + n)
} else {
scoops.put(flavor, n);
}
totalScoops += n;
}
public int getFlavor(String flavor) {
if (scoops.containsKey(flavor)) {
return scoops.get(flavor);
} else {
return 0;
}
}
public Set<String> getFlavors() {
return scoops.keySet();
}
public String toString() {
if (totalScoops == 0) {
return "No ice cream :(";
} else {
return totalScoops + " scoops of ice of ice cream with " +
scoops.keySet();
}
}
public boolean sameFlavors(IceCream other) {
return scoops.keySet().equals(other.getFlavors());
}
}
Question 6 - Stacks & Queues Programming¶
One possible solution appears below.
public static void separate(Queue<String> q) {
Stack<String> s = new Stack<>();
int size = q.size();
// Move length 1 to stack
for (int i = 0; i < size; i++) {
String str = q.remove();
if (str.length() == 1) {
s.push(str);
} else {
q.add(str);
}
}
size = q.size();
// Move length 2 to stack
for (int i = 0; i < size; i++) {
String str = q.remove();
if (str.length() == 2) {
s.push(str);
} else {
q.add(str);
}
}
// Move length 3 (the rest) to stack
q2s(q, s);
// Reverse contents of everything
s2q(s, q);
q2s(q, s);
s2q(s, q);
}
If you want to check if your solution works, please use this program to test out your solution. If your solution works as you wrote it without modifying the logic of your solution and you did not receive an E, you should submit a regrade request.