X357: ArrayBag toString

Write the toString member method that returns the String representation of the items in the bag in the form [item, item, item, item]. For example, if the following operations were performed on the bag

bag1.add("H"); 
bag1.add("E"); 
bag1.add("L");
bag1.add("L"); 
bag1.add("O");

then the toString should return "[H, E, L, L, O]".
Note that each item (except the last item) is followed by a comma and a space.The member fields your method implementations may access/change are:

public static final class ArrayBasedStack<T> implements StackInterface<T>{
    private T[] contents;
    private int numberOfEntries;
    private int capacity;
}

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.