X681: LinkedBag ToString

For this question assume the following code:

x
 
1
2
public class LinkedBag<T> implements BagInterface<T>{
3
    private Node firstNode;
4
    private int numberOfEntries;
5
6
7
    public LinkedBag() {
8
      firstNode = null;
9
      numberOfEntries = 0;
10
    }
11
12
13
    public int getCurrentSize(){
14
       return numberOfEntries;
15
    }
16
}
17
18
19
public class  Node<T> {
20
    private T data;
21
    private Node<T> next;
22
    public Node(T dataPortion, Node<T> nextNode) {
23
        data = dataPortion;
24
        next = nextNode;
25
    } // end constructor
26
    public Node(T dataPortion) {
27
        this(dataPortion, null);
28
    } // end constructor
29
    public Node<T> getNext() {
30
        return next;
31
    }
32
    public void setNext(Node<T> newNext) {
33
        next = newNext;
34
    }
35
    public T getData() {
36
        return data;
37
    }
38
    public void setData(T data) {
39
        this.data = data;
40
    }
41
}

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

An empty bag should return: ""

Your Answer:

xxxxxxxxxx
4
 
1
public String toString () {
2
  
3
}
4

Feedback

Your feedback will appear here when you check your answer.