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

java program For this question, you must implement the class (static) method boo

ID: 3854095 • Letter: J

Question

java program

For this question, you must implement the class (static) method boolean isSkipped (Stack s1, Stack s2) » The method isSkipped returns true if and only if the stacks designated by sl and s2 contain the same elements, in the same order, but with elements . . missing in s2. Given sl designating a stack containing the ele- ments 1,2,3,4,5,6,7, where 7 is the top element, and s2 designating a stack containing the elements 1,3,5,7, where 7 is the top element, isSkipped(sl 2,4,6 s2) returns true » Both stacks, designated by sl and s2, must remain unchanged following a call to the method isSkipped. Specifically, given sl and s2, two refer- ence variables designating stacks, following the call isSkipped(sl, s2), sl contains the same elements, in the same order, as it did before the call to isSkipped. Similarly, s2 contains the same elements, in the same order, as it did before the call to isSkipped » You can assume that both, sl and s2, will not be null » An empty stack is considered the skipped version of another empty stack » The parameters of the mcthod isSkipped are of type Stack, which is an interface. For this question, there is an interface named Stack

Explanation / Answer

I tried many ways to implement isSkipped(). But I could not.

Considering my knowledge, this implementing isSkipped() cannot be done. Because we have just push() and pop() methods. If we use these methods in isSkipped() method(ofcourse, we have to use them because we have only them), we will end up with a stack that is changed(this is not we want to happen).

But I implemented DynamicStack class such a way you really want by following all constraints(not using arrays,using DynamicStack objects).

DynamicStackDriver has main(). run it.

DynamicStackDriver.java

public class DynamicStackDriver
{
   public static void main(String a[])
   {
       DynamicStack d1 = new DynamicStack();
       d1.setItemValue(1);
      
       d1.push(2);
       d1.push(3);
       d1.push(4);
       d1.push(5);
       System.out.println();
       d1.pop();
       d1.pop();
       d1.pop();
       d1.push(100);
       System.out.println("Top Element:"+d1.top());
       System.out.println("Size of Stack:"+d1.size());
   }
   public static boolean isSkipped(Stack s1,Stack s2)
   {
       return false;
   }
}

DynamicStack.java
class DynamicStack implements Stack
{
   DynamicStack next;
   private int itemValue;
   private int size=1;
   DynamicStack()
   {
       next=null;
   }
   public void push(int item)
   {
       DynamicStack d = new DynamicStack();
       d.setItemValue(item);
       DynamicStack temp = this;
       System.out.print("Stack Contents:");
       while(temp.next!=null)
       {
           System.out.print(" "+temp.getItemValue());
           temp = temp.next;
       }
       System.out.print(" "+temp.getItemValue());
       temp.next=d;
       System.out.print(" "+temp.next.getItemValue());
       System.out.println();
       size++;
   }
   public int pop()
   {
       size--;
       if(next==null)
       return this.getItemValue();
       DynamicStack temp = this,parent=null;
       while(temp.next!=null)
       {
           //System.out.print(" "+temp.getItemValue());
           parent = temp;
           temp = temp.next;
       }
       int data = temp.getItemValue();
       if(parent!=null)
       parent.next = null;
       return data;
      
   }
   public int top()
   {
       if(next==null)
       return this.getItemValue();
       DynamicStack temp = this;
       while(temp.next!=null)
       {
           //System.out.print(" "+temp.getItemValue());
           temp = temp.next;
       }
       return temp.getItemValue();
   }
   public int size()
   {
       return this.size;
   }
   public boolean isEmpty()
   {
       return false;
   }
   public void setItemValue(int item)
   {
       this.itemValue = item;
   }
   public int getItemValue()
   {
       return itemValue;
   }
}

Stack.java

public interface Stack{
   public abstract void push(int item);
   public abstract int pop();
   public abstract boolean isEmpty();
}