Question: 1. Write a recursive (not iterative) Java method public Boolean sameStack(StackArray s2) to test whether two stacks contain the same elements. The elements stored in

  1. 1. Write a recursive (not iterative) Java method public Boolean

sameStack(StackArray s2)

to test whether two stacks contain the same elements. The elements stored in the stack are integers.

One Stack object calls this method with a second stack object, from inside the main()

stack1.sameStack(stack2);

Then put it to this program:

package ArrayImplementations; public class StackArray { private int top=-1; private static final int MAX_ITEMS = 10; private E items[]; @SuppressWarnings("unchecked") public StackArray() { items = (E[]) new Object[MAX_ITEMS]; System.out.println("Stack Created!"); } public void push(E e) { if (isFull()==true) { System.out.println("Stack Full!"); } else{ top=top+1; items[top] = e; } } public E pop() { if (isEmpty()==true) { System.out.println("Stack Empty!"); } else{ E e = (E) items[top]; items[top] = null; top = top-1; return e; } return null; } public boolean isFull() { if (top == items.length-1) { return true; } return false; } public boolean isEmpty(){ if (top==-1) { return true; } return false; } @Override public String toString() { System.out.println("Array:"); System.out.print("{"); for(int i = 0; i < items.length ;i++) { System.out.print(items[i]+" "); } System.out.print("}"); return ""; } public static void main(String[] args) { // Code reference for countPosNeg method StackArray intStack = new StackArray(); intStack.push(10); intStack.push(10); intStack.push(30); intStack.push(-40); System.out.println("intStack before counting"); System.out.println(intStack); // call countPosNeg here System.out.println("intStack after counting"); System.out.println(intStack); // Code reference for sameStack method StackArray stack = new StackArray(); StackArray stack2 = new StackArray(); stack.push(10); stack.push(20); stack.push(30); stack.push(40); stack2.push(10); stack2.push(20); stack2.push(30); stack2.push(40); System.out.println(stack); System.out.println( stack2); //Calling comparison method // stack.sameStack(stack2); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!