Question: java problems Question 1 For this question, you must implement the class (static) method: boolean isSkipped ( Stack s1 , Stack s2 ) The method
java problems
Question 1
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 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(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 refer- ence 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:
1
public interface Stack { public abstract void push( int item ); public abstract int pop(); public abstract boolean isEmpty ();
}
3
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.
Question 2
Consider the implementation of the class CircularQueue below. Given a queue designated by q and containing the following elements: A, B, C, D, E, F, G, where A is the front element of the queue, following the call, q.magic(4), what will be the content of the queue?
(a) E, F, G (b) A,B,C,D
(c) E,F,G,A,B,C,D (d) E,F,G,A,B,C,D,A,B,C,D
(e) None of the above
Answer:
public class CircularQueue
public CircularQueue(int capacity) { if (capacity < 0) {
throw new IllegalArgumentException(negative number); 2
}
elem s = new E[capacity]; front = 1; rear = 1;
}
public void magic(int n) { if (rear != 1 && rear != front) {
} }
}
while (n > 0) { E current = elems[front];
elems [ front ] = null ; front = (front + 1) % elems.length; rear = (rear + 1) % elems.length; elems[rear] = current;
n; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
