Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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