| CIT594 Midterm Exam Spring 2006 |
Name_________________________________________ |
Please keep all your answers short and to the point. Do not provide extra information
that was not asked for.
| You want to do the following: | What interfaces, if any, must your Employee class implement? | What methods must your Employee class implement? (Show the complete signature.) |
|---|---|---|
|
Use Employee objects as keys in a HashMap |
None | public boolean equals(Object o) public int hashCode() |
| Keep a SortedSet of Employee
objects |
Comparable (or use Comparator) | public int compareTo(T o) |
| Be able to sort Employee objects by either
their name field or their EmployeeId field |
None (but its Comparator must implement |
None |
| w | |||
| ux | wy | ||
| wyyy |
public boolean term() {
if (!term()) return false; Recursion is not with a simpler case.
while (multiplyOperator()) {
if (!term()) error("No term after '+' or '-'");
}
return true; Also accepted: no base case, left recursive, infinite recursion
}
![]() |
|
int findFive(final int ARRAY_SIZE, int[] array) {
for (int i = 0; i < ARRAY_SIZE; i++) {
if (array[i] == 5) return i;
}
return -1; O(1), or constant time; found on average after 5 probes.
}
int log2(int n) {
int result = 0;
int power = 1;
while (power < n) {
result++;
power = 2 * power; log2(n)
}
return result;
}
| (best) |
|
(worst)
|
| Collection | boolean add(E o) | Iterator<E> iterator() |
|---|---|---|
| boolean addAll(Collection<? extends E> c) | boolean remove(Object o) | |
| void clear() | boolean removeAll(Collection<?> c) | |
| boolean contains(Object o) | boolean retainAll(Collection<?> c) | |
| boolean containsAll(Collection<?> c) | int size() | |
| boolean equals(Object o) | Object[] toArray() | |
| int hashCode() | <T> T[] toArray(T[] a) |
|
| boolean isEmpty() | ||
| Set | Just those declared in Collection. | |
| SortedSet | Comparator<? super E> comparator() | SortedSet<E> headSet(E toElement |
| E first() | SortedSet<E> tailSet(E fromElement) | |
| E last() | SortedSet<E> subSet(E fromElement, E toElement) | |
| List | void add(int index, E element) | ListIterator<E> listIterator() |
| boolean addAll(int index, Collection<? extends E> c) | ListIterator<E> listIterator(int index) | |
| E get(int index) | E remove(int index) | |
| int indexOf(Object o) | E set(int index, E element) | |
| int lastIndexOf(Object o) | List<E> subList(int fromIndex, int toIndex) | |
| Map | void clear() | boolean isEmpty() |
| boolean containsKey(Object key) | Set<K> keySet() | |
| boolean containsValue(Object value) | V put(K key, V value) | |
| Set<Map.Entry<K,V>> entrySet() | void putAll(Map<? extends K,? extends V> t) | |
| boolean equals(Object o) | V remove(Object key) | |
| V get(Object key) | int size() | |
| int hashCode() | Collection<V> values() | |
| SortedMap | Comparator<? super K> comparator() | K lastKey() |
| K firstKey() | SortedMap<K,V> subMap(K fromKey, K toKey) | |
| SortedMap<K,V> headMap(K toKey) | SortedMap<K,V> tailMap(K fromKey) | |
| Bubble sort |
Everything on the right end of the array is in the correct place. |
|---|---|
| Insertion sort |
All elements on the left end of the array are sorted with respect
to one another |
| Selection sort |
Everything on the left end of the array is in the correct place. |
|
Schlemiel gets a job as a street painter, painting the dotted lines down the middle of the road. On the first day he takes a can of paint out to the road and finishes 300 yards of the road. "That's pretty good!" says his boss, "you're a fast worker!" and pays him a kopeck. The next day Schlemiel only gets 150 yards done. "Well, that's not nearly as good as yesterday, but you're still a fast worker. 150 yards is respectable," and pays him a kopeck. The next day Schlemiel lpaints 30 yards of the road. "Only 30!" shouts his boss. "That's unacceptable! On the first day you did ten times that much work! What's going on?" "I can't help it," says Schlemiel. "Every day I get farther and farther away from the paint can!" |