Single-page summary of the patterns that recur across F19 PracticeAPE flavors. Use this for the night-before review; deeper material lives in the flavors and integration leaves.
Wednesday, July 8 in-lab APE covers three topics: linked lists (the heaviest part), file I/O, and selection sort, under exam conditions. For interactive practice on these exact patterns, use the practice drills.
public final class Foo { // final unless designed for inheritance
private final String name; // private final fields
public Foo(final String name) { // final params
if (name == null || name.isBlank())
throw new IllegalArgumentException( // IAE preconditions
"Foo: name must be non-null, non-blank, got " + name);
this.name = name;
}
public String getName() { return name; } // accessor
@Override public boolean equals(final Object o) { ... } // always
@Override public int hashCode() { ... } // pair with equals
@Override public String toString() { ... } // always
}
public abstract class Base {
private final String label;
protected Base(final String label) { /* validate; assign */ }
public final String getLabel() { return label; }
public abstract double compute();
@Override public String toString() { return getClass().getSimpleName() + "[...]"; }
// equals/hashCode using getClass()
}
public final class Concrete extends Base { /* override compute, super in equals */ }
// Natural order via Comparable<T>
@Override public int compareTo(final Foo that) {
return Integer.compare(this.id, that.id); // never `this.id - that.id`
}
// Alternative ordering
Comparator<Foo> byName = Comparator.comparing(Foo::getName);
Comparator<Foo> byNameThenId = Comparator
.comparing(Foo::getName)
.thenComparingInt(Foo::getId);
list.sort(byNameThenId);
public class MyLinkedList<T> {
private static final class Node<T> { T value; Node<T> next; /* ... */ }
private Node<T> head;
private int size;
public void addAt(final int index, final T item) {
if (index < 0 || index > size) throw new IndexOutOfBoundsException(...);
if (index == 0) { /* prepend */ } else { /* walk to index-1; splice */ }
size++;
}
public T get(final int index) {
if (index < 0 || index >= size) throw new IndexOutOfBoundsException(...);
Node<T> cur = head; for (int i = 0; i < index; i++) cur = cur.next; return cur.value;
}
}
private final Node<T> dummy = new Node<>(null);
public void addAt(final int index, final T item) {
if (index < 0 || index > size) throw new IndexOutOfBoundsException(...);
Node<T> prev = dummy;
for (int i = 0; i < index; i++) prev = prev.next; // dummy = prev when index = 0
prev.next = new Node<>(item, prev.next);
size++;
}
public class SortedList<T extends Comparable<T>> { // recursive bound
public void add(final T item) {
// item.compareTo(...) is now legal
}
}
final on fields, parameters, and leaf classes (when not designed for inheritance)Class: field must be ..., got <value> message formatObjects.requireNonNull or explicit null checks, never silent NPE@Override on every overrideInteger.compare, Double.compare, never a - bComparator.comparing chains, not hand-written if/else ladders== for object equality (lane 07 concept 12)catch blocksArrayList, Comparable without <T>)throw new RuntimeException("error") with no detail messageprintStackTrace() and continueinstanceof first| Module | Skill | Key idiom |
|---|---|---|
recursion |
Helper-pattern recursion, no loops | findLargest(arr, i+1, max(largest, arr[i])) |
data-interfaces |
Multi-field Comparable | int c = a.compareTo(b); if (c != 0) return c; chain |
data-inheritance |
Multi-level abstract hierarchy | Cylinder.area() = 2 * super.area() + lateral |
linkedlist-basic |
addAll / clone | New nodes; never share node identity |
linkedlist-advanced |
cutList(int) |
Clamp index, splice, update both sizes |
sorting |
DH-LL hand-rolled sort | Iterate head.next; swap data fields, not nodes |
files |
InputFile/OutputFile clients | Open → loop → close |
Authoritative current reference: Spring23APE at /Users/jessicadoner/1-teaching/cscd210/sources/12-related-cs1/cscd211-general/Spring23APE/.
Historical context: F19.PracticeAPE-{General, Inheritance, Interface, LinkedList-DH, LinkedList-NDH}; S20.Lab14-Old_APE. No problem text reproduced.