java program For this question, you must implement the class (static) method boo
ID: 3849765 • 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 s1 and s2 contain the same elements, in the same order, but with elements 2,4,6,. . . missing in s2. Given s1 designating a stack containing the elements 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(s1, s2) returns true.
• Both stacks, designated by s1 and s2, must remain unchanged following a call to the method isSkipped. Specifically, given s1 and s2, two reference variables designating stacks, following the call isSkipped(s1, s2), s1 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, s1 and s2, will not be null.
• An empty stack is considered the skipped version of another empty stack.
• The parameters of the method isSkipped are of type Stack, which is an interface.
For this question, there is an interface named Stack:
• Notice that the parameter of the method push and the return value of the method pop are of type int.
• Assume the existence of DynamicStack, which implements the interface Stack. It has one constructor and its signature is DynamicStack().
• You cannot use arrays to store temporary data.
• You must use objects of the class DynamicStack() to store temporary data
• You do not know anything about the implementation of DynamicStack. In particular, you do not know if it uses an array or not.
• You can assume that DynamicStack can store an arbitrarily large number of elements.
public interface Stack public abstract void push (int item); public abstract int pop public abstract boolean isEmptyExplanation / 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();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.