JAVA: Suppose we define an IntArrayStack class that implements the interface Sta
ID: 3844795 • Letter: J
Question
JAVA:
Suppose we define an IntArrayStack class that implements the interface Stack<Integer >. (See exercise 306.) Our class has instance variables
private Integer [] theArray; // To hold the values in the stack
private int topIndex; // For an empty stack, topIndex = -1
final int maxSize; // the size of theArray after it gets allocated
Implement the one parameter constructor for IntArrayStack
// Initialize this.maxSize to maxSize or 2, whichever is bigger
// Allocate an Integer array of size this.maxSize and assign to theArray
// Initialize topIndex so that the stack is empty
public IntArrayStack(int maxSize) {
}
Explanation / Answer
Please find my code.
public IntArrayStack(int maxSize) {
if(maxSize >= 2)
this.maxSize = maxSize;
else
maxSize = 2;
// creating Integer array
theArray = new Integer[maxSize];
topIndex = 0; // initializing top variable
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.