The private instance variables of the class include an array named for storing a StringBag data collection of objects. Each object also has an integer named to maintain the StringBag String n number of meaningful elements that are in the The and StringBag. add occurencesOf methods will need both instance variables to accomplish their responsibilities. The constructor establishes an empty object by setting to zero. The array capacity is set to the StringBag n arbitrary initial capacity of 10. We know how big the collection will grow to when used don’t will deal with that later. laterwe public class StringBag { private String[] data; // Stores the collection private int n; // Current number of elements // Construct an empty StringBag object public StringBag() { n = 0; data = new String[10]; // Initial capacity is 10 } public void add(String stringToAdd) Both and must be available to the method. This is not a problem, since any n data add method has access to the private instance variables of To add an StringBag StringBag. element to the the argument reference passed to the parameter can StringBag, stringToAdd be placed at the "end" of the array, or more specifically, at the first available array location. This two-step algorithm summarizes how a new is added to the first available array position: String Adding an element Algorithm: the-argument-passed-to-StringBag.add data[n] = increment by n +1 The argument passed to method is stored into the proper array location using StringBag’s n add as the index. Then gets incremented by 1 to reflect the new addition. Incrementing by 1 n n maintains the number of elements in the StringBag. Incrementing also conveniently sets up a situation where the next added element is inserted n into the proper array location. The array location at is the next place to store the next data[n] element can be placed. This is demonstrated in the following view of the state of the StringBag before and after the string after this code executes "and a fourth" StringBag bag = new StringBag(); bag.add("A string"); bag.add("Another string"); bag.add("and still another");
The private instance variables of the StringBag class include an array named data for storing a collection of String objects. Each StringBag object also has an integer named n to maintain the number of meaningful elements that are in the StringBag. The add and occurencesOf methods will need both instance variables to accomplish their responsibilities. The constructor establishes an empty StringBag object by setting n to zero. The array capacity is set to the arbitrary initial capacity of 10. We don’t know how big the collection will grow to when used laterwe will deal with that later. public class StringBag { private String[] data; // Stores the collection private int n; // Current number of elements // Construct an empty StringBag object public StringBag() { n = 0; data = new String[10]; // Initial capacity is 10 } public void add(String stringToAdd) Both n and data must be available to the add method. This is not a problem, since any StringBag method has access to the private instance variables of StringBag. To add an element to the StringBag, the argument reference passed to the stringToAdd parameter can be placed at the "end" of the array, or more specifically, at the first available array location. This two-step algorithm summarizes how a new String is added to the first available array position: Algorithm: Adding an element data[n] = the-argument-passed-to-StringBag.add increment n by +1 The argument passed to StringBag’s add method is stored into the proper array location using n as the index. Then n gets incremented by 1 to reflect the new addition. Incrementing n by 1 maintains the number of elements in the StringBag. Incrementing n also conveniently sets up a situation where the next added element is inserted into the proper array location. The array location at data[n] is the next place to store the next element can be placed. This is demonstrated in the following view of the state of the StringBag before and after the string "and a fourth" after this code executes StringBag bag = new StringBag(); bag.add("A string"); bag.add("Another string"); bag.add("and still another");