{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Midterm 2 Solutions 13

Nuumk ohtakduunuunuunuurald

Info icon This 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern