Java: Two stacks are the same if they have the same size and their elements at the corresponding positions are the same. Add the method equalStack to Class StackClass that takes as a parameter a StackClass object, say otherStack and return true if the stack is the same as otherStack. Also write the definition of method equalStack and a program to test your method. In StackClass.java, please finish method - public boolean equalStack(StackClass otherStack) and testing main program public class Problem52 { public static void main(String[] args) { StackClass intStack = new StackClass(50); StackClass tempStack = new StackClass(50); //Please add your code here! } } public class StackClass implements StackADT { 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 boolean equalStack(StackClass otherStack) { //Please add your code here! } //end equalStack } public class StackException extends RuntimeException { public StackException() { } public StackException(String msg) { super(msg); } } public class StackOverflowException extends StackException { public StackOverflowException() { super("Stack Overflow"); } public StackOverflowException(String msg) { super(msg); } } public class StackUnderflowException extends StackException { public StackUnderflowException() { super("Stack Underflow"); } public StackUnderflowException(String msg) { 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

Java: Two stacks are the same if they have the same size and their elements at the corresponding positions are the same. Add the method equalStack to Class StackClass that takes as a parameter a StackClass object, say otherStack and return true if the stack is the same as otherStack. Also write the definition of method equalStack and a program to test your method. In StackClass.java, please finish method - public boolean equalStack(StackClass<T> otherStack) and testing main program

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

//Please add your code here!

}
}

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 boolean equalStack(StackClass<T> otherStack)
{
//Please add your code here!

} //end equalStack
}

public class StackException extends RuntimeException
{
public StackException()
{
}

public StackException(String msg)
{
super(msg);
}
}

public class StackOverflowException extends StackException
{
public StackOverflowException()
{
super("Stack Overflow");
}

public StackOverflowException(String msg)
{
super(msg);
}
}

public class StackUnderflowException extends StackException
{
public StackUnderflowException()
{
super("Stack Underflow");
}

public StackUnderflowException(String msg)
{
super(msg);
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 8 images

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