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...


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;


 }















  1. This push method can work but the if statement should be:


      if (topOfStack == theData.length - 1) reallocate();













  2. This push method will work provided we modify the reallocate method.













  3. This push method can work, but topOfStack-- ; should come after


     theData[topOfStack] = obj;













  4. This push method can work, but topOfStack-- ; should be topOfStack++;










Jun 02, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here