Question: Consider the following starter code for a StringBag class: public class StringBag { // the array to hold the data private String [] bag; //

Consider the following starter code for a StringBag class:

public class StringBag {

// the array to hold the data

private String [] bag;

// the number of elements being held (also the next index where a new

// String can be put).

private int count;

public StringBag(int length) {

bag = new String[length];

count = 0;

}

}

1) Write a function that returns the number of elements currently held in the StringBag.

Write your test case for this method:

public void testSize()

{

// Replace this test with your code

}

Write your method:

public int size()

{

// Replace this test with your code

}

2) Write a function that inserts a String into the StringBag. If there is space in the bag, then the new element should be put at the next unoccupied slot in the array (at the end) and the method should return true. If there is no space, dont add anything, but just return false.

Write your test case for this method:

@Test

public void testAdd()

{

// Replace this test with your code

}

Write your method:

public boolean add(String item)

{

// Replace this test with your code

}

3) Write a function that determines the index of a String in the StringBag. It should return the index if found, or -1 if it is not found.

Write your test case for this method:

@Test

public void testIndexOf()

{

// Replace this test with your code

}

Write your method:

public int indexOf(String item)

{

// Replace this test with your code

}

4) Write a function that determines if a String exists in the StringBag. It should return true if it does, and false if it does not.

Write your test case for this method:

@Test

public void testContains()

{

// Replace this test with your code

}

Write your method:

public boolean contains(String item)

{

// Replace this test with your code

}

5) Write a function that removes a String from the StringBag (if it is in the StringBag). It should do this by finding the index where the String exists, and then overwriting it with the element at the last position. The last element should then be dropped from the bag. If an element is removed, return true, otherwise return false.

Write your test case for this method:

@Test

public void testRemove()

{

// Replace this test with your code

}

Write your method:

public boolean remove(String item)

{

// Replace this test with your code

}

6) Write a function that produces a single comma-separated String with the contents of the StringBag concatenated together.

Write your test case for this method:

@Test

public void testToString()

{

// Replace this test with your code

}

Write your method:

public String toString(String item)

{

// Replace this test with your code

}

7) Write a function that reverses the ordering of the contents of the StringBag (e.g. an element that was at index 0 is now at index n-1, what was at index 1 is now at index n-2, etc.)

Write your test case for this method:

@Test

public void testReverse()

{

// Replace this test with your code

}

Write your method:

public void reverse()

{

// Replace this test with your code

}

8) It is likely that you wrote the above code so that if you tried to store a null string in the bag, something will crash with a NullPointerException at some point. Describe what you would need to do to your code in order to accommodate a StringBag that can hold nulls.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!