Question: In java please, provided code. public class GenericStack { private java.util.ArrayList list = new java.util.ArrayList(); public int getSize() { return list.size(); } public E peek()

In java please, provided code.

public class GenericStack { private java.util.ArrayList list = new java.util.ArrayList(); public int getSize() { return list.size(); } public E peek() { return list.get(getSize() 1); } public void push(E o) { list.add(o); } public E pop() { E o = list.get(getSize() 1); list.remove(getSize() 1); return o; } public boolean isEmpty() { return list.isEmpty(); } public String toString() { return "stack: " + list.toString(); } }

In java please, provided code. public class GenericStack { private java.util.ArrayList list= new java.util.ArrayList(); public int getSize() { return list.size(); } public E

Revise the GenericStack class in Listing 19.1 to implement the Stack using an array rather than an ArrayList. You will need to create an array with a generic type and also an int variable to keep track of how many elements are in the stack. You may use the code below as a starting point. In the above code, the array is called elements and with the variable size to keep a count of how many elements in the stack. You should check the array size before adding content to the stack - the push() method. If the array is full, create a new array that double the size of the current array and copy elements from current array to the new array. Hence, you need to finish up the code above by implementing the following methods: peek ( ) pop () push ( ) isempty () 1 and getsize () method. This method should return whatever the value in size is. Write your test code to have a stack of Strings and a stack of integers

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!