Question: Hi my question is for the Java code below it is a generic class and I'm needing help with finishing the constructor (I believe I
Hi my question is for the Java code below it is a generic class and I'm needing help with finishing the constructor (I believe I need the second constructor for strings also but it isn't working yet). Also I've added the getSize() method but it isn't working yet either, thanks for any help!
public class ourNewStack
public ourNewStack() { currentSize = 0; index = 0; value = new Object[initialCapacity]; limitation = initialCapacity; } // TODO: finish constructor public ourNewStack(int s) { if(s <= 0) s = initialCapacity;
value = new Object[s]; }
public ourNewStack(String S) { if(S <= 0) S = initialCapacity;
value = new Object[S]; } // TODO: finish push() method public void push(T item) { Object[index] = item; ++index; ++currentSize; }
// TODO: finish pop() method public Object pop(T item) { if(currentSize == 0) { System.out.println("empty stack, no pop"); return null; } int temp = Object[index - 1]; --index; --currentSize; return temp; }
// TODO: finish other methods public Object peek() { if(currentSize == 0) { System.out.println("Stack is empty, no peek"); return -1; } return ourNewStack<>(index - 1); }
public Object getSize() { return currentSize; }
public static void main(String[] args) { ourNewStack
ourNewStack
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
