Question: Consider Bloch's non - generic Stack implementation; public class Stack { private Object [ ] elements; private int size = 0 ; private static final
Consider Bloch's nongeneric Stack implementation;
public class Stack
private Object elements;
private int size ;
private static final int DEFAULTINITIALCAPACITY ;
public Stack
this.elements new ObjectDEFAULTINITIALCAPACITY;
public void push Object e
ensureCapacity;
elementssize e;
public Object pop
if size throw new IllegalStateExceptionStackpop";
Object result elementssize;
elementssize null; Eliminate obsolete reference
return result;
private void ensureCapacity
if elementslength size
Object oldElements elements;
elements new Objectsize ;
System.arraycopyoldElements elements, size;
Rewrite Stack to be immutable. Keep the representation variables elements and size.
Do the right thing with push
Do the right thing with pop
Get rid of anything that doesn't make sense in an immutable data type.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
