The following 3 methods are to be added to the Array-based implementation of the
ID: 3752074 • Letter: T
Question
The following 3 methods are to be added to the Array-based implementation of the Bag ADT (ArrayBag.h and ArrayBag.cpp). Methods must compose a new Bag object by accessing the elements of the underlying array of the Bag objects. You may not convert the Bags to vectors in the completion of these methods. The main method must also be updated to thoroughly test the newly added methods. Your output does not need to match the samples shown.
Note: Remember that the ArrayBag is a template, not an actual class. The last line of ArrayBag.h says: #include "ArrayBag.cpp". Therefore, this file is in the folder that you unzipped and can be opened and edited with NetBeans by selecting File/Open File..., but cannot be added to the project. We discussed alternatives to this method. You may copy and paste the code from the cpp into the header so you have it all in one place. Or you can add the cpp to the project and include it in the file with main(). To add the cpp to the project, right click the project and select "Add existing item..." Also note that the publisher uses <ItemType> instead of the usual <T>.
1) bagUnion: The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method union for the ArrayBag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method's parameter. The method signature (which should appear in the .h file and be implemented in the .cpp file is:
ArrayBag<ItemType> bagUnion(const ArrayBag<ItemType> &otherBag) const;
This method would be called in main() with:
ArrayBag<int> bag1u2 = bag1.bagUnion(bag2);
Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. The union does not affect the contents of the original bags.
2) bagIntersection: The intersection of two bags is a new bag containing the entries that occur in both of the original bags. Design and specify a method intersection for the ArrayBag that returns as a new bag the intersection of the bag receiving the call to the method and the bag that is the method's parameter. The method signature is:
ArrayBag<ItemType> bagIntersection(const ArrayBag<ItemType> &otherBag) const;
Note that the intersection of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the intersection of these bags contains x two times. The intersection does not affect the contents of the original bags.
3) bagDifference: The difference of two bags is a new bag containing the entries that would be left in one bag after removing those that also occur in the second. Design and specify a method difference for the ArrayBag that returns as a new bag the difference of the bag receiving the call to the method and the bag that is the method's parameter. The method signature is:
ArrayBag<ItemType> bagDifference(const ArrayBag<ItemType> &otherBag) const;
Note that the difference of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the difference of these bags contains x three times.
First bag: The bag contains 5 items: 1 2 3 4 5 Second bag: The bag contains 5 items: 20 30 40 50 1 The bag containing the union of these bags: The bag contains 10 items: 1 2 3 4 5 20 30 40 50 1 First bag: The bag contains 5 items: one two three four five Second bag: The bag contains 5 items: twenty thirty fourty fifty one The bag containing the union of these bags: The bag contains 10 items: one two three four five twenty thirty fourty fifty oneExplanation / Answer
// File Name: TemplateArrayBag.h
#ifndef ArrayBag_H
#define ArrayBag_H
#include <iostream>
#define MAX 50 // Maximum size of array
using namespace std;
// Defines a template class ArrayBag
template <class ItemType>
class ArrayBag
{
private:
// Template type array of size MAX to store data
ItemType itemArray[MAX];
// To store the length of the array
int length;
public:
// Prototype of member function
ArrayBag();
void addItem(ItemType);
ItemType getItem(int);
int getLength();
void showItem();
ArrayBag<ItemType> bagUnion(const ArrayBag<ItemType> &otherBag) const;
ArrayBag<ItemType> bagIntersection(const ArrayBag<ItemType> &otherBag) const;
ArrayBag<ItemType> bagDifference(const ArrayBag<ItemType> &otherBag) const;
};// End of class
#endif
-----------------------------------------------------------------------------------------------------
// File Name: TemplateArrayBag.cpp
#include "TemplateArrayBag.h"
#include <iostream>
using namespace std;
// Default constructor definition
template <class ItemType>
ArrayBag<ItemType>::ArrayBag()
{
length = 0;
}// End of default constructor
// Template type of function to return the length of the string
template <class ItemType>
int ArrayBag<ItemType>::getLength()
{
return length;
}// End of function
// Template type function to add an item to the array
template <class ItemType>
void ArrayBag<ItemType>::addItem(ItemType item)
{
itemArray[length++] = item;
}// End of function
// Template type function to return an item at index position given as parameter from the array
template <class ItemType>
ItemType ArrayBag<ItemType>::getItem(int index)
{
return itemArray[index];
}// End of function
// Template type function to show the array
template <class ItemType>
void ArrayBag<ItemType>::showItem()
{
// Loops till length of the array
for(int x = 0; x < length; x++)
// Displays each index position of the array
cout<<itemArray[x]<<", ";
cout<<endl;
}// End of function
// Template type function to perform union operation
// Parameter: const ArrayBag<ItemType> &otherBag is a ArrayBag class object of type template (otherBag)
// Returns: ArrayBag<ItemType> is a ArrayBag class object of type template (temp)
template <class ItemType>
ArrayBag<ItemType> ArrayBag<ItemType>::bagUnion(const ArrayBag<ItemType> &otherBag) const
{
// Creates a temporary object of template type of class ArrayBag using default constructor
ArrayBag<ItemType> temp;
// Declares a counter for temp object
int counter;
// Loops till the length of the implicit available object array length
for(counter = 0; counter < length; counter++)
// Assigns x index position of array belongs to implicit available object
// to counter index position of temp object's array
temp.itemArray[counter] = itemArray[counter];
// Loops till the length of the parameter object array length
for(int x = 0; x < otherBag.length; x++, counter++)
// Assigns x index position of array belongs to parameter object
// to counter index position of temp object's array
temp.itemArray[counter] = otherBag.itemArray[x];
// Adds the length of implicit object array length with parameter object array length
// stores it in temporary object length
temp.length = length + otherBag.length;
// Returns the object
return temp;
}// End of function
// Template type function to perform intersection operation
// Parameter: const ArrayBag<ItemType> &otherBag is a ArrayBag class object of type template (otherBag)
// Returns: ArrayBag<ItemType> is a ArrayBag class object of type template (temp)
template <class ItemType>
ArrayBag<ItemType> ArrayBag<ItemType>::bagIntersection(const ArrayBag<ItemType> &otherBag) const
{
// Creates a temporary object of template type of class ArrayBag using default constructor
ArrayBag<ItemType> temp;
// Declares a counter for temp object
int counter = 0;
// Loops till the length of the implicit available object array length
for(int x = 0; x < length; x++)
{
// Loops till the length of the parameter object array length
for(int y = 0; y < otherBag.length; y++)
{
// Checks if x index position of the array belongs to implicit available object
// is equals to y index position of the array belongs to parameter object
if(itemArray[x] == otherBag.itemArray[y])
{
// Assigns x index position of array belongs to parameter object
// to counter index position of temp object's array
// Increase the counter by one
temp.itemArray[counter++] = itemArray[x];
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
// Stores the counter value as the length of the temporary object
temp.length = counter;
// Returns the object
return temp;
}// End of function
// Template type function to perform difference operation
// Parameter: const ArrayBag<ItemType> &otherBag is a ArrayBag class object of type template (otherBag)
// Returns: ArrayBag<ItemType> is a ArrayBag class object of type template (temp)
template <class ItemType>
ArrayBag<ItemType> ArrayBag<ItemType>::bagDifference(const ArrayBag<ItemType> &otherBag) const
{
// Creates a temporary object of template type of class ArrayBag using default constructor
ArrayBag<ItemType> temp;
// Declares a counter for temp object
int counter = 0;
// To store found status
int found;
// Loops till the length of the implicit available object array length
for(int x = 0; x < length; x++)
{
// Sets the found status to -1 for not found for each element of implicit object array
found = -1;
// Loops till the length of the parameter object array length
for(int y = 0; y < otherBag.length; y++)
{
// Checks if x index position of the array belongs to implicit available object
// is equals to y index position of the array belongs to parameter object
if(itemArray[x] == otherBag.itemArray[y])
{
// Sets the found status to loop variable value y
found = y;
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop
// Checks if found status is -1 then element not found
if(found == -1)
// Assigns x index position of array belongs to parameter object
// to counter index position of temp object's array
// Increase the counter by one
temp.itemArray[counter++] = itemArray[x];
}// End of outer for loop
// Stores the counter value as the length of the temporary object
temp.length = counter;
// Returns the object
return temp;
}// End of function
---------------------------------------------------------------------------------------------------
// File Name: MainTemplateArrayBag.cpp
#include "TemplateArrayBag.cpp"
#include <iostream>
using namespace std;
// main function definition
int main()
{
// Creates a string arrays with values
string arrayStr1[] = {"One", "Two", "Three", "Four", "Five"};
string arrayStr2[] = {"Twenty", "Thirty", "Fourth", "Fifty", "One"};
// Creates a integer arrays with values
int arrInt1[] = {1, 2, 3, 4, 5};
int arrInt2[] = {20, 30, 40, 50, 1};
// Declares two objects of class ArrayBag of type integer using default constructor
ArrayBag<int> firstInt;
ArrayBag<int> secondInt;
// Declares two objects of class ArrayBag of type string using default constructor
ArrayBag<string> firstStr;
ArrayBag<string> secondStr;
// Loops 5 times
for(int x = 0; x < 5; x++)
{
// Calls the method to add an element to the array belongs to object
// With the x index position of the local array
firstInt.addItem(arrInt1[x]);
secondInt.addItem(arrInt2[x]);
firstStr.addItem(arrayStr1[x]);
secondStr.addItem(arrayStr2[x]);
}// End of for loop
// Union operation for integer
// Calls the function to display the length of first bag for integer
cout<<" First bag: The bag contains "<<firstInt.getLength()<<" items: ";
// Calls the function to display first information for integer
firstInt.showItem();
// Calls the function to displays the length of second bag for integer
cout<<" Second bag: The bag contains "<<secondInt.getLength()<<" items: ";
// Calls the function to display second information for integer
secondInt.showItem();
// Calls the function to perform union operation for integer
ArrayBag<int> bagUnionInt = firstInt.bagUnion(secondInt);
// Calls the function to displays the length of union bag for integer
cout<<" The bag contains the union of these bags: The bag contains "<<bagUnionInt.getLength()<<" items: ";
// Calls the function to display union bag information for integer
bagUnionInt.showItem();
// Union operation for string
// Calls the function to displays the length of first bag for string
cout<<" First bag: The bag contains "<<firstStr.getLength()<<" items: ";
// Calls the function to display first information for string
firstStr.showItem();
// Calls the function to displays the length of second bag for string
cout<<" Second bag: The bag contains "<<secondStr.getLength()<<" items: ";
// Calls the function to display second information for string
secondStr.showItem();
// Calls the function to perform union operation for string
ArrayBag<string> bagUnionStr = firstStr.bagUnion(secondStr);
// Calls the function to displays the length of union bag for string
cout<<" The bag contains the union of these bags: The bag contains "<<bagUnionStr.getLength()<<" items: ";
// Calls the function to display union bag information for string
bagUnionStr.showItem();
// Intersection operation for integer
cout<<" First bag: The bag contains "<<firstInt.getLength()<<" items: ";
firstInt.showItem();
cout<<" Second bag: The bag contains "<<secondInt.getLength()<<" items: ";
secondInt.showItem();
// Calls the function to perform intersection operation for integer
ArrayBag<int> bagIntersectionInt = firstInt.bagIntersection(secondInt);
cout<<" The bag contains the union of these bags: The bag contains "<<bagIntersectionInt.getLength()<<" items: ";
bagIntersectionInt.showItem();
// Intersection operation for string
cout<<" First bag: The bag contains "<<firstStr.getLength()<<" items: ";
firstStr.showItem();
cout<<" Second bag: The bag contains "<<secondStr.getLength()<<" items: ";
secondStr.showItem();
// Calls the function to perform intersection operation for string
ArrayBag<string> bagIntersectionStr = firstStr.bagIntersection(secondStr);
cout<<" The bag contains the intersection of these bags: The bag contains "<<bagIntersectionStr.getLength()<<" items: ";
bagIntersectionStr.showItem();
// Difference operation for integer
cout<<" First bag: The bag contains "<<firstInt.getLength()<<" items: ";
firstInt.showItem();
cout<<" Second bag: The bag contains "<<secondInt.getLength()<<" items: ";
secondInt.showItem();
// Calls the function to perform difference operation for integer
ArrayBag<int> bagDifferenceInt = firstInt.bagDifference(secondInt);
cout<<" The bag contains the union of these bags: The bag contains "<<bagDifferenceInt.getLength()<<" items: ";
bagDifferenceInt.showItem();
// Difference operation for string
cout<<" First bag: The bag contains "<<firstStr.getLength()<<" items: ";
firstStr.showItem();
cout<<" Second bag: The bag contains "<<secondStr.getLength()<<" items: ";
secondStr.showItem();
// Calls the function to perform difference operation for string
ArrayBag<string> bagDifferenceStr = firstStr.bagDifference(secondStr);
cout<<" The bag contains the intersection of these bags: The bag contains "<<bagDifferenceStr.getLength()<<" items: ";
bagDifferenceStr.showItem();
}// End of main function
Sample Output:
First bag: The bag contains 5 items:
1, 2, 3, 4, 5,
Second bag: The bag contains 5 items:
20, 30, 40, 50, 1,
The bag contains the union of these bags: The bag contains 10 items:
1, 2, 3, 4, 5, 20, 30, 40, 50, 1,
First bag: The bag contains 5 items:
One, Two, Three, Four, Five,
Second bag: The bag contains 5 items:
Twenty, Thirty, Fourth, Fifty, One,
The bag contains the union of these bags: The bag contains 10 items:
One, Two, Three, Four, Five, Twenty, Thirty, Fourth, Fifty, One,
First bag: The bag contains 5 items:
1, 2, 3, 4, 5,
Second bag: The bag contains 5 items:
20, 30, 40, 50, 1,
The bag contains the union of these bags: The bag contains 1 items:
1,
First bag: The bag contains 5 items:
One, Two, Three, Four, Five,
Second bag: The bag contains 5 items:
Twenty, Thirty, Fourth, Fifty, One,
The bag contains the intersection of these bags: The bag contains 1 items:
One,
First bag: The bag contains 5 items:
1, 2, 3, 4, 5,
Second bag: The bag contains 5 items:
20, 30, 40, 50, 1,
The bag contains the union of these bags: The bag contains 4 items:
2, 3, 4, 5,
First bag: The bag contains 5 items:
One, Two, Three, Four, Five,
Second bag: The bag contains 5 items:
Twenty, Thirty, Fourth, Fifty, One,
The bag contains the intersection of these bags: The bag contains 4 items:
Two, Three, Four, Five,