Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A StringBag is a container that holds String objects. Consider the following starter code for a StringBag class. a. [2 points] Write a function that

A StringBag is a container that holds String objects. Consider the following starter code for a StringBag class. a. [2 points] Write a function that returns the number of strings that currently held in the StringBag (not the total capacity of the bag). i. Write your test case for this method. @Test public void testSize() { StringBag testBag = new StringBag(2); assertEquals(2,testBag.Size()); } ii. Write your method. public int size() { return this.count; } b. [2 points] 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. i. Write your test case for this method. @Test public void testAdd(){ StringBag testBag = new StringBag(2); String testString = test; assertTrue(testBag.add(testString)); } ii. Write your method. public boolen add(String item) { for (i=0; i < this.count; i++) { if (this.bag[i] == null) { this.bag[i] = item; return true; } } return false; } c. [2 points] 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. i. Write your test case for this method. @Test public void testIndexOf() { StringBag testBag = new StringBag(1); testBag.add("test") assertEquals(0,testBag.indexOf("test")); } public int indexOf(String item) { for (i=0; i < this.count; i++) { if (item.equals(bag[i])) { return i; } return -1; } ii. Write your method. d. [2 points] 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. i. Write your test case for this method. @Test public void testContains() { // Replace this test with your code } ii. Write your method. public boolean contains(String item) { // Replace this test with your code } e. [2 points] 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. i. Write your test case for this method. @Test public void testContains(){ StringBag testBag = new StringBag(1); testBag.add("test"); assertTrue(testBag.contains("test")); } ii. Write your method. public boolean remove(String item) { // Replace this test with your code } f. [2 points] Write a function that produces a single comma-separated String with the contents of the StringBag concatenated together. i. Write your test case for this method. @Test public void testToString() { // Replace this test with your code } ii. Write your method. public String toString() { // Replace this test with your code } g. [2 points] 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.) i. Write your test case for this method. @Test public void testReverse() { // Replace this test with your code } ii. Write your method. public void reverse() { // Replace this test with your code }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions