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 2
- Part A-2 3
Part B Assertions
Point A | Point B | Point C | Point D | Point E | |
---|---|---|---|---|---|
list.contains(num) | S | S | N | N | S |
list.size() > 0 | A | A | A | S | S |
count > 0 | S | A | A | S | S |
Part C Explain in Plain English
Option C “Removes all negative numbers from value Sets in the Map and removes all Map entries where the corresponding value Set only contained negative numbers.”
Question 2 - Tracing¶
[ [0, 1, 2], [3, 4, 5] ]
[ [4, 8], [23, 42], [23, 42] ]
[ [-9, 8, 14] ]
[ [5, 10, 15, 20], [12, 10, 15, 20], [12, 12, 18, 24] ]
Question 3 - Debugging¶
- Part A: Line 20 was the line causing the bug (not including the condition checking that there were enough items to remove).
- Part B: One possible fix to line 20 was to change the if condition to include
|| items.get(item) < count
.
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 24 was the source of the bug for Part A and for Part B to describe adding a separate condition after calculating numLeft
that threw the IllegalArgumentException
if numLeft
was negative.
Question 4 - Collections Programming¶
One possible solution appears below.
public Map<String, Set<String>> favoriteStudySpots
(Map<String, Map<String, Double>> ratings, double target) {
Map<String, Set<String>> result = new TreeMap<>();
for (String person : ratings.keySet()) {
result.add(person, new TreeSet<>());
Map<String, Double> ratingsFor = ratings.get(person);
for (String locn : ratingsFor.keySet()) {
if (ratingsFor.get(locn) >= target) {
result.get(person).add(locn);
}
}
}
return result;
}
Question 5 - Objects Programming¶
One possible solution appears below.
public class GroomingAppointment implements Appointment {
private Map<String, Integer> services;
private int month;
private int day;
private int hour;
private int minute;
private String customerName;
public GroomingAppointment(String name, int month, int day, int hour, int minute) {
if (month < 1 || month > 12
|| day < 1 || day > 31
|| hour < 1 || hour > 24
|| minute < 0 || min > 59) {
throw new IllegalArgumentException();
}
services = new HashMap<>();
this.month = month;
this.day = day;
this.hour = hour;
this.minute = minute
}
public void addService(String service, int mins) {
services.put(service, mins);
}
public int totalAppointmentTime() {
int total = 0;
for (String service : services.keySet()) {
total += services.get(service);
}
return total;
}
public String getDate() {
return this.month + "/" + this.day;
}
public String getTime() {
return this.hour + ":" + this.minutes;
}
public Set<String> getServices() {
return services.keySet();
}
public String toString() {
return this.name + "'s appointment scheduled for "
+ this.getDate() + " at " + this.getTime();
}
public boolean conflictsWith(Appointment other) {
String otherDate = other.getDate();
String otherTime = other.getTime();
return otherDate.equals(this.getDate()) && otherTime.equals(this.getTime());
}
}
Question 6 - Stacks & Queues Programming¶
One possible solution appears below.
public static void expand(Stack<Integer> s1, Stack<Integer> s2) {
if (s1.size() != s2.size()) {
throw new IllegalArgumentException();
}
Queue<Integer> q = new LinkedList<Integer>();
// merge the values into the q
while (!s1.isEmpty()) {
q.add(s2.pop());
q.add(s1.pop());
}
// split the values up and make duplicates as you go
// Variant: You could instead move one set of values back to the Q and then
// the Q and them move from Q->S later
while (!q.isEmpty()) {
int n2 = q.remove();
int n1 = q.remove();
for (int i = 0; i < n2; i++) {
s1.push(n1);
}
s2.push(n2);
}
// fix order
s2q(s2, q);
q2s(q, s2);
s2q(s1, q);
q2s(q, s1);
}
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 and you have satisfied the requirements of the problem (e.g., only using the permitted auxiliary data structures and permitted Stack and Queue methods), you should submit a regrade request.