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 { private Object[] value; private int currentSize; private int limitation; private int index; private static int initialCapacity = 10;

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 xBox = new ourNewStack<>(); xBox.push(100); xBox.push(200); xBox.push(300); while(xBox.getSize() > 0) { System.out.println(xBox.pop()); }

ourNewStack yBox = new ourNewStack<>(3); String tmp = (String)yBox.peek(); yBox.push("AAA"); yBox.push("BBB"); yBox.push("CCC"); yBox.push("DDD"); System.out.println(yBox.peek()); System.out.println(yBox.pop()); System.out.println(yBox.pop()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!