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. In StackClass.java, please finish method - public void reverseStack(StackClass otherStack) and testing main program – Problem53.java.  Note: java programming, please use given outline code public class Problem53{ publicstaticvoidmain(String[] args){ StackClass intStack = newStackClass(); StackClass tempStack = newStackClass();   } }      public interface StackADT { publicvoidinitializeStack();   publicbooleanisEmptyStack(); //Method to determine whether the stack is empty. // Returns true if the stack is empty;   publicbooleanisFullStack(); //Method to determine whether the stack is full. //Postcondition: Returns true if the stack is full;   publicvoidpush(TnewItem) throwsStackOverflowException; //Method to add newItem to the stack //The stack exists and is not full //The stack is changed and newItem // is added to the top of stack // If the stack is full, the method // throws StackOverflowException publicTpeek() throwsStackUnderflowException; //Method to return a reference to the top element of //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 publicvoidpop() throwsStackUnderflowException; //Method to remove the top element of the stack //The stack exists and is not empty //The stack is changed and the top // element is removed from the stack // throws StackUnderflowException }   public class StackClass implements StackADT { privateintmaxStackSize;  privateintstackTop;  privateT[] list;    //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. publicStackClass() { maxStackSize = 100; stackTop = 0;  list = (T[]) new Object[maxStackSize];  } //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 publicStackClass(intstackSize) { 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;  stackTop = 0;  list = (T[]) new Object[maxStackSize];  }   publicvoidinitializeStack() { for (inti = 0; i < stackTop; i++) list[i] = null; stackTop = 0; } //Method to determine whether the stack is empty. //Returns true if the stack is empty; // otherwise, returns false. publicbooleanisEmptyStack() { return (stackTop == 0); } //Method to determine whether the stack is full //Returns true if the stack is full; // otherwise, returns false publicbooleanisFullStack() { return (stackTop == maxStackSize); } //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. // throws StackOverflowException publicvoidpush(TnewItem) throwsStackOverflowException { if (isFullStack()) thrownewStackOverflowException(); list[stackTop] = newItem;  stackTop++;  } //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; // element of the stack is returned. publicTpeek() throwsStackUnderflowException { if (isEmptyStack()) thrownewStackUnderflowException(); return (T) list[stackTop - 1]; } //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. // throws StackUnderflowException publicvoidpop() throwsStackUnderflowException { if (isEmptyStack()) thrownewStackUnderflowException(); stackTop--;  list[stackTop] = null; } publicvoidreverseStack(StackClass otherStack) {     } }     public class StackException extends RuntimeException { publicStackException() { } publicStackException(Stringmsg) { super(msg); } }     public class StackOverflowException extends StackException { publicStackOverflowException() { super("Stack Overflow"); } publicStackOverflowException(Stringmsg) { super(msg); } }     public class StackUnderflowException extends StackException { publicStackUnderflowException() { super("Stack Underflow"); } publicStackUnderflowException(Stringmsg) { super(msg); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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.

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 – Problem53.java. 

Note: java programming, please use given outline code

public class Problem53{
publicstaticvoidmain(String[] args){
StackClass<Integer> intStack = newStackClass<Integer>();
StackClass<Integer> tempStack = newStackClass<Integer>();
 
}
 
 
public interface StackADT<T>
{
publicvoidinitializeStack();
 
publicbooleanisEmptyStack();
//Method to determine whether the stack is empty.
// Returns true if the stack is empty;
 
publicbooleanisFullStack();
//Method to determine whether the stack is full.
//Postcondition: Returns true if the stack is full;
 
publicvoidpush(TnewItem) throwsStackOverflowException;
//Method to add newItem to the stack
//The stack exists and is not full
//The stack is changed and newItem
// is added to the top of stack
// If the stack is full, the method
// throws StackOverflowException

publicTpeek() throwsStackUnderflowException;
//Method to return a reference to the top element of
//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

publicvoidpop() throwsStackUnderflowException;
//Method to remove the top element of the stack
//The stack exists and is not empty
//The stack is changed and the top
// element is removed from the stack
// throws StackUnderflowException
}
 
public class StackClass<T> implements StackADT<T>
{
privateintmaxStackSize; 
privateintstackTop; 
privateT[] list; 
 
//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.
publicStackClass()
{
maxStackSize = 100;
stackTop = 0; 
list = (T[]) new Object[maxStackSize]; 
}
//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
publicStackClass(intstackSize)
{
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; 
stackTop = 0; 
list = (T[]) new Object[maxStackSize]; 
}
 
publicvoidinitializeStack()
{
for (inti = 0; i < stackTop; i++)
list[i] = null;

stackTop = 0;
}
//Method to determine whether the stack is empty.
//Returns true if the stack is empty;
// otherwise, returns false.
publicbooleanisEmptyStack()
{
return (stackTop == 0);
}

//Method to determine whether the stack is full
//Returns true if the stack is full;
// otherwise, returns false
publicbooleanisFullStack()
{
return (stackTop == maxStackSize);
}

//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.
// throws StackOverflowException
publicvoidpush(TnewItem) throwsStackOverflowException
{
if (isFullStack())
thrownewStackOverflowException();

list[stackTop] = newItem; 
stackTop++; 
}

//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;
// element of the stack is returned.
publicTpeek() throwsStackUnderflowException
{
if (isEmptyStack())
thrownewStackUnderflowException();

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

//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.
// throws StackUnderflowException
publicvoidpop() throwsStackUnderflowException
{
if (isEmptyStack())
thrownewStackUnderflowException();

stackTop--; 
list[stackTop] = null;
}

publicvoidreverseStack(StackClass<T> otherStack)
{
 
 
}
}
 
 
public class StackException extends RuntimeException
{
publicStackException()
{
}

publicStackException(Stringmsg)
{
super(msg);
}
}
 
 
public class StackOverflowException extends StackException
{
publicStackOverflowException()
{
super("Stack Overflow");
}

publicStackOverflowException(Stringmsg)
{
super(msg);
}
}
 
 
public class StackUnderflowException extends StackException
{
publicStackUnderflowException()
{
super("Stack Underflow");
}

publicStackUnderflowException(Stringmsg)
{
super(msg);
}
}
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY