public class FinalReview { // For Comparable programming, you would write this whole class on its own public class Office implements Comparable { private double width; private double length; private boolean couch; private int windows; public Office (int width, int length, boolean couch, int windows) { this.width = width; this.length = length; this.couch = couch; this.windows = windows; } public String toString() { // Common bug: Formatting errors in text returned String s = "width: " + width + ", length: " + length + ", windows: " + windows; if (couch) { s += ", has a couch"; } return s; } public boolean isCorner() { return length == width && windows == 2; } public int compareTo (Office other) { // Common bug: Returning a double value or trying to cast a double if (length * width < other.length * other.width) { return -1; } else if (length * width > other.length * other.width) { return 1; } else { if (windows != other.windows) { return other.windows - windows; } else { if (couch && !other.couch) { return -1; } else if (!couch && other.couch) { return 1; } else { return 0; } } } } } // For Collections programming, you would just write this method public void recordGrade(Map> grades, String id, double grade, String course) { // Common Bug: Forgetting to make a data structure for a new entry if (!grades.containsKey(id)) { grades.put(id, new TreeMap<>()); } Map next = grades.get(id); // Common Bug: Not checking if there is an entry for this course (i.e., just calling get) if (next.containsKey(course)) { grade = Math.max(grade, next.get(course)); } next.put(course, grade); } // For LinkedIntList programming, the method would be added to the LinkedIntList class // A good exercise to understand how this works is to draw out a picture and verifies // if it works by hand! public LinkedIntList removeFirstOf3() { // Common Bug: Mixing up LinkedIntList and ListNode LinkedIntList other = new LinkedIntList(); if (front != null) { // Common Bug: Forgetting empty case // Set up front of other list, keep reference to end of list so we can add there other.front = front; ListNode back1 = other.front; // Update our front to remove first value, current will be pointing to // second node in original group front = front.next; ListNode current = front; // Common Bug: Having a slightly wrong loop condition that doesn't check all // the nulls it needs while (current != null && current.next != null && current.next.next != null) { current = current.next; // Move this node to the end of the other list back1.next = current.next; back1 = back1.next; // Update current to be in correct next position current.next = current.next.next; current = current.next; } // Common Bug: Forgetting to set fields to null if necessary back1.next = null; } // Common Bug: Forgetting to return return other; } public boolean bubble() { boolean swap = false; // Common bug: Skipping a length 0 and length 1 list if (front != null && front.next != null) { // Common bug: Forgetting to handle front case if applicable if (front.data > front.next.data) { swap = true; ListNode temp = front; front = front.next; temp.next = front.next; front.next = temp; } ListNode current = front; while (current.next != null && current.next.next != null) { if (current.next.data > current.next.next.data) { swap = true; ListNode temp = current.next.next; current.next.next = temp.next; temp.next = current.next; current.next = temp; } current = current.next; } } return swap; } // For Binary Tree programming, the method would be added to the IntTree class // A good exercise to understand how this works is to draw out a picture and verifies // if it works by hand! public void completeToLevel(int target) { if (target < 1) { throw new IllegalArgumentException(); } overallRoot = complete(overallRoot, target, 1); } private IntTreeNode complete(IntTreeNode root, int target, int level) { if (level <= target) { // Commnon Bug: Mixing up logic and not realizing we don't want an else branch for recursion if (root == null) { root = new IntTreeNode(-1); } // Common Bug: Forgetting x=change(x) pattern if it's necessary root.left = complete(root.left, target, level + 1); root.right = complete(root.right, target, level + 1); } return root; } }