Q1 java : If in the ArrayStack, the first element that we push to the stack, is stored in location capacity – 1 (capacity is the capacity of the array theData) in the array theData and the subsequent elements that we push, we keep on adding before the last element added in the array. That is, in the constructor of class ArrayStack, we initialize the topOfStack to capacity and we write the push method as follows: public E push(E obj) { if (topOfStack == 0) reallocate(); topOfStack--; theData[topOfStack] = obj; return obj; } This push method can work but the if statement should be: if (topOfStack == theData.length - 1) reallocate(); This push method will work provided we modify the reallocate method. This push method can work, but topOfStack-- ; should come after theData[topOfStack] = obj; This push method can work, but topOfStack-- ; should be topOfStack++;
Q1
java :
If in the ArrayStack, the first element that we push to the stack, is stored in location capacity – 1 (capacity is the capacity of the array theData) in the array theData and the subsequent elements that we push, we keep on adding before the last element added in the array. That is, in the constructor of class ArrayStack, we initialize the topOfStack to capacity and we write the push method as follows:
public E push(E obj)
{
if (topOfStack == 0)
reallocate();
topOfStack--;
theData[topOfStack] = obj;
return obj;
}
-
This push method can work but the if statement should be:
if (topOfStack == theData.length - 1) reallocate();
-
This push method will work provided we modify the reallocate method.
-
This push method can work, but topOfStack-- ; should come after
theData[topOfStack] = obj;
-
This push method can work, but topOfStack-- ; should be topOfStack++;
Step by step
Solved in 2 steps