package lab03stringbag; /** * Lab 3 String Bag - tester for Lab 3 * Programmed b
ID: 3667415 • Letter: P
Question
package lab03stringbag;
/**
* Lab 3 String Bag - tester for Lab 3
* Programmed by Stephen Brower
* Inspired by Michael Main
* @author Stephen T. Brower
*/
public class StringBagSimpleTest
{
/**
* The main method is the program's starting point.
* @param args the command line arguments
*/
public static void main(String[] args)
{
StringBag fruitBag = new StringBag(3);
System.out.println(" Display Bag upon startup:");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
fruitBag.add("Pear");
fruitBag.add("Plum");
fruitBag.add("Peach");
System.out.println(" Display Bag after 3 adds");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
fruitBag.add("Mango");
System.out.println(" Display bag after 1 additional add");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// does banana exist?
if (fruitBag.exists("banana"))
System.out.println(" Yes! There is a banana in the bag!");
else
System.out.println(" No! No banana in the bag");
// does plum exist?
if (fruitBag.exists("plum"))
System.out.println(" Yes! There is a plum in the bag!");
else
System.out.println(" No! No plum in the bag");
System.out.println(" Number of banana? " + fruitBag.countOccurrences("banana"));
System.out.println(" Number of plum? " + fruitBag.countOccurrences("plum"));
// attempt to remove banana
if (fruitBag.remove("banana"))
System.out.println(" Was able to remove banana.");
else
System.out.println(" Sorry! unable to remove banana!");
System.out.println(" Display Bag after removing banana?");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove plum
if (fruitBag.remove("plum"))
System.out.println(" Was able to remove plum.");
else
System.out.println(" Sorry! unable to remove plum!");
System.out.println(" Display Bag after removing plum");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
}
}
//below is the code for the extra credit
package lab03stringbag;
/**
* Lab 3 String Bag - Extra Credit tester for Lab 3
* Programmed by Stephen Brower
* Inspired by Michael Main
* @author Stephen T. Brower
*/
public class StringBagSimpleTestExtraCredit
{
/**
* The main method is the program's starting point.
* @param args the command line arguments
*/
public static void main(String[] args)
{
StringBag fruitBag = new StringBag(9);
System.out.println(" Display Bag upon startup:");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
fruitBag.add("Pear");
fruitBag.add("Banana");
fruitBag.add("Plum");
fruitBag.add("watermellon");
fruitBag.add("Grapefruit");
fruitBag.add("Peach");
fruitBag.add("apple");
fruitBag.add("Mellon");
fruitBag.add("macintosh");
System.out.println(" Display Bag after 9 adds");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
fruitBag.add("Mango");
System.out.println(" Display bag after 1 additional add");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// does pomegranate exist? (non existant)
if (fruitBag.exists("pomegranate"))
System.out.println(" Yes! There is a pomegranate in the bag!");
else
System.out.println(" No! No pomegranate in the bag");
// does macintosh exist?
if (fruitBag.exists("macintosh"))
System.out.println(" Yes! There is a macintosh in the bag!");
else
System.out.println(" No! No macintosh in the bag");
System.out.println(" Number of pomegranate? " + fruitBag.countOccurrences("pomegranate")); // (non existant)
System.out.println(" Number of macintosh? " + fruitBag.countOccurrences("macintosh"));
// attempt to remove pomegranate (non existant)
if (fruitBag.remove("pomegranate"))
System.out.println(" Was able to remove pomegranate.");
else
System.out.println(" Sorry! unable to remove pomegranate!");
System.out.println(" Display Bag after removing pomegranate?");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove macintosh (middle)
if (fruitBag.remove("macintosh"))
System.out.println(" Was able to remove macintosh.");
else
System.out.println(" Sorry! unable to remove macintosh!");
System.out.println(" Display Bag after removing macintosh");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove apple (first)
if (fruitBag.remove("apple"))
System.out.println(" Was able to remove apple.");
else
System.out.println(" Sorry! unable to remove apple!");
System.out.println(" Display Bag after removing apple");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove watermellon (last)
if (fruitBag.remove("watermellon"))
System.out.println(" Was able to remove watermellon.");
else
System.out.println(" Sorry! unable to remove watermellon!");
System.out.println(" Display Bag after removing watermellon");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
}
}
Explanation / Answer
StringBagSimpleTestExtraCredit.java
public class HelloWorld
{
public static void main(String[] args)
{
StringBag fruitBag = new StringBag(9);
System.out.println(" Display Bag upon startup:");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
fruitBag.add("Pear");
fruitBag.add("Banana");
fruitBag.add("Plum");
fruitBag.add("watermellon");
fruitBag.add("Grapefruit");
fruitBag.add("Peach");
fruitBag.add("apple");
fruitBag.add("Mellon");
fruitBag.add("macintosh");
System.out.println(" Display Bag after 9 adds");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
fruitBag.add("Mango");
System.out.println(" Display bag after 1 additional add");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// does pomegranate exist? (non existant)
if (fruitBag.exists("pomegranate"))
System.out.println(" Yes! There is a pomegranate in the bag!");
else
System.out.println(" No! No pomegranate in the bag");
// does macintosh exist?
if (fruitBag.exists("macintosh"))
System.out.println(" Yes! There is a macintosh in the bag!");
else
System.out.println(" No! No macintosh in the bag");
System.out.println(" Number of pomegranate? " + fruitBag.countOccurrences("pomegranate")); // (non existant)
System.out.println(" Number of macintosh? " + fruitBag.countOccurrences("macintosh"));
// attempt to remove pomegranate (non existant)
if (fruitBag.remove("pomegranate"))
System.out.println(" Was able to remove pomegranate.");
else
System.out.println(" Sorry! unable to remove pomegranate!");
System.out.println(" Display Bag after removing pomegranate?");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove macintosh (middle)
if (fruitBag.remove("macintosh"))
System.out.println(" Was able to remove macintosh.");
else
System.out.println(" Sorry! unable to remove macintosh!");
System.out.println(" Display Bag after removing macintosh");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove apple (first)
if (fruitBag.remove("apple"))
System.out.println(" Was able to remove apple.");
else
System.out.println(" Sorry! unable to remove apple!");
System.out.println(" Display Bag after removing apple");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove watermellon (last)
if (fruitBag.remove("watermellon"))
System.out.println(" Was able to remove watermellon.");
else
System.out.println(" Sorry! unable to remove watermellon!");
System.out.println(" Display Bag after removing watermellon");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
}
}
StringBag.java
public class StringBag
{
//region FIELDS
private String[] data; //the data being stored
private int numElements; //the number of elements in the actual array
/* No Arg constructor. Initializes to 10 empty slots in the array.*/
public StringBag()
{
//default is 10 slots, no elements
data = new String[10];
numElements = 0;
}
/* Initializes the StringBag with the given capacity as empty slots.*/
public StringBag(int initCapacity)
{
data = new String[initCapacity];
numElements = 0;
}
//endregion
/* Enlarges the array to be twice its current size plus 1. (2 * length + 1) */
private void enlargeArray()
{
//create a new enlarged array
String[] newArray = new String[2 * data.length + 1];
//copy the data
for (int x = 0; x < numElements; x++)
{
newArray[x] = data[x];
}
//reference the new array
data = newArray;
}
/* Returns the capacity of the current StringBag instance.*/
public int getCapacity()
{
return data.length;
}
/* Returns the number of elements currently stored in the array.*/
public int getSize()
{
return numElements;
}
/* Determines whether or not the StringBag holds the given String.*/
public boolean exists(String target)
{
boolean targetFound = false;
//loop through the data and return true if a match to target is found
for (int x = 0; x < numElements && !targetFound; x++)
{
if (data[x].equalsIgnoreCase(target))
{
targetFound = true;
}
}
return targetFound;
}
/*
Counts the number of times the target string appears in StringBag*/
public int countOccurrences(String target)
{
int occurrences = 0;
//loop through the data and tally the occurrences
for (int x = 0; x < numElements ; x++)
{
if (data[x].equalsIgnoreCase(target))
{
occurrences++;
}
}
return occurrences;
}
//endregion
/*
Adds a string to the StringBag.*/
public void add(String newElement)
{
//check the capacity of the array
if (numElements == data.length)
{
enlargeArray();
}
boolean spotFound = false;
//loop through the array and compare the newElement alphabetically to find a spot
for (int x = 0; x < numElements && !spotFound; x++)
{
//if compareTo is negative then newElement is alphabetically first, and must be placed here
if (data[x].compareToIgnoreCase(newElement) > 0)
{
spotFound = true;
//move the other elements in data up 1 space
for (int y = numElements; y >= x + 1; y--)
{
data[y] = data[y-1];
}
data[x] = newElement;
numElements++;
}
}
//if no spot found, add it to the end
if (!spotFound)
{
data[numElements++] = newElement;
}
}
/* Removes a string from the StringBag. */
public boolean remove(String target)
{
boolean targetFound = false;
//loop through the data and compare to target
for (int x = 0; x < numElements && !targetFound ; x++ )
{
if (data[x].equalsIgnoreCase(target))
{
targetFound = true;
//a match was found, decrease the number of elements
numElements--;
//shift the remaining elements down
for (int y = x; y < numElements; y++)
{
data[y] = data[y + 1];
}
//clear the last element
data[numElements] = "";
}
}
return targetFound; //if we get this far, a match was not found
}
//endregion
/* Convert contents of the StringBag into one string.*/
public String toString()
{
//first check to see if the StringBag is empty
if (numElements == 0)
{
return "Empty.";
}
//create a container for the return string
String returnString = "";
//add each string in the StringBag to the returnString separated by comma (or period for last element)
for (int x = 0; x < numElements ; x++)
{
returnString += data[x];
if (x == numElements - 1)
{
returnString += ".";
}
else
{
returnString += ", ";
}
}
//return the result
return returnString;
}
//endregion
}
StringBagSimpleTest.java
public class StringBagSimpleTest
{
/**
* The main method is the program's starting point.*/
public static void main(String[] args)
{
StringBag fruitBag = new StringBag(3);
System.out.println(" Display Bag upon startup:");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
fruitBag.add("Pear");
fruitBag.add("Plum");
fruitBag.add("Peach");
System.out.println(" Display Bag after 3 adds");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
fruitBag.add("Mango");
System.out.println(" Display bag after 1 additional add");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// does banana exist?
if (fruitBag.exists("banana"))
System.out.println(" Yes! There is a banana in the bag!");
else
System.out.println(" No! No banana in the bag");
// does plum exist?
if (fruitBag.exists("plum"))
System.out.println(" Yes! There is a plum in the bag!");
else
System.out.println(" No! No plum in the bag");
System.out.println(" Number of banana? " + fruitBag.countOccurrences("banana"));
System.out.println(" Number of plum? " + fruitBag.countOccurrences("plum"));
// attempt to remove banana
if (fruitBag.remove("banana"))
System.out.println(" Was able to remove banana.");
else
System.out.println(" Sorry! unable to remove banana!");
System.out.println(" Display Bag after removing banana?");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove plum
if (fruitBag.remove("plum"))
System.out.println(" Was able to remove plum.");
else
System.out.println(" Sorry! unable to remove plum!");
System.out.println(" Display Bag after removing plum");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
}
}
StringBagSimpleTestExtraCreditCrash.java
public class StringBagSimpleTestExtraCreditCrash
{
/*The main method is the program's starting point.*/
public static void main(String[] args)
{
StringBag fruitBag = new StringBag(9);
System.out.println(" Display Bag upon startup:");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
fruitBag.add("Pear");
fruitBag.add("Banana");
fruitBag.add("Plum");
fruitBag.add("watermellon");
fruitBag.add("Grapefruit");
fruitBag.add("Peach");
fruitBag.add("apple");
fruitBag.add("Mellon");
fruitBag.add("macintosh");
System.out.println(" Display Bag after 9 adds");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove plum (2nd to last)
if (fruitBag.remove("plum"))
System.out.println(" Was able to remove plum.");
else
System.out.println(" Sorry! unable to remove plum!");
System.out.println(" Display Bag after removing watermellon");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
// attempt to remove watermellon (last)
if (fruitBag.remove("watermellon"))
System.out.println(" Was able to remove watermellon.");
else
System.out.println(" Sorry! unable to remove watermellon!");
System.out.println(" Display Bag after removing watermellon");
System.out.println(fruitBag);
System.out.println("Capacity: " + fruitBag.getCapacity() + " Size: " + fruitBag.getSize());
}
}
Sample output
Display Bag upon startup:
empty
Capacity: 9 Size: 0
Display Bag after 9 adds
apple, Banana, Grapefruit, macintosh, Mellon, Peach, Pear, Plum, watermellon,
Capacity: 9 Size: 9
Display bag after 1 additional add
apple, Banana, Grapefruit, macintosh, Mango, Mellon, Peach, Pear, Plum, watermellon,
Capacity: 19 Size: 10
No! No pomegranate in the bag
Yes! There is a macintosh in the bag!
Number of pomegranate? 0
Number of macintosh? 1
Sorry! unable to remove pomegranate!
Display Bag after removing pomegranate?
apple, Banana, Grapefruit, macintosh, Mango, Mellon, Peach, Pear, Plum, watermellon,
Capacity: 19 Size: 10
Was able to remove macintosh.
Display Bag after removing macintosh
apple, Banana, Grapefruit, Mango, Mellon, Peach, Pear, Plum, watermellon,
Capacity: 19 Size: 9
Was able to remove apple.
Display Bag after removing apple
Banana, Grapefruit, Mango, Mellon, Peach, Pear, Plum, watermellon,
Capacity: 19 Size: 8
Was able to remove watermellon.
Display Bag after removing watermellon
Banana, Grapefruit, Mango, Mellon, Peach, Pear, Plum,
Capacity: 19 Size: 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.