Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Web browsers commonly allow you to navigate through a \"history\" of web pages t

ID: 3907231 • Letter: W

Question

Web browsers commonly allow you to navigate through a "history" of web pages that have previously been visited. The mechanism is somewhat like a stack, in that the most recently visited pages are at the top of the history and revisited when the "back" button is pressed.

However, the history does not necessarily have unbounded capacity. In reality, there may exist a fixed limit on the size of the history. The issue arises as to what should happen when the capacity is exhausted and a new item is pushed onto the stack. One possibility is to throw an exception. But this is not how a Web browser behaves. If it only has room to save 50 pages in its history and yet you visit more pages, it will make room in the history for a new page by throwing away the page that is on the very bottom of the history (i.e., the least recently visited page). The formal Stack interface does not help, as it gives us no way to directly access or remove the object on the bottom of the stack.

Your Task:

Implement the following MyLeakyStack<E> class using array.

public class MyLeakyStack<E> implements Stack<E> {

   @Override
   public int size() {
       // TODO Auto-generated method stub
       return 0;
   }

   @Override
   public boolean isEmpty() {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public void push(E e) {
       // TODO Auto-generated method stub
      
   }

   @Override
   public E top() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public E pop() {
       // TODO Auto-generated method stub
       return null;
   }

}

Explanation / Answer

public class MyLeakyStack implements Stack { private Object[] arr; private int count; public MyLeakyStack() { arr = new Object[]; count = 0; } @Override public int size() { return count; } @Override public boolean isEmpty() { return count == 0; } @Override public void push(E e) { if(count == arr.length) { Object[] copy = new Object[2 * count]; for(int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote