Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

In problem, solve each inequality. Graph the solution set. 2 6 2 1

Answered: 1 week ago

Question

Compare levels of resolution in conflict outcomes?

Answered: 1 week ago

Question

Strategies for Managing Conflict Conflict Outcomes?

Answered: 1 week ago