Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public interface Stack { public void push (T value): public T pop (): } public c

ID: 3886769 • Letter: P

Question

public interface Stack { public void push (T value): public T pop (): } public class LinkedStack implements Stack { private Node first = null: public void push (T value) { Node n = new Node (value): n. next = first: first = n: } public T pop () { if (first = null) { throw new IllegalAccessException(): } T value = first. value: first = first. next: return value: } } public class FixedArrayStack implements Stack { private T [] items: private int top = 0: public FixedArrayStack (int size) { items = (T [])new Object [size]: } public void push (T value) { items [top] = value: top ++: } public T pop () { top --: T value = items [top]: items [top] = null: return value: } } public static main (String [] args) { Stack stack: if (args [0] = 'ARRAY') { stack = new FixedArrayStack (args. length): } else { Look at the push method in FixedArrayStack class, What do you think? will throw an exception in some cases will always throw an exception looks perfectly fine the method is syntactically wrong

Explanation / Answer

Answer is a) will throw an exception in some cases.

The issue is push method implementation is ok but it is not having the size check. In constructor we have initialized array withe size size. So array can not have elements more than size.

So after pushing size number of elements, it will throw ArrayIndexOutofBoundException as at that instance we will be trying to push element at position > size.

So there must be a check in push to check whether array size is equal to capacity or not.