Question: public int size() { if (rest == null) return 1; return 1 + rest.size(); } Given the following StringStack wrapper class: public class StringStack {
public int size() { if (rest == null) return 1; return 1 + rest.size(); } Given the following StringStack wrapper class:
public class StringStack { private InternalStringStack stack; public StringStack() { // Create an empty stack stack = null; } public void push(String s) { stack = new InternalStringStack(s, stack); } public String top() { if (stack == null) return null; return stack.getData(); } } Which of the following implementations of size will work for the StringStack wrapper class? The size method should return the number of items (String objects) in the represented stack structure and should work correctly for empty and non-empty stacks.
public int size() { return stack.size(); } |
public int size() { return stack.size() + 1; } |
public int size() { if (stack == null) return 0; return stack.size(); } |
public int size() { if (stack == null) return 0; return stack.size() + 1; } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
