Answered step by step
Verified Expert Solution
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 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started