Nuumk ohtakduunuunuunuurald

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ce “Queue” with “Stack” and “enqueue” with “push”) QeemQ=nwTakduu(; uu y e rceQee) Sse.u.rnl(y.aSzSFr); ytmotpitnmQmxieoa() / opl­ieerr a’ idmxieoa nQee /Cmietm ro:cntfn aSzSFri uu Takduu y e uu(; rceQeemQ=nwQee) Sse.u.rnl(y.aSzSFr); ytmotpitnmQmxieoa() / opl­ieerr ttctp sasbls ftednmctp /Cmietm ro:sai yei ucaso h yai ye QeemQ=nwTakduu(; uu y e rceQee) mQeqee“IE) y.nuu(MK”; / ohTakduu.nuu( n uu.nuu( r ald /Bt rceQeeeqee)adQeeeqee)aecle Common Mistakes: For the first part, the most common mistake was getting it completely wrong by saying that there’s no error. The error is in the second line ­­ remember that Java checks the static class during compile­time, and the dynamic class during run­time. Since myQ is of static type Queue and Queues don’t have maxSizeSoFar(), there’s a compile­time error. For the second part, the most common mistake was being confused about run­time error vs compile time error. Or rather, they detected the run­time error in the second line if the first line was allowed to compile. However, the first line doesn’t compile because saying that a Queue “is­a” TrackedQueue is false—not all Queues are TrackedQueues. Java will complain about “incompatible types”. In the third part, most people didn’t realize that both TrackedQueue’s enqueue() andQueue’s enqueue() are called. First, TrackedQueue’s enqueue() method is called, which, if you implemented 1a correctly makes a call to super.enqueue(). Question 2: Sample Solution (Version A): A, Enqueue: linear time; need to free up a slot by shifting all elements by 1 slot. A, Dequeue: constant time; simply call ArrayList.remove(N­1). Sample Solution (Version B): A, Enqueue: constant time; simply call ArrayList.add() at the back of the queue A, Dequeue: linear time; remove the first element, then shift all other elements by 1 slot. Sample Solution (Both Versions): B, Enqueue: constant time; adding to the front of a linked list takes constant tim...
View Full Document

Ask a homework question - tutors are online