X376: LinkedBag ToString

For this question assume the following code:


public class LinkedBag<T> implements BagInterface<T>{
    private Node firstNode;
    private int numberOfEntries;
    private Node currentNode;

    public LinkedBag() {
      firstNode = null;
      numberOfEntries = 0;
      currentNode = firstNode;
    }

    public LinkedBag() {
       firstnode = null;
       numberOfEntries = 0;
       currentnode = firstnode;
    }
    public int getCurrentSize(){
       return numberOfEntries;
    }
}


public static class  Node<T> {
    private T data;
    private Node<T> next;
    public Node(T dataPortion, Node<T> nextNode) {
        data = dataPortion;
        next = nextNode;
    } // end constructor
    public Node(T dataPortion) {
        this(dataPortion, null);
    } // end constructor
    public Node<T> getNext() {
        return next;
    }
    public void setNext(Node<T> newNext) {
        next = newNext;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
}

Given the following method signature, write a toString method that will print out the contents of the linked bag:

For example, calling toString on a linked bag that contained "A", "B", and "C" would return "[A,B,C]"

You will not have access to any of the methods defined in BagInterface

Reset

Practice a different Java exercise