This sample midterm contains the kinds of questions that will be on the actual midterm examinations. It is much longer than the total size of the all midterm questions will be. At least one question on the midterm will be all but identical to one here.
We will not post solutions to the sample midterm problems because, in our experience, people will be tempted to look at the solution before completely doing the problem. If anyone wishes to check their answers, they may give us a paper copy for review.
We will also answer questions by email, generally by giving hints or asking leading questions back.
AbstractCollection.
How is it able to implement almost all the required methods?
What can you say about the implementation?
Bag class to be generic. Name
at least one place where the word Bag remains just Bag
instead of changing to Bag<Item>.
T) can be used most places
where a class name can be used. Where can it not be used?
Why not?
Bag::contains, assuming Bag has the following fields:private int _count; private Coin contents[CAPACITY];Fill the code in the following skeleton:
private class Node {
public Coin data;
public Node next;
public Node(Coin c, Node n) { data = c; next = n; }
}
private Node head;
public Bag() { head = null; }
116 public void insert(CarControl element)
117 {
118 ensureCapacity(2*contents.length+1);
119
120 ++manyItems;
121
122 if (currentIndex == manyItems) {
123 contents[currentIndex] = element;
124 } else if (currentIndex == 0) {
125 for (int i=manyItems; i > 0; --i) {
126 contents[i] = contents[i-1];
127 }
128 } else {
129 for (int i=currentIndex; i < manyItems; ++i) {
130 contents[i+1] = contents[i];
131 }
132 }
133
134 contents[currentIndex] = element;
135
136 }
java.lang.ArrayIndexOutOfBoundsException: 1 at edu.uwm.cs351.DiskSeq.insert(DiskSeq.java:126) at Driver.testDiskSeq(Driver.java:182) at Driver.main(Driver.java:27) Initial tests Passed 112 tests. Failed 1 test.Your friend says ``Great! I failed only 1 out of 113 tests!'' You gently tell them ...[what?]
OutOfMemoryError. What is this error?
Why did the driver not catch it?
61 public void insert(T t) {
62 for (Node p = head; head != null; p = p.next)
63 if (p.data.compareTo(t) > 0) break;
64 if (p == head) p = new Node(t, head);
65 else p.next = new Node(t,p);
66 }
p is null.
There's a simple thing wrong with the loop header on line 62. What?
Fix it. (This doesn't fix the full problem, of course.)
p is null.
What happened? Fix the bug. This will require a bigger change.
1 class DoublyLinkedIntSeq {
2 private class Node {
3 int data;
4 Node prev, next;
5 Node(int d) { data = d; next = prev = null; }
6 };
7 private Node head, tail;
8 private Node current;
15 private boolean _wellFormed() { ... }
35 public DoublyLinkedIntSeq() {
36 head = tail = null;
37 assert _wellFormed() : "Invariant false after constructor";
38 }
41 public void insert(int v) {
42 assert _wellFormed() : "Invariant false before insert";
43 if (current == null) {
44 if (tail != null) {
45 Node n = new Node(v);
46 n.next = tail;
47 tail.next = n;
48 tail = tail.next;
49 } else {
50 head = tail = new Node(v);
51 }
52 current = tail;
53 } else {
54 Node p = new Node(v);
55 p.prev = current.prev;
56 p.next = current;
57 current.prev.next = p;
58 current.prev = p;
59 current = p;
60 }
61 assert _wellFormed() : "Invariant false after insert";
62 }
63
64 public void advance() { // no bugs here!
65 if (!hasCurrent()) throw new IllegalStateException("at end already");
66 current = current.next;
67 }
68
69 public boolean hasCurrent() { // no bugs here!
70 return current != null;
71 }
72
73 public void print() { // no bugs here!
74 for (Node p = head; p != null; p = p.next) {
75 if (p != head) System.out.print(",");
76 System.out.print(p.data);
77 }
78 System.out.println();
79 }
To test the code, we try the following test:
82 DoublyLinkedIntSeq a = new DoublyLinkedIntSeq();
83 a.insert(112);
84 a.insert(66);
85 a.print();
This code crashes on line 57:
Exception in thread "main" java.lang.NullPointerException
at DoublyLinkedIntSeq.insert(DoublyLinkedIntSeq.java:57)
at DoublyLinkedIntSeq.main(DoublyLinkedIntSeq.java:75)
112. What should be on line
57? (Fix the whole bug)
87 DoublyLinkedIntSeq b = new DoublyLinkedIntSeq();
88 b.insert(42);
89 b.advance();
90 b.insert(88);
91 b.print();
Whether or not the first bug (b) is fixed, this will print
42,8842,8842,8842,... ad infinitum. Why? Explain!
_wellFormed() but it didn't get a chance.
What is the job of wellFormed() and What problem could
it have detected? Why doesn't it always
run? Explain the reasoning behind this convention!