Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider Bloch's non - generic Stack implementation; public class Stack { private Object [ ] elements; private int size = 0 ; private static final

Consider Bloch's non-generic Stack implementation;
public class Stack {
private Object[] elements;
private int size =0;
private static final int DEFAULT_INITIAL_CAPACITY =16;
public Stack(){
this.elements = new Object[DEFAULT_INITIAL_CAPACITY];
}
public void push (Object e){
ensureCapacity();
elements[size++]= e;
}
public Object pop (){
if (size ==0) throw new IllegalStateException("Stack.pop");
Object result = elements[--size];
elements[size]= null; // Eliminate obsolete reference
return result;
}
private void ensureCapacity(){
if (elements.length == size){
Object oldElements[]= elements;
elements = new Object[2*size +1];
System.arraycopy(oldElements,0, elements, 0, size);
}
}
}
1.Rewrite Stack to be immutable. Keep the representation variables elements and size.
2.Do the right thing with push().
3.Do the right thing with pop().
4.Get rid of anything that doesn't make sense in an immutable data type.
Please avoid the use of AI.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How do books become world of wonder?

Answered: 1 week ago