X789: 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 class ArrayBag<T> implements BagInterface<T> {
    private T[] contents;
    private static final int MAX = 25;
    private int numberOfEntries;

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.