Add the following operation to the Class StackClass: void reverseStack(StackClass otherStack); This operation copies the elements of a stack in reverse order onto another stack. Consider the following statements: StackClass stack1; StackClass stack2; The statement stack1.reverseStack(stack2); copies the elements of stack1 onto stack2 in the reverse order. That is, the top element of stack1 is the bottom element of stack2, and so on. The old contents of stack2 are destroyed and stack1 is unchanged. Write the definition of the method to implement the operation reverseStack. Also write a program to test the method reverseStack.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%
  1. Add the following operation to the Class StackClass:

void reverseStack(StackClass<T> otherStack);

This operation copies the elements of a stack in reverse order onto another stack.

Consider the following statements:

StackClass<T> stack1;

StackClass<T> stack2;

The statement

stack1.reverseStack(stack2);

copies the elements of stack1 onto stack2 in the reverse order. That is, the top element of stack1 is the bottom element of stack2, and so on. The old contents of stack2 are destroyed and stack1 is unchanged.

  1. Write the definition of the method to implement the operation reverseStack. Also write a program to test the method reverseStack.

In StackClass.java, please finish method - public void reverseStack(StackClass<T> otherStack) and testing main program

public class Problem53
{
public static void main(String[] args)
{
StackClass<Integer> intStack = new StackClass<Integer>();
StackClass<Integer> tempStack = new StackClass<Integer>();

//Please add your code here!
}
}

public interface StackADT<T>
{
public void initializeStack();
//Method to initialize the stack to an empty state.

public boolean isEmptyStack();

public boolean isFullStack();

public void push(T newItem) throws StackOverflowException;

// throws StackOverflowException.

public T peek() throws StackUnderflowException;

// throws StackUnderflowException;
// otherwise, a reference to the top
// element of the stack is returned.

public void pop() throws StackUnderflowException;
//Method to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.
// If the stack is empty, the method
// throws StackUnderflowException.
}

public class StackClass<T> implements StackADT<T>
{
private int maxStackSize; //variable to store the
//maximum stack size
private int stackTop; //variable to point to
//the top of the stack
private T[] list; //array of reference variables

//Default constructor
//Create an array of size 100 to implement the stack.
//Postcondition: The variable list contains the base
// address of the array, stackTop = 0,
// and maxStackSize = 100.
public StackClass()
{
maxStackSize = 100;
stackTop = 0; //set stackTop to 0
list = (T[]) new Object[maxStackSize]; //create the array
}//end default constructor

//Constructor with a parameter
//Create an array of size stackSize to implement the
//stack.
//Postcondition: The variable list contains the base
// address of the array, stackTop = 0,
// and maxStackSize = stackSize.
public StackClass(int stackSize)
{
if (stackSize <= 0)
{
System.err.println("The size of the array to "
+ "implement the stack must be "
+ "positive.");
System.err.println("Creating an array of size 100.");

maxStackSize = 100;
}
else
maxStackSize = stackSize; //set the stack size to
//the value specified by
//the parameter stackSize
stackTop = 0; //set stackTop to 0
list = (T[]) new Object[maxStackSize]; //create the array
}//end constructor

//Method to initialize the stack to an empty state.
//Postcondition: stackTop = 0
public void initializeStack()
{
for (int i = 0; i < stackTop; i++)
list[i] = null;

stackTop = 0;
}//end initializeStack

//Method to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty;
// otherwise, returns false.
public boolean isEmptyStack()
{
return (stackTop == 0);
}//end isEmptyStack

//Method to determine whether the stack is full.
//Postcondition: Returns true if the stack is full;
// otherwise, returns false.
public boolean isFullStack()
{
return (stackTop == maxStackSize);
}//end isFullStack

//Method to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem
// is added to the top of stack.
// If the stack is full, the method
// throws StackOverflowException
public void push(T newItem) throws StackOverflowException
{
if (isFullStack())
throw new StackOverflowException();

list[stackTop] = newItem; //add newItem at the
//top of the stack
stackTop++; //increment stackTop
}//end push

//Method to return a reference to the top element of
//the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the method
// throws StackUnderflowException;
// otherwise, a reference to the top
// element of the stack is returned.
public T peek() throws StackUnderflowException
{
if (isEmptyStack())
throw new StackUnderflowException();

return (T) list[stackTop - 1];
}//end peek

//Method to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.
// If the stack is empty, the method
// throws StackUnderflowException
public void pop() throws StackUnderflowException
{
if (isEmptyStack())
throw new StackUnderflowException();

stackTop--; //decrement stackTop
list[stackTop] = null;
}//end pop

public void reverseStack(StackClass<T> otherStack)
{
//Complete this method


}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education